From 63478e77a448c00e010a0361e506c0bf294a1766 Mon Sep 17 00:00:00 2001 From: Duane Powell Date: Tue, 18 Feb 2020 10:05:38 -0600 Subject: Commit solutions for perl weekly challenge 048 --- challenge-048/duane-powell/perl5/ch-1.pl | 57 +++++++++++++++++++++++++++++ challenge-048/duane-powell/perl5/ch-2.pl | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100755 challenge-048/duane-powell/perl5/ch-1.pl create mode 100755 challenge-048/duane-powell/perl5/ch-2.pl diff --git a/challenge-048/duane-powell/perl5/ch-1.pl b/challenge-048/duane-powell/perl5/ch-1.pl new file mode 100755 index 0000000000..8ab2eb41d0 --- /dev/null +++ b/challenge-048/duane-powell/perl5/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature 'say'; + +# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ Task #1 + +my $SWORDSMAN = shift || 50; + +# These constants are not needed but it could be fun to add more weapons and rules +use constant { + NO_SWORD => 0, + SWORD => 1, +}; + +# Swordsman constructor +sub new_swordsman { + my $name = shift; + my $armed = shift; + return {name => $name, armed => $armed, next => undef}; +} + +# Create n swordsman and arrange them in a cirle +my $first_swordsman = new_swordsman(1, SWORD); +my $swordsman = $first_swordsman; +foreach my $s (2 .. $SWORDSMAN) { + my $next = new_swordsman($s, NO_SWORD); + # expand the circle and continue + $swordsman->{next} = $next; + $swordsman = $next; +} +$swordsman->{next} = $first_swordsman; + +# Execute man to your right and pass the sword until there is +# only one man standing, i.e. the next swordsman is yourself +$swordsman = $first_swordsman; +until ($swordsman->{next} == $swordsman) { + my $condemned = $swordsman->{next}; # ID the condemned + my $next = $condemned->{next}; # ID who gets the SWORD next + $condemned = undef; # execute the condemned + $swordsman->{armed} = NO_SWORD; # pass the sword + $next->{armed} = SWORD; + # contract the circle and continue + $swordsman->{next} = $next; + $swordsman = $next; +} + +say "$SWORDSMAN Swordsman arranged in a circle, the last man standing is Swordsman " . $swordsman->{name}; + +__END__ + +./ch-1.pl +50 Swordsman arranged in a circle, the last man standing is Swordsman 37 + +./ch-1.pl 3 +3 Swordsman arranged in a circle, the last man standing is Swordsman 3 + diff --git a/challenge-048/duane-powell/perl5/ch-2.pl b/challenge-048/duane-powell/perl5/ch-2.pl new file mode 100755 index 0000000000..8a4d2a7033 --- /dev/null +++ b/challenge-048/duane-powell/perl5/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature 'say'; +use DateTime; +use DateTime::Duration; + +# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ Task #2 + +my $year_end = shift || 3000; + +my $dt = DateTime->new('year' => 2000, 'month' => 1, 'day' => 1); +my $day = DateTime::Duration->new('days' => 1); + +until ($dt->year == $year_end) { + my @pali = split('',$dt->mdy); + # @pali is a 10 element array MM-DD-YYYY + say $dt->mdy() if ($pali[0] == $pali[9] and $pali[1] == $pali[8] and $pali[3] == $pali[7] and $pali[4] == $pali[6]); + $dt->add( $day ); +} + +__END__ + +./ch-2.pl +10-02-2001 +01-02-2010 +11-02-2011 +02-02-2020 +12-02-2021 +03-02-2030 +04-02-2040 +05-02-2050 +06-02-2060 +07-02-2070 +08-02-2080 +09-02-2090 +10-12-2101 +01-12-2110 +11-12-2111 +02-12-2120 +12-12-2121 +03-12-2130 +04-12-2140 +05-12-2150 +06-12-2160 +07-12-2170 +08-12-2180 +09-12-2190 +10-22-2201 +01-22-2210 +11-22-2211 +02-22-2220 +12-22-2221 +03-22-2230 +04-22-2240 +05-22-2250 +06-22-2260 +07-22-2270 +08-22-2280 +09-22-2290 + -- cgit From 0a6fd9c3a63a5315ff28e0cd8a134ce2c081a7e7 Mon Sep 17 00:00:00 2001 From: duanepowell Date: Tue, 17 Nov 2020 12:16:59 -0600 Subject: Commit solution to task #! --- challenge-087/duane-powell/README | 2 ++ challenge-087/duane-powell/perl/ch-1.pl | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 challenge-087/duane-powell/README create mode 100755 challenge-087/duane-powell/perl/ch-1.pl diff --git a/challenge-087/duane-powell/README b/challenge-087/duane-powell/README new file mode 100644 index 0000000000..8d755eaceb --- /dev/null +++ b/challenge-087/duane-powell/README @@ -0,0 +1,2 @@ +Solutions by Duane Powell + diff --git a/challenge-087/duane-powell/perl/ch-1.pl b/challenge-087/duane-powell/perl/ch-1.pl new file mode 100755 index 0000000000..fd1bd968fe --- /dev/null +++ b/challenge-087/duane-powell/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature 'say'; + +# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-087/ TASK #1 +# You are given an unsorted array of integers @N. +# Write a script to find the longest consecutive sequence. Print 0 if none sequence found + +my @N = @ARGV; +unless (scalar @N) { + # @N is empty so fill with random numbers + push @N, int(rand(100))+1 for (1..40); + print join(',',@N), "\n"; + +} + +@N = sort @N; +my @longest; + +# Init @temp with first elemenet of @N +my @temp = (shift @N); +while (scalar @N) { + + # Is the last element of @temp consecutive with the next element of @N ? + if ( $temp[-1]+1 == $N[0] ) { + # Yes, push element + push @temp, shift @N; + } + else { + # No, re-init @temp with first element of @N + @temp = (shift @N); + } + + # Note: the winner of ties go to the first sequence found + @longest = @temp if (scalar @temp > scalar @longest); + +} + +say scalar(@longest) ? join(',',@longest) : 0; +exit; + + -- cgit