diff options
| author | rir <rirans@comcast.net> | 2025-08-30 18:46:04 -0400 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2025-08-30 18:46:04 -0400 |
| commit | 6d80a8bebd13b9d9e06494bdfa866906452413d6 (patch) | |
| tree | 603e53871d986e08aa4d3bef026dcd5c66ce8532 | |
| parent | d24aee7bfe3441c6cfc9c1f3f8ad6a5a9b68de61 (diff) | |
| download | perlweeklychallenge-club-6d80a8bebd13b9d9e06494bdfa866906452413d6.tar.gz perlweeklychallenge-club-6d80a8bebd13b9d9e06494bdfa866906452413d6.tar.bz2 perlweeklychallenge-club-6d80a8bebd13b9d9e06494bdfa866906452413d6.zip | |
336
| -rw-r--r-- | challenge-336/0rir/raku/ch-1.raku | 72 | ||||
| -rw-r--r-- | challenge-336/0rir/raku/ch-2.raku | 89 |
2 files changed, 161 insertions, 0 deletions
diff --git a/challenge-336/0rir/raku/ch-1.raku b/challenge-336/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..c500589b2d --- /dev/null +++ b/challenge-336/0rir/raku/ch-1.raku @@ -0,0 +1,72 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ 🐧 +use v6.e.PREVIEW; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +336-1: Equal Group Submitted by: Mohammad Sajid Anwar +You are given an array of integers. + +Write a script to return true if the given array can be divided into one or more groups: each group must be of the same size as the others, with at least two members, and with all members having the same value. + + +Example 1 +Input: @ints = (1,1,2,2,2,2) +Output: true + +Groups: (1,1), (2,2), (2,2) + +Example 2 +Input: @ints = (1,1,1,2,2,2,3,3) +Output: false + +Groups: (1,1,1), (2,2,2), (3,3) + +Example 3 +Input: @ints = (5,5,5,5,5,5,7,7,7,7,7,7) +Output: true + +Groups: (5,5,5,5,5,5), (7,7,7,7,7,7) + +Example 4 +Input: @ints = (1,2,3,4) +Output: false + +Example 5 +Input: @ints = (8,8,9,9,10,10,11,11) +Output: true + +Groups: (8,8), (9,9), (10,10), (11,11) + +=end comment + +my @Test = + (1,1,2,2,2,2), True, + (1,1,1,2,2,2,3,3), False, + (5,5,5,5,5,5,7,7,7,7,7,7), True, + (1,2,3,4), False, + (8,8,9,9,10,10,11,11), True, + + (1,1,2,2), True, + (1,1,1), True, + (1,1), True, + (1,), False, + (), False, +; +plan +@Test ÷ 2; + +multi task( @a where (@a ~~ Empty or @a == 1) ) { False } +multi task( @a -->Bool:D) { + my BagHash[Int] $bh; + ++$bh{$_} for @a; + (my $m = $bh.values.min) > 1 and so $bh.values.all %% $m; +} + +for @Test -> @in, $exp { + is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()"; +} +done-testing; +my @int = 8,8,9,9,10,10,11,11 ; +say "\nInput: @int = ({@int.join( ', ')})\nOutput: &task( @int)" + diff --git a/challenge-336/0rir/raku/ch-2.raku b/challenge-336/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..28c2b49423 --- /dev/null +++ b/challenge-336/0rir/raku/ch-2.raku @@ -0,0 +1,89 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ 🐧 +use v6.e.PREVIEW; +use Test; + +=begin comment +336-2: Final Score Submitted by: Mohammad Sajid Anwar + +You are given an array of scores by a team. + +Write a script to find the total score of the given team. The score can be any integer, +, C or D. The + adds the sum of previous two scores. The score C invalidates the previous score. The score D will double the previous score. + + +Example 1 +Input: @scores = ("5","2","C","D","+") +Output: 30 + +Example 2 +Input: @scores = ("5","-2","4","C","D","9","+","+") +Output: 27 + +Round 1: 5 +Round 2: 5 + (-2) +Round 3: 5 + (-2) + 4 +Round 4: 5 + (-2) (invalidate the previous score 4) +Round 5: 5 + (-2) + (-4) (double the previous score -2) +Round 6: 5 + (-2) + (-4) + 9 +Round 7: 5 + (-2) + (-4) + 9 + 5 (sum of previous two scores) +Round 8: 5 + (-2) + (-4) + 9 + 5 + 14 (sum of previous two scores) + +Total Scores: 27 + +Example 3 +Input: @scores = ("7","D","D","C","+","3") +Output: 45 + +Example 4 +Input: @scores = ("-5","-10","+","D","C","+") +Output: -55 + +Example 5 +Input: @scores = ("3","6","+","D","C","8","+","D","-2","C","+") +Output: 128 + +=end comment + +my @Test = +# # @in $exp + ("5","2","C","D","+"), 30, + ("5","-2","4","C","D","9","+","+"), 27, + ("7","D","D","C","+","3"), 45, + ("-5","-10","+","D","C","+"), -55, + ("3","6","+","D","C","8","+","D","-2","C","+"), 128, + + ('1', '1'), 2, +; +plan +@Test; + +grammar Final-score { + rule TOP { ^ <number> [[ <plus> |<double>|<cancel>] | <number> ]+ $ } + token number { [ '+' | '-'] ? \d+ } + token plus { '+' } + token double { 'D' } + token cancel { 'C' } +} + +class Fs { + has @.score = [0,]; + method TOP( $/) { $/.make: @.score.sum; } + method number( $/) { @.score.push: $/.Int; } + method plus( $/) { @.score.push: (@.score[*-2,*-1]).sum; } + method double( $/) { @.score.push: (@.score[*-1].Int × 2).Int; } + method cancel( $/) { @.score.pop; } +} + +sub task( $a --> Int) { + my $m = Final-score.parse: $a.join( ' '), :actions(Fs.new); + $m.made.Str.Int; +} + +for @Test -> $code, $exp { + my $m = Final-score.parse: $code.join( ' '); + is $m.Str, $code.join(' '), 'parses'; + is task($code), $exp, "$exp <- $code.raku()"; +} +done-testing; +my @score = "-5","-10","+","D","C","+"; + +say qq{\nInput: @score = ["{@score.join: '","' }"]\nOutput: }, task @score; |
