diff options
Diffstat (limited to 'challenge-260')
| -rw-r--r-- | challenge-260/0rir/raku/ch-1.raku | 56 | ||||
| -rw-r--r-- | challenge-260/0rir/raku/ch-2.raku | 71 |
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-260/0rir/raku/ch-1.raku b/challenge-260/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..a1aeab2376 --- /dev/null +++ b/challenge-260/0rir/raku/ch-1.raku @@ -0,0 +1,56 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +260-1: Unique Occurrences Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints. Write a script to return 1 if +the number of occurrences of each value in the given array is unique or 0 +otherwise. + +Example 1 +Input: @ints = (1,2,2,1,1,3) +Output: 1 + +The number 1 occurred 3 times. +The number 2 occurred 2 times. +The number 3 occurred 1 time. + +All occurrences are unique, therefore the output is 1. +Example 2 +Input: @ints = (1,2,3) +Output: 0 +Example 3 +Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +Output: 1 +=end comment + +my @Test = + (1,2,2,1,1,3), True, + (1,2,3), False, + (-2,0,1,-2,1,1,0,1,-2,9), True, + (0…10000).List, False, + (1…100_000, 999_999).List, False, + (1 xx 70, 2 xx 71, 3 xx 72, 4 xx 75, 5 xx 76).flat, True; +; + +plan @Test ÷ 2; + +sub func( $l -->Bool) { + my $k = $l.Bag.values.sort.List; + $k eqv $k.unique.List; +} + +for @Test -> $in, $exp { + is func($in), $exp, ($++).Str; +} + +done-testing; +my @int = (1 xx 70, 2 xx 71, 3 xx 72, 4 xx 75, 5 xx 76).flat; + +say "\nInput: @int = @int[]\nOutput: ", func(@int).Int; + + +exit; + diff --git a/challenge-260/0rir/raku/ch-2.raku b/challenge-260/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..a5195503c4 --- /dev/null +++ b/challenge-260/0rir/raku/ch-2.raku @@ -0,0 +1,71 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +Task 2: Dictionary Rank +Submitted by: Mark Anderson You are given a word, $word. Write a +script to compute the dictionary rank of the given word. + +Example 1 +Input: $word = 'CAT' +Output: 3 + +All possible combinations of the letters: +CAT, CTA, ATC, TCA, ACT, TAC + +Arrange them in alphabetical order: +ACT, ATC, CAT, CTA, TAC, TCA + +CAT is the 3rd in the list. +Therefore the dictionary rank of CAT is 3. +Example 2 +Input: $word = 'GOOGLE' +Output: 88 +Example 3 +Input: $word = 'SECRET' +Output: 255 +=end comment + +=begin comment +I am just using the built-in &permutation, but this can be calculated more +efficiently than generating the permutations. + +'TADE' and 'TEDA', since they start with the 4th ranked letter, will be in +the index range 4! minus 3! … 4! So with bookkeeping the problem can be +partitioned and repartitioned. This simplification doesn't address that +duplicates share identity. +=end comment + +my @Test = + Str, Int, + '', Int, + 'XYZ', 1, + "ooO", 1, + 'Deet', 1, + 'CAT', 3, + 'TADE', 19, + 'Cat', 3, + 'GOOGLE', 88, + 'SECRET', 255, +; +plan @Test ÷ 2; + +# grind it +multi gen( Str:U $a -->Int) { Int } +multi gen( Str:D $a where * eqv '' -->Int) { Int } +multi gen( Str:D $a -->Int) { + ( $a.fc.comb.permutations».join).unique.sort.first( $a.fc, :k) + 1 +} + +for @Test -> $in, $exp { + is gen($in), gen($in), "$exp.raku() <- $in.raku()"; +} + +done-testing; +my $word = 'Supercad'; +say "\nInput: \$word = $word\nOutput: &gen($word)"; + + |
