aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2020-06-29 10:04:00 +0200
committerLuca Ferrari <fluca1978@gmail.com>2020-06-29 10:04:00 +0200
commit41994ccfdc6245a1586bd3b00a6c053c15707e8f (patch)
tree44466b50d47d2fe5e50222da8306ce6ef8aeff2f
parent2136a6d92e9970bf708f65c49a53ef639d6dc2c0 (diff)
downloadperlweeklychallenge-club-41994ccfdc6245a1586bd3b00a6c053c15707e8f.tar.gz
perlweeklychallenge-club-41994ccfdc6245a1586bd3b00a6c053c15707e8f.tar.bz2
perlweeklychallenge-club-41994ccfdc6245a1586bd3b00a6c053c15707e8f.zip
Task 1 done.
-rw-r--r--challenge-067/luca-ferrari/raku/ch-1.p650
1 files changed, 50 insertions, 0 deletions
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;
+}