diff options
| author | carlos157oliveira <carlos157oliveira@gmail.com> | 2023-01-14 18:19:51 -0300 |
|---|---|---|
| committer | carlos157oliveira <carlos157oliveira@gmail.com> | 2023-01-14 18:19:51 -0300 |
| commit | b3b746a6b9797b88260332df5dfafb952bab2faf (patch) | |
| tree | 49a76c57f8273ddff6804d454e4b096e31f1f468 | |
| parent | 70a1571ce4c48f53a1eebc84eddb5c035f88b987 (diff) | |
| download | perlweeklychallenge-club-b3b746a6b9797b88260332df5dfafb952bab2faf.tar.gz perlweeklychallenge-club-b3b746a6b9797b88260332df5dfafb952bab2faf.tar.bz2 perlweeklychallenge-club-b3b746a6b9797b88260332df5dfafb952bab2faf.zip | |
solution to challenge 199
| -rw-r--r-- | challenge-199/carlos-oliveira/raku/ch-1.raku | 18 | ||||
| -rw-r--r-- | challenge-199/carlos-oliveira/raku/ch-2.raku | 23 |
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-199/carlos-oliveira/raku/ch-1.raku b/challenge-199/carlos-oliveira/raku/ch-1.raku new file mode 100644 index 0000000000..1146a7aa1f --- /dev/null +++ b/challenge-199/carlos-oliveira/raku/ch-1.raku @@ -0,0 +1,18 @@ +use Test; + + +sub good-pairs (Int:D @integers --> List:D[Int:D]) { + my $list-end = @integers.end; + my @pairs; + for (0..$list-end X 0..$list-end).grep: { @_[0] < @_[1] } -> ($i, $j) { + if @integers[$i] == @integers[$j] { + @pairs.push: ($i, $j) + } + } + return @pairs; +} + + +is good-pairs(my Int @ = 1,2,3,1,1,3), [(0, 3), (0, 4), (2, 5), (3, 4)], 'Test 1'; +is good-pairs(my Int @ = 1,2,3), [], 'Test 2'; +is good-pairs(my Int @ = 1,1,1,1), [(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)], 'Test 3'; diff --git a/challenge-199/carlos-oliveira/raku/ch-2.raku b/challenge-199/carlos-oliveira/raku/ch-2.raku new file mode 100644 index 0000000000..97a4d664a0 --- /dev/null +++ b/challenge-199/carlos-oliveira/raku/ch-2.raku @@ -0,0 +1,23 @@ +use Test; + + +sub good-triplets (Int:D @integers, Int:D :$x!, Int:D :$y!, Int:D :$z! --> List:D[Int:D]) { + my $end = @integers.end; + my @triplets; + for (0..$end X 0..$end X 0..$end).grep: { @_[0] < @_[1] < @_[2] } -> ($i, $j, $k) { + ($x, $y, $z) + ==> zip ( + abs(@integers[$i] - @integers[$j]), + abs(@integers[$j] - @integers[$k]), + abs(@integers[$i] - @integers[$k]) + ) + ==> map({ @_[0] <= @_[1] }) + ==> my @result; + @triplets.push: (@integers[$i], @integers[$j], @integers[$k]) if all(@result); + } + return @triplets; +} + + +is good-triplets(Array[Int].new(3,0,1,1,9,7), :x(7), :y(2), :z(3)), [<3 0 1>, <3 0 1>, <3 1 1>, <0 1 1>]; +is good-triplets(Array[Int].new(1,1,2,2,3), :x(0), :y(0), :z(1)), []; |
