Perl Weekly Challenge 52: overlapping ranges and nobel numbers

CORONAVIRUS

PWC 52 - Task 1

+1

-1

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( "

" );

$from

$to

+1

-1

100

$h = 1

$dd

1 + 1

1 - 1

$uu

2 + 1

2 - 1

101

123

1

$h

2

PWC 52 - Task 2

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; }

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; } }

One way to let me improve my knowledge about Raku (aka Perl 6) is to implement programs in it. Unluckily, I don’t have any production code to implement in Raku yet (sob!). So, why not try solving the Perl Weekly Challenge tasks?In the following, the assigned tasks for Challenge 52 The situation in Italy is ugly and to some extent desperate: it has been a week since I’m closed into my house with my son and my wife, we cannot move and cannot go outside. Probably the soldiers will become to monitor the streets very soon this week.I cannot go visiting my mum that lives 10 minutes by car from me, and it is not clear when this emergency status will give us a break.So, Perl Weekly Challenge is not my first thought right now, but at the same time it is a way to stay in touch with the Perl community and also to get into the same routine that let me think everything is fine, at least for the time I spend in front of a Perl script! The first task was about Stepping numbers, numbers where the difference between single adiacent digits is only one unit.The first approach I thought about was to iterate over a sequence, disassembling every number into its single digits and then comparing every digit. Then, I realized that I can do the same forward: fixed the first number, I can compute every following digit keeping a difference ofand. Therefore, my solution is:I disassemble the starting numberand ending numberinto their own digits. Then I loop until I’m sure I’ve gone over the ending number. At every iteration I compute the following number addingandand do the same with the following number. Therefore, starting from, I got, then I computeasand, finally I computeasand. The result provides meand. There are no more stepping numbers that begins with, so I incrementand strt with, and so on. Task 2 was not clear to me: it was a play againt the computer in the search of picking more valuable moneys from a set on the table.Assuming I want to cheat, and made myself win (as if I was really playing), the player will always choose first the money with the highest value. So:Once the player has done his move, the computer must do the opposite.If we want to simulate a random challenge, the program becomes even more simple:At every turn a random number is picked and so the player picks from the left or the right of the list and the computer does the opposite.I suspet the intent of the task was to consider all the possibilities, that make me think a tree structure has to be built.