diff options
| -rw-r--r-- | challenge-052/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-052/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-052/luca-ferrari/raku/ch-1.p6 | 45 | ||||
| -rw-r--r-- | challenge-052/luca-ferrari/raku/ch-2.p6 | 59 |
4 files changed, 106 insertions, 0 deletions
diff --git a/challenge-052/luca-ferrari/blog-1.txt b/challenge-052/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..250f46f2b9 --- /dev/null +++ b/challenge-052/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/03/17/PerlWeeklyChallenge52.html#task1 diff --git a/challenge-052/luca-ferrari/blog-2.txt b/challenge-052/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..d07ba23638 --- /dev/null +++ b/challenge-052/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/03/17/PerlWeeklyChallenge52.html#task2 diff --git a/challenge-052/luca-ferrari/raku/ch-1.p6 b/challenge-052/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..5662de8d30 --- /dev/null +++ b/challenge-052/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,45 @@ +#!env raku +# +# +# Task 1 +# <https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/> +# +# Write a script to accept two numbers between 100 and 999. +# It should then print all Stepping Numbers between them. +# +# A number is called a stepping number if the adjacent digits +# have a difference of 1. +# For example, 456 is a stepping number but 129 is not. + + + + + +sub MAIN( Int:D :$from where { 100 <= $from <= 999 }, + Int:D :$to where { 100 <= $to <= 999 && $to > $from } ) { + + say "Searching STEPPING NUMBERs between $from and $to"; + + + # compose manually all possible stepping numbers + # starting from the hundred value of the from number + my ( $h, $d, $u ) = $from.comb; + my ( $H, $D, $U ) = $to.comb; + my @stepping = (); + while ( $h <= $H ) { + + for 1, -1 { + my $dd = ( 0 <= $h + $_ <= 9 ) ?? $h + $_ !! Nil; + for 1, -1 { + my $uu = ( 0 <= $dd + $_ <= 9 ) ?? $dd + $_ !! Nil; + + @stepping.push( $h * 100 + $dd * 10 + $uu ) if ( $uu && $dd ); + } + } + + $h++; + } + + say @stepping.grep( $from <= * <= $to ).join( "\n" ); + say "-----------------------"; +} diff --git a/challenge-052/luca-ferrari/raku/ch-2.p6 b/challenge-052/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..edbc65125b --- /dev/null +++ b/challenge-052/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,59 @@ +#!env raku +# +# +# Task 2 +# <https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/> +# +# Suppose there are following coins arranged on a table in a line in random order. +# £1, 50p, 1p, 10p, 5p, 20p, £2, 2p +# Suppose you are playing against the computer. +# Player can only pick one coin at a time from either ends. +# Find out the lucky winner, who has the larger amounts in total? + +my @moneys = 1, 0.5, 0.01, 0.05, 0.2, 2, 0.02; + + +@moneys.say; +my @moves; + +my ( @player, @computer ); + + +# First approach: let's cheat and make the player choose always the max +# value there is on the table +while ( @moneys.elems ) { + my $left = @moneys.shift || 0; + my $right = @moneys.pop || 0; + + @player.push: $left > $right ?? $left !! $right; + @computer.push: $left > $right ?? $right !! $left; +} + + +say "Player wins with { @player } = { [+] @player } vs { @computer } = { [+] @computer }"; + + +say "Let's play another random turn"; + +@player = (); +@computer = (); +@moneys = 1, 0.5, 0.01, 0.05, 0.2, 2, 0.02; + +while ( @moneys.elems ) { + if 99.rand.Int %% 2 { + @player.push: @moneys.shift || 0; + @computer.push: @moneys.pop || 0; + } + else { + @player.push: @moneys.pop || 0; + @computer.push: @moneys.shift || 0; + + } +} + +my $player-score = [+] @player; +my $computer-score = [+] @computer; + +say "Computer wins with { @computer } = $computer-score VS { @player } = $player-score" if $computer-score > $player-score; +say "Computer looses with { @computer } = $computer-score VS { @player } = $player-score" if $computer-score < $player-score; +say "Tie!" if $computer-score == $player-score; |
