From 2c159f7f5e54032e7520a62b2da91d38c0d6fe92 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Jul 2020 10:07:21 +0200 Subject: Task 1 done. --- challenge-069/luca-ferrari/raku/ch-1.p6 | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 challenge-069/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-069/luca-ferrari/raku/ch-1.p6 b/challenge-069/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..58e45ee97f --- /dev/null +++ b/challenge-069/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,45 @@ +#!raku + +sub MAIN( Int $A where { 1 <= $A <= 10**15 } + , Int $B where { $A <= $B <= 10**15 } ) { + say "Working from $A to $B"; + + my %reverse = 0 => 0 + , 1 => 1 + , 6 => 9 + , 8 => 8 + , 9 => 6; + + my @found; + + for $A .. $B { + # special case: single number + @found.push( $_ ) && next if $_.chars == 1 && $_ == any( 0, 1, 8 ); + + my @digits = $_.split: '', :skip-empty; + + # special case: if the number of digits is odd + # the central digit must be a self reversing one + next if ! @digits.elems %% 2 + && @digits[ ( @digits.elems / 2 ).Int ] != any ( 0, 1 , 8 ); + + my $ok = True; + CHECKING: + for 0 ..^ @digits.elems -> $index { + my ( $left, $right ) = $index, @digits.elems - $index - 1; + last if $left == $right || $left > $right; + $ok = False if ( %reverse{ @digits[ $left ] }:!exists ) + || ( %reverse{ @digits[ $right ] }:!exists ); + + last CHECKING if ! $ok; + $ok &= %reverse{ @digits[ $left ] } == @digits[ $right ]; + last CHECKING if ! $ok; + } + + @found.push: $_ if $ok; + } + + + + @found.join( ', ' ).say; +} -- cgit From 1de237043d68e612ce59f40df13be2b4c899f7b6 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Jul 2020 10:18:29 +0200 Subject: Usa gather/take around the for loop. --- challenge-069/luca-ferrari/raku/ch-1.p6 | 55 ++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/challenge-069/luca-ferrari/raku/ch-1.p6 b/challenge-069/luca-ferrari/raku/ch-1.p6 index 58e45ee97f..018e9ce2e0 100644 --- a/challenge-069/luca-ferrari/raku/ch-1.p6 +++ b/challenge-069/luca-ferrari/raku/ch-1.p6 @@ -1,7 +1,11 @@ #!raku -sub MAIN( Int $A where { 1 <= $A <= 10**15 } - , Int $B where { $A <= $B <= 10**15 } ) { + + + + +sub MAIN( Int $A where { 1 <= $A <= 10**15 } = 1 + , Int $B where { $A <= $B <= 10**15 } = 10**15 ) { say "Working from $A to $B"; my %reverse = 0 => 0 @@ -10,36 +14,37 @@ sub MAIN( Int $A where { 1 <= $A <= 10**15 } , 8 => 8 , 9 => 6; - my @found; - for $A .. $B { - # special case: single number - @found.push( $_ ) && next if $_.chars == 1 && $_ == any( 0, 1, 8 ); + my @found = gather { + for $A .. $B { + # special case: single number + take $_ if $_.chars == 1 && $_ == any( 0, 1, 8 ); - my @digits = $_.split: '', :skip-empty; + my @digits = $_.split: '', :skip-empty; - # special case: if the number of digits is odd - # the central digit must be a self reversing one - next if ! @digits.elems %% 2 - && @digits[ ( @digits.elems / 2 ).Int ] != any ( 0, 1 , 8 ); + # special case: if the number of digits is odd + # the central digit must be a self reversing one + next if ! @digits.elems %% 2 + && @digits[ ( @digits.elems / 2 ).Int ] != any ( 0, 1 , 8 ); - my $ok = True; - CHECKING: - for 0 ..^ @digits.elems -> $index { - my ( $left, $right ) = $index, @digits.elems - $index - 1; - last if $left == $right || $left > $right; - $ok = False if ( %reverse{ @digits[ $left ] }:!exists ) - || ( %reverse{ @digits[ $right ] }:!exists ); + my $ok = True; + CHECKING: + for 0 ..^ @digits.elems -> $index { + my ( $left, $right ) = $index, @digits.elems - $index - 1; + last if $left == $right || $left > $right; + $ok = False if ( %reverse{ @digits[ $left ] }:!exists ) + || ( %reverse{ @digits[ $right ] }:!exists ); - last CHECKING if ! $ok; - $ok &= %reverse{ @digits[ $left ] } == @digits[ $right ]; - last CHECKING if ! $ok; - } + last CHECKING if ! $ok; + $ok &= %reverse{ @digits[ $left ] } == @digits[ $right ]; + last CHECKING if ! $ok; + } - @found.push: $_ if $ok; - } + take $_ if $ok; + } + } # end of gather + @found.unique.join( ', ' ).say; - @found.join( ', ' ).say; } -- cgit From ee350f871100c088452662657738ba74717bf5ab Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Jul 2020 10:51:26 +0200 Subject: Task 2 done. --- challenge-069/luca-ferrari/raku/ch-2.p6 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 challenge-069/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-069/luca-ferrari/raku/ch-2.p6 b/challenge-069/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..2b58f28b1c --- /dev/null +++ b/challenge-069/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,31 @@ +#!raku + + + +sub switch( Str $string where { $string ~~ / ^ <[0 1]>+ $ / } ) { + my @bits; + for $string.split( '', :skip-empty ) { + @bits.push( 0 ) && next if $_ == 1; + @bits.push: 1 if $_ == 0; + } + + @bits.join; +} + + +sub reverse( Str $string where { $string ~~ / ^ <[0 1]>+ $ / } ) { + $string.split( '', :skip-empty ).reverse.join; +} + +sub MAIN( Int:D $max? = 100 ) { + my $current-string; + for 0 .. $max { + $current-string = '' if $_ == 0; + $current-string = '0' if $_ == 1; + next if $_ <= 1; + $current-string = $current-string ~ '0' ~ switch( reverse( $current-string ) ); + } + + $current-string.say; + +} -- cgit From 01a69006d8e88e6d5f402deb2620a261af34c155 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Jul 2020 11:28:50 +0200 Subject: Blog references. --- challenge-069/luca-ferrari/blog-1.txt | 1 + challenge-069/luca-ferrari/blog-2.txt | 1 + challenge-069/luca-ferrari/raku/ch-2.p6 | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 challenge-069/luca-ferrari/blog-1.txt create mode 100644 challenge-069/luca-ferrari/blog-2.txt diff --git a/challenge-069/luca-ferrari/blog-1.txt b/challenge-069/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..7eecad9c00 --- /dev/null +++ b/challenge-069/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/07/13/PerlWeeklyChallenge69.html#task1 diff --git a/challenge-069/luca-ferrari/blog-2.txt b/challenge-069/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..27ef7197c5 --- /dev/null +++ b/challenge-069/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/07/13/PerlWeeklyChallenge69.html#task2 diff --git a/challenge-069/luca-ferrari/raku/ch-2.p6 b/challenge-069/luca-ferrari/raku/ch-2.p6 index 2b58f28b1c..4603520f6f 100644 --- a/challenge-069/luca-ferrari/raku/ch-2.p6 +++ b/challenge-069/luca-ferrari/raku/ch-2.p6 @@ -26,6 +26,6 @@ sub MAIN( Int:D $max? = 100 ) { $current-string = $current-string ~ '0' ~ switch( reverse( $current-string ) ); } - $current-string.say; + $current-string.csay; } -- cgit