diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-23 07:11:40 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-23 07:11:40 +0000 |
| commit | f01edce0371760d94081baef0f72ff5033d9858c (patch) | |
| tree | ef367e8be9fdf94cb7c34874c32eff889f06d08e | |
| parent | 55a399590d1e4f87661c9e4a9851072e9e73d3f9 (diff) | |
| parent | 3276df0d24c74a9ca79287df779333b64cfd6a6e (diff) | |
| download | perlweeklychallenge-club-f01edce0371760d94081baef0f72ff5033d9858c.tar.gz perlweeklychallenge-club-f01edce0371760d94081baef0f72ff5033d9858c.tar.bz2 perlweeklychallenge-club-f01edce0371760d94081baef0f72ff5033d9858c.zip | |
Merge pull request #1295 from user-person/master
User-person's solutions for challenge 48
| -rwxr-xr-x | challenge-048/user-person/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-048/user-person/perl/ch-2.pl | 80 | ||||
| -rwxr-xr-x | challenge-048/user-person/python/ch-1.py | 59 | ||||
| -rwxr-xr-x | challenge-048/user-person/python/ch-2.py | 71 |
4 files changed, 274 insertions, 0 deletions
diff --git a/challenge-048/user-person/perl/ch-1.pl b/challenge-048/user-person/perl/ch-1.pl new file mode 100755 index 0000000000..777e6f6132 --- /dev/null +++ b/challenge-048/user-person/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-1.pl # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ # +# # +# Survivor # +# There are 50 people standing in a circle in position 1 to 50. # +# The person standing at position 1 has a sword. He kills the next person # +# i.e. standing at position 2 and pass on the sword to the immediate next # +# i.e. person standing at position 3. Now the person at position 3 does # +# the same and it goes on until only one survives. # +# # +# Write a script to find out the survivor. # +# # +# # +########################################################################### + +use strict; +use warnings; +use Getopt::Long; + +GetOptions(\my %option, 'verbose|v') or die "Bad options\n"; + +# -v or --verbose for detailed output. + +my @circle = ( 1 .. 50 ); + +for (my $i = 1; scalar @circle > 1 ; ++$i) { + + print "i: $i\n" if $option{verbose}; + + my $victim = splice(@circle, $i, 1); + + if ($option{verbose}) { + + print $circle[$i-1] . " killed " . $victim . "\n"; + print "@circle\n\n"; + + } + + # When the end of the array is reached, + # roll back the index to go back around the circle for another iteration. + # In odd cases the next victim will be 0. In even cases it will be 1. + # These have to be adjusted due to the for loop's incrementing: -1 for odd, 0 for even. + + if ( $#circle - $i == 0) { + + $i = -1; + + } elsif ( $#circle - $i == -1) { + + $i = 0; + + } +} + +print "@circle survives\n"; + +__END__ +output: + +37 survives diff --git a/challenge-048/user-person/perl/ch-2.pl b/challenge-048/user-person/perl/ch-2.pl new file mode 100755 index 0000000000..07875214a6 --- /dev/null +++ b/challenge-048/user-person/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-2.pl # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ # +# # +# Palindrome Dates # +# Write a script to print all Palindrome Dates between 2000 and 2999. # +# The format of date is mmddyyyy. For example, the first one was on # +# October 2, 2001 as it is represented as 10022001. # +# # +########################################################################### + +use strict; +use warnings; + +# M M D D Y Y Y Y +# [01][*][012][2][2][012][*][01] +# $k $j $i $i $j $k +# +# $k - Months can only begin with 0 or 1. +# $j - The second months digit needs all numbers e.g. January 01 to October 10 (and of course beyond). +# $i - Days begin with 0,1,2,3, However all years begin with 2 so the cooresponding number +# means all days end in 2. 32 is not a valid day so the 3 is not needed. + +for ( my $i=0; $i < 3; ++$i ){ + + for ( my $j=0; $j < 10; ++$j ){ + + EDGES: + for ( my $k=0; $k < 2; ++$k ){ + + if (( $k == 1 and $j > 2 ) or ( $k == 0 and $j == 0 )) { # Months cannot be > 12 or 00 + next EDGES; + } + print "$k$j-${i}2-2$i$j$k\n"; + } + } +} + +__END__ +output: + +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 diff --git a/challenge-048/user-person/python/ch-1.py b/challenge-048/user-person/python/ch-1.py new file mode 100755 index 0000000000..47285428ff --- /dev/null +++ b/challenge-048/user-person/python/ch-1.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +########################################################################### +# script name: ch-1.py # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ # +# # +# Survivor # +# There are 50 people standing in a circle in position 1 to 50. # +# The person standing at position 1 has a sword. He kills the next person # +# i.e. standing at position 2 and pass on the sword to the immediate next # +# i.e. person standing at position 3. Now the person at position 3 does # +# the same and it goes on until only one survives. # +# # +# Write a script to find out the survivor. # +# # +########################################################################### + +from optparse import OptionParser + +parser = OptionParser() + +parser.add_option("-v","--verbose",action="store_true", dest="verbose") +(options, args) = parser.parse_args() + +circle = list(range(1,51)) + +i = 1 + +while len(circle) > 1: + + if options.verbose: + print ("i : ",i) + + victim = circle.pop(i) + + if options.verbose: + print(circle[i-1], " killed ", victim) + print(*circle, sep =" ") + print() + + # When the end of the array is reached, + # roll back the index to go back around the circle for another iteration. + # In odd cases the next victim will be 0. In even cases it will be 1. + # These have to be adjusted due to the for loop's incrementing -1 for odd, 0 for even. + + if (len(circle)-1) - i == 0: + i = -1 + + elif (len(circle)-1) - i == -1 : + i = 0 + + i += 1 + +print(*circle, " survives") + +# output: +# +# 37 survives diff --git a/challenge-048/user-person/python/ch-2.py b/challenge-048/user-person/python/ch-2.py new file mode 100755 index 0000000000..bcf648a352 --- /dev/null +++ b/challenge-048/user-person/python/ch-2.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +########################################################################### +# script name: ch-2.py # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ # +# # +# Palindrome Dates # +# Write a script to print all Palindrome Dates between 2000 and 2999. # +# The format of date is mmddyyyy. For example, the first one was on # +# October 2, 2001 as it is represented as 10022001. # +# # +########################################################################### + +# M M D D Y Y Y Y +# [01][*][012][2][2][012][*][01] +# k j i i j k +# +# k - Months can only begin with 0 or 1. +# j - The second months digit needs all numbers e.g. January 01 to October 10 (and of course beyond). +# i - Days begin with 0,1,2,3, However all years begin with 2 so the corresponding number +# means all days end in 2. 32 is not a valid day so the 3 is not needed. + +for i in range(3): + for j in range(10): + for k in range(2): + if (k == 1 and j > 2) or (k == 0 and j == 0): + continue + print(k,j,"-",i,"2-2",i,j,k,sep='') + k += 1 + j += 1 + i += 1 + +# output: +# +# 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 |
