aboutsummaryrefslogtreecommitdiff
path: root/challenge-048
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-02-17 10:46:57 +0000
committerGitHub <noreply@github.com>2020-02-17 10:46:57 +0000
commit1442ff0d42b7cf42abe46f4e9a83c5b63d62008a (patch)
tree1af1d999727c360a6ee8822cd4a5621059a50349 /challenge-048
parent8524fe24762991f12ea172c387e0e9e843cab5b4 (diff)
parent856bf8cc3bb14a4c2dee88dd0f3feebf4fd48059 (diff)
downloadperlweeklychallenge-club-1442ff0d42b7cf42abe46f4e9a83c5b63d62008a.tar.gz
perlweeklychallenge-club-1442ff0d42b7cf42abe46f4e9a83c5b63d62008a.tar.bz2
perlweeklychallenge-club-1442ff0d42b7cf42abe46f4e9a83c5b63d62008a.zip
Merge pull request #1268 from fluca1978/pwc48
Pwc48
Diffstat (limited to 'challenge-048')
-rw-r--r--challenge-048/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-048/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-048/luca-ferrari/raku/ch-1.p657
-rw-r--r--challenge-048/luca-ferrari/raku/ch-2.p631
4 files changed, 90 insertions, 0 deletions
diff --git a/challenge-048/luca-ferrari/blog-1.txt b/challenge-048/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..c74aff5631
--- /dev/null
+++ b/challenge-048/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/02/17/PerlWeeklyChallenge48.html#task1
diff --git a/challenge-048/luca-ferrari/blog-2.txt b/challenge-048/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..c27011114c
--- /dev/null
+++ b/challenge-048/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/02/17/PerlWeeklyChallenge48.html#task2
diff --git a/challenge-048/luca-ferrari/raku/ch-1.p6 b/challenge-048/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..f8f29f1f71
--- /dev/null
+++ b/challenge-048/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,57 @@
+#!env raku
+
+# Perl Weekly Challenge 48
+# see <https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/>
+#
+# Task 1
+# 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.
+
+
+# Given the list of people, find out
+# the next living person after the current one specified.
+#
+# Implements the list rotation.
+sub next-alive( @people, $current-person ) {
+ my $next = $current-person;
+
+ loop {
+ $next++;
+ $next = $next >= @people.elems ?? $next % @people.elems !! $next;
+ return $next if @people[ $next ].defined;
+ }
+}
+
+
+
+sub MAIN( Int :$how_many_people = 50 ) {
+
+ # False means that the person does not have the sword,
+ # True means she has
+ # Nil means the person is died
+ my @people = False xx $how_many_people;
+
+ # give the sword
+ @people[ 0 ] = True;
+
+ while ( @people.grep( *.defined ) > 1 ) {
+ # find out who has the sword
+ my $killer = @people.first: *.so, :k;
+ # then find out the next person to kill
+ my $killed = next-alive( @people, $killer );
+ @people[ $killed ] = Nil; # killed!
+ @people[ $killer ] = False; # pass the sword
+ # now get the next person that will hold the sword
+ my $next-killer = next-alive( @people, $killed );
+ @people[ $next-killer ] = True; # the next killer
+ }
+
+ "The person who survives is { $_ + 1 }".say given @people.first: *.so, :k;
+}
diff --git a/challenge-048/luca-ferrari/raku/ch-2.p6 b/challenge-048/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..89f23ca19b
--- /dev/null
+++ b/challenge-048/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,31 @@
+#!env perl6
+# Perl Weekly Challenge 48
+# see <https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/>
+#
+# Task 2
+# 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.
+
+
+
+sub MAIN( Int :$year-start? = 2000,
+ Int :$year-end? = 2999 ) {
+
+
+ my $current-date = Date.new( :year( $year-start ),
+ :day(1),
+ :month(1),
+ formatter => { sprintf( "%02d%02d%04d", .month, .day, .year ) } );
+ my $end-date = Date.new( :year( $year-end ), :day(31), :month(12) );
+
+ for 1 .. $end-date - $current-date {
+ $current-date += 1; # add one day at a time
+ # print the date if its string representation is the same as
+ # the flipped string representation
+ "Palindrome: $current-date".say if $current-date.Str ~~ $current-date.Str.flip;
+ }
+
+}