From 41994ccfdc6245a1586bd3b00a6c053c15707e8f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Jun 2020 10:04:00 +0200 Subject: Task 1 done. --- challenge-067/luca-ferrari/raku/ch-1.p6 | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-067/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-067/luca-ferrari/raku/ch-1.p6 b/challenge-067/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..ae742e4567 --- /dev/null +++ b/challenge-067/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,50 @@ +#!raku + +# Task 1 +# You are given two integers $m and $n. Write a script print all possible combinations of $n numbers from the list 1 2 3 … $m. +# +# Every combination should be sorted i.e. [2,3] is valid combination but [3,2] is not. + + +sub MAIN( Int :$m where { $m > 2 } = 5, + Int :$n where { $n < $m } = 2 ) { + + # available digits + my @digits = 1 .. $m; + # found combinations + my @combinations; + + for @digits -> $start { + + # build the array of combinations starting with the + # current digits, place another one that is increased by one + # so to keep sorting... + my @combination = $start; + @combination.push: $start + 1; + + + # ... and all an element until I've made the array + while ( @combination.elems < $n ) { + @combination.push( @combination[ *-1 ] + 1 ); + } + + + # the last element of the array must be the value + # I've got as parameter, otherwise iterate + while ( @combination[ *-1 ] < $m ) { + # clone the array because I'm going to change it! + @combinations.push: Array.new( @combination ); + + + # increase by one every element, so it will be kept in + # order + for 1 ..^ @combination.elems { + @combination[ $_ ] += 1; + } + } + + @combinations.push: @combination if ( @combination[ *-1 ] == $m ); + } + + @combinations.join( ", " ).say; +} -- cgit From 9eff05a97c7ac13a99e24ecec74b8ca84a91ed19 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Jun 2020 10:16:27 +0200 Subject: Task 2 done. --- challenge-067/luca-ferrari/raku/ch-2.p6 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-067/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-067/luca-ferrari/raku/ch-2.p6 b/challenge-067/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..e414b630db --- /dev/null +++ b/challenge-067/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#!raku + +# Task 2 +# You are given a digit string $S. Write a script to print all possible letter combinations that the given digit string could represent. + + +sub MAIN( Str $S ) { + my %letters = + 1 => [ '_', ',', '@' ] + , 2 => [ 'A', 'B', 'C' ] + , 3 => [ 'D', 'E', 'F' ] + , 4 => [ 'G', 'H', 'I' ] + , 5 => [ 'J', 'K', 'L' ] + , 6 => [ 'M', 'N', 'O' ] + , 7 => [ 'P', 'Q', 'R', 'S' ] + , 8 => [ 'T', 'U', 'V' ] + , 9 => [ 'W', 'X', 'Y', 'Z' ]; + + my @combinations; + for $S.comb -> $current { + @combinations.push( %letters{ $current } ) if %letters{ $current }:exists; + } + + ( [X] @combinations ).join( "\n" ).lc.say; +} -- cgit From 0ccc8cf05e749451a61254b0ca240fc80b3c7a12 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Jun 2020 11:36:51 +0200 Subject: Blog references. --- challenge-067/luca-ferrari/blog-1.txt | 1 + challenge-067/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-067/luca-ferrari/blog-1.txt create mode 100644 challenge-067/luca-ferrari/blog-2.txt diff --git a/challenge-067/luca-ferrari/blog-1.txt b/challenge-067/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..53886516ee --- /dev/null +++ b/challenge-067/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/06/29/PerlWeeklyChallenge67.html#task1 diff --git a/challenge-067/luca-ferrari/blog-2.txt b/challenge-067/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..f4a8e9f455 --- /dev/null +++ b/challenge-067/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/06/29/PerlWeeklyChallenge67.html#task2 -- cgit From e278df37b0c49be8f048866a815aa43a13e0760a Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Jun 2020 13:35:05 +0200 Subject: A little better implementation of task 1. --- challenge-067/luca-ferrari/raku/ch-1.p6 | 46 ++++++++++++++------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/challenge-067/luca-ferrari/raku/ch-1.p6 b/challenge-067/luca-ferrari/raku/ch-1.p6 index ae742e4567..f9d123fe1d 100644 --- a/challenge-067/luca-ferrari/raku/ch-1.p6 +++ b/challenge-067/luca-ferrari/raku/ch-1.p6 @@ -9,42 +9,34 @@ sub MAIN( Int :$m where { $m > 2 } = 5, Int :$n where { $n < $m } = 2 ) { - # available digits - my @digits = 1 .. $m; # found combinations my @combinations; - for @digits -> $start { + for 0 ..^ $m { - # build the array of combinations starting with the - # current digits, place another one that is increased by one - # so to keep sorting... - my @combination = $start; - @combination.push: $start + 1; + # cannot get negative numbers! + next if $m - $_ < $n; + # current combination + my @combination; + # push the last digit of this combination, for example m=5, n=3 -> 5 + @combination.push: $m - $_; - # ... and all an element until I've made the array - while ( @combination.elems < $n ) { - @combination.push( @combination[ *-1 ] + 1 ); - } - - - # the last element of the array must be the value - # I've got as parameter, otherwise iterate - while ( @combination[ *-1 ] < $m ) { - # clone the array because I'm going to change it! - @combinations.push: Array.new( @combination ); + # fill up the combination with decreasing order + @combination.push( @combination[ *-1 ] - 1 ) while ( @combination.elems < $n ); + # push this initial combination, e.g., 5,4,3 + @combinations.push: Array.new( @combination.reverse ); - # increase by one every element, so it will be kept in - # order - for 1 ..^ @combination.elems { - @combination[ $_ ] += 1; - } + # now go backwards all the digits until we reach '1' with the last digit + # e.g. 5,4,2; 5,4,1 + while ( @combination[ *-1 ] > 1 ) { + @combination[ *-1 ] -= 1; + @combinations.push: Array.new( @combination.reverse ); } - - @combinations.push: @combination if ( @combination[ *-1 ] == $m ); } - @combinations.join( ", " ).say; + + + @combinations.sort.join( ", " ).say; } -- cgit From 14e647fc20118e2813d7e3aa3b31af54677b6aa5 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Jun 2020 13:43:40 +0200 Subject: Brute force at task 1. --- challenge-067/luca-ferrari/raku/ch-1.p6 | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/challenge-067/luca-ferrari/raku/ch-1.p6 b/challenge-067/luca-ferrari/raku/ch-1.p6 index f9d123fe1d..a408701f34 100644 --- a/challenge-067/luca-ferrari/raku/ch-1.p6 +++ b/challenge-067/luca-ferrari/raku/ch-1.p6 @@ -12,31 +12,15 @@ sub MAIN( Int :$m where { $m > 2 } = 5, # found combinations my @combinations; - for 0 ..^ $m { - # cannot get negative numbers! - next if $m - $_ < $n; - - # current combination - my @combination; - # push the last digit of this combination, for example m=5, n=3 -> 5 - @combination.push: $m - $_; - - # fill up the combination with decreasing order - @combination.push( @combination[ *-1 ] - 1 ) while ( @combination.elems < $n ); - # push this initial combination, e.g., 5,4,3 - @combinations.push: Array.new( @combination.reverse ); - - - # now go backwards all the digits until we reach '1' with the last digit - # e.g. 5,4,2; 5,4,1 - while ( @combination[ *-1 ] > 1 ) { - @combination[ *-1 ] -= 1; - @combinations.push: Array.new( @combination.reverse ); - } + for ( 1 x $n ).Int ^..^ ( $m x $n ).Int { + my @digits = $_.comb; + next if @digits.elems != $n; + my $ok = True; + $ok = False if ( @digits[ $_ ] >= @digits[ $_ + 1 ] ) for 0 ..^ @digits.elems - 1; + @combinations.push: @digits if $ok; } - @combinations.sort.join( ", " ).say; } -- cgit From 9c185dffa7f9568cad642076729b93d3e1011168 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Jun 2020 13:48:10 +0200 Subject: Fix boundaries. --- challenge-067/luca-ferrari/raku/ch-1.p6 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/challenge-067/luca-ferrari/raku/ch-1.p6 b/challenge-067/luca-ferrari/raku/ch-1.p6 index a408701f34..ed10fbdcee 100644 --- a/challenge-067/luca-ferrari/raku/ch-1.p6 +++ b/challenge-067/luca-ferrari/raku/ch-1.p6 @@ -6,7 +6,7 @@ # Every combination should be sorted i.e. [2,3] is valid combination but [3,2] is not. -sub MAIN( Int :$m where { $m > 2 } = 5, +sub MAIN( Int :$m where { 10 > $m > 2 } = 5, Int :$n where { $n < $m } = 2 ) { # found combinations @@ -16,6 +16,7 @@ sub MAIN( Int :$m where { $m > 2 } = 5, for ( 1 x $n ).Int ^..^ ( $m x $n ).Int { my @digits = $_.comb; next if @digits.elems != $n; + next if @digits.grep( * > $m ); my $ok = True; $ok = False if ( @digits[ $_ ] >= @digits[ $_ + 1 ] ) for 0 ..^ @digits.elems - 1; @combinations.push: @digits if $ok; -- cgit