diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-20 19:33:21 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-20 19:33:21 +0000 |
| commit | 15eb0fa8a6087bfe772f3ebd529daa444bf153de (patch) | |
| tree | ce134fb1cde23d24208a36ce304f602ae33fc263 | |
| parent | e3a07f9731b7a68fbad58bbecc863655cb39628a (diff) | |
| parent | be71c1b5db8c842aff4319990b39421f1f4dd7e1 (diff) | |
| download | perlweeklychallenge-club-15eb0fa8a6087bfe772f3ebd529daa444bf153de.tar.gz perlweeklychallenge-club-15eb0fa8a6087bfe772f3ebd529daa444bf153de.tar.bz2 perlweeklychallenge-club-15eb0fa8a6087bfe772f3ebd529daa444bf153de.zip | |
Merge pull request #1285 from saiftynet/branch-048
Challenge-048 solutions by saiftynet submitted using EZPWC
| -rw-r--r-- | challenge-048/saiftynet/perl/ch-1.pl | 72 | ||||
| -rw-r--r-- | challenge-048/saiftynet/perl/ch-2.pl | 53 |
2 files changed, 125 insertions, 0 deletions
diff --git a/challenge-048/saiftynet/perl/ch-1.pl b/challenge-048/saiftynet/perl/ch-1.pl new file mode 100644 index 0000000000..cfc025b625 --- /dev/null +++ b/challenge-048/saiftynet/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/env/perl +# Task 1 Challenge 048 Solution by saiftynet +# +# 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. +# +# Probably the best way to implement a circular list is to shift an element +# and then push it back on the ther end...s the whole array rotates, and +# our index remain in the same position, rather than to have a pointer +# that is checked to see if it has reached the boundary every time and +# reset to end or start depending on which way we moving the pointer. +# +# For the purposes of this task, we will have the sword holder at position 0 +# the victime is at postion 1, and the sword receiver is at position 2. +# +# For a bit of fun, an attempt is made at animating this. This works on +# an 80x24 console that supports `cls` in Windows or `clear` in Linux +# or OSX + + +my @circleList=(1..50); + +# clear screen can be done using Term::XXX, but in case the module is +# not accessible, I have adapted an answer from +# https://stackoverflow.com/questions/197933 + +my $clearScreen=( $^O=~/Win/)?`cls`:`clear`; + +my $animate=1; # set this to 0 if you dont want to animate. + +while (scalar @circleList >1){ + if ($animate){ # animation is optional; may not work or be desired + select(undef, undef, undef, 0.25); + print $clearScreen; + circle(\@circleList); + } + else{ + print join ",",@circleList; + } + my $killer=shift @circleList; # get the killer, ready to push to end + my $killed=shift @circleList; # get the killed and dump it + push @circleList,$killer; # push killer to end of list + print "\n$killer killed $killed".# display results + ( (scalar @circleList > 1)? + " and passed sword to $circleList[0]\n": + " and is last man standing!!\n"); + +} + +# subroutine to plot a circular list in a circle. +sub circle{ + my $circleList=shift; + my @plotArea = (); # reset plot area + push @plotArea, [(" ") x 40,"\n"] + foreach (0..16); # two spaces per point + + my $center=[9,20]; # adjust for different console sizes + my $radius=[8,18]; # adjust for different console sizes + foreach my $angle (0..@$circleList-1){ + $plotArea [$$center[0]+$$radius[0]*sin($angle*6.28/@$circleList)] + [$$center[1]-$$radius[1]*cos($angle*6.28/@$circleList)] + =$$circleList[$angle]; + } + print @{$_} foreach (@plotArea); # the plot area +} diff --git a/challenge-048/saiftynet/perl/ch-2.pl b/challenge-048/saiftynet/perl/ch-2.pl new file mode 100644 index 0000000000..edbd65602b --- /dev/null +++ b/challenge-048/saiftynet/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/env/perl +# Task 2 Challenge 048 Solution by saiftynet +# +# 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. + +# With Palindromic dates, the year contains the digits of the month +# and date, so can be easiy derived from the year using substr. +# The MMDDYYY format is a requirement of the task, but is probably the +# most illogical date format (and therefore naturally favoured by +# Americans). +# DDMMYYY (the most common worldwide) and YYYYMMDD (ISO 8601, the most +# logical and least ambiguous format) also can produce different +# palindromic dates. This script can yield these formats too. + +# There are many better ways of getting valid dates via modules. +# Much more interesting to roll-your-own, so didn't use them. + + +# Changing this string will show palindromes in other formats +# valid formats are MDY, DMY and YMD. The format can also be +# passed from the command line using e.g. perl ch-2.pl DMY + +my $format = $ARGV[0] // "MDY"; # valid strings "MDY", "DMY" and "YMD". +die 'Invalid string, must be "MDY", "DMY" or "YMD"' if $format !~/^MDY|DMY|YMD\b/; + +foreach my $yyyy (2000..2999){ + my $mm = substr($yyyy,3,1).substr($yyyy,2,1); + my $dd = substr($yyyy,1,1).substr($yyyy,0,1); + + ($mm,$dd)=($dd,$mm) if $format eq "DMY"; # Swap if DDMMYYY required + + next if ($mm >12 or $mm ==0); # Discard invalid months + if ($mm =~/02/){ # February is special case + $notLeapYear=($y % 4)||(!($y%100)&&($y%400)); + my $FebDays=(28+($notLeapYear?0:1)).""; + next if ($dd gt $FebDays or $dd eq "00") + } + elsif ($mm=~/^01|03|05|07|08|10|12/){ # months with 31 days + next if ($dd gt "31" or $dd eq "00"); + } + else{ # all the rest have 30 days + next if ($dd gt "30" or $dd eq "00"); + + } + + print "M $mm, D $dd, Y $yyyy : $mm$dd$yyyy \n" if $format eq "MDY"; + print "D $dd, M $mm, Y $yyyy : $dd$mm$yyyy \n" if $format eq "DMY"; + print "Y $yyyy, M $mm, D $dd : $yyyy$mm$dd \n" if $format eq "YMD"; +} |
