aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-207/0rir/raku/ch-1.raku109
-rw-r--r--challenge-207/0rir/raku/ch-2.raku64
2 files changed, 173 insertions, 0 deletions
diff --git a/challenge-207/0rir/raku/ch-1.raku b/challenge-207/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..1276bc31ef
--- /dev/null
+++ b/challenge-207/0rir/raku/ch-1.raku
@@ -0,0 +1,109 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ « ␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+207-1: Keyboard Word Submitted by: Mohammad S Anwar
+Given an array of words, print all the words in the given array that can
+be types using alphabet on only one row of the keyboard.
+
+Let us assume the keys are arranged as below:
+Row 1: qwertyuiop
+Row 2: asdfghjkl
+Row 3: zxcvbnm
+
+Example 1
+Input: @words = ("Hello","Alaska","Dad","Peace")
+Output: ("Alaska","Dad")
+Example 2
+Input: @array = ("OMG","Bye")
+Output: ()
+
+=end comment
+
+my @Test =
+ [ ["Hello","Alaska","Dad","Peace"], ["Alaska","Dad"].Set ],
+ [ ["Hello","Alaska","Dad","Peace"], ["Dad","Alaska"].Set ],
+ [ ["Try","Daff","XXX"], ["Try","Daff","XXX"].Set ],
+ [ ["OMG","Bye"], [].Set ],
+ [ ["were","tort","yup","trey","popeye","pout","we",
+ "you","i","rue","quiet","etiquette","tire","retort",
+ "wet", "wry","rye", "trope","yet",],
+ ["were","tort","yup","trey","popeye","pout","we",
+ "you","i","rue","quiet","etiquette","tire","retort",
+ "wet", "wry","rye", "trope","yet",].Set ],
+ [ ["tie","rotor","op","worry","queue","or","try","type","True",
+ "out","iop","write","put","rw","ro","quit","top","tree",
+ "as","has","add","trie"],
+ ["tie","rotor","op","worry","queue","or","try","type","True",
+ "out","iop","write","put","rw","ro","quit","top","tree",
+ "as","has","add","trie"].Set ],
+;
+
+my @T-keyboard-live =
+ ['qwer', 'tyuiop', 'asd', 'fghjkl', 'zxc',' vbnm'], ['a','b'],
+ ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'], ['a',],
+ ['qwertyuiopasdfghjklzxcvbnm'],
+;
+
+my @T-keyboard-die =
+ [,].Array, ['qwe', 'asd', '', 'zxc'],
+ ['qawe', 'asd', 'zxc'], ['qwe', 'asd', 'zxec'],
+ ['qwe', 'axsd', '', 'zxc'],
+;
+plan @Test + @T-keyboard-die + @T-keyboard-die;
+
+my @qwerty-def = 'qwertyuiop', 'asdfghjkl', 'zxcvbnm';
+
+# Keyboard convert and validate.
+sub keyboard-valid( @kb-def where * !~~ Empty --> Array) {
+
+ my @kr = @kb-def.map: *.lc.comb.Set;
+
+ for ^@kr -> $me {
+ for ^@kr -> $other {
+ next if $me == $other;
+ if (@kr[$me] ∩ @kr[$other] ≢ Empty )
+ or (@kr[$me] ≡ Empty.Set)
+ or ( @kr[$other] ≡ Empty.Set)
+ {
+ die 'Bad keyboard: empty line or shared keys.'; # ??? rules
+ } } }
+ @kr;
+}
+
+sub filter-one-row-words( @word, @key-row-def --> Set) {
+ my @one-row-word = gather for @word -> $w {
+ if $w.lc.comb.Set ⊆ @key-row-def.any {
+ take $w;
+ next;
+ } }
+ @one-row-word.Set;
+}
+
+for @T-keyboard-live -> @t {
+ lives-ok { keyboard-valid @t }, 'live';
+}
+for @T-keyboard-die -> @t {
+ dies-ok { keyboard-valid @t }, 'die';
+}
+
+my @kr = keyboard-valid( @qwerty-def);
+
+for @Test -> (@in, $exp) {
+ my $got = filter-one-row-words( @in, @kr);
+ is-deeply $got, $exp, "$got"
+}
+done-testing;
+
+my @array = 'Raku','do', 'does', 'any', "tie","rotor","worry",
+ "queue","or","try","type","True", 'when','error',
+ "out","write","put",'for',"rw","ro","quit","tree",
+ "as","has","add","trie", 'once';
+
+my $set = filter-one-row-words( @array, @kr);
+say "\nInput: @array = (@array.sort())";
+say " Output: ", $set.keys.sort;
+
+exit;
diff --git a/challenge-207/0rir/raku/ch-2.raku b/challenge-207/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..1154f74e67
--- /dev/null
+++ b/challenge-207/0rir/raku/ch-2.raku
@@ -0,0 +1,64 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ « ␤ » ∴
+use v6.e.PREVIEW;
+use Test;
+
+=begin comment
+207-2: H-Index Submitted by: Mohammad S Anwar
+Given an array of integers containing citations a researcher has received
+for each paper, compute the researcher’s H-Index. For more information
+please checkout the wikipedia page.
+
+The H-Index is the largest number h such that h articles have at least h
+citations each. For example, if an author has five publications, with 9, 7,
+6, 2, and 1 citations (ordered from greatest to least), then the author’s
+h-index is 3, because the author has three publications with 3 or more
+citations. However, the author does not have four publications with 4 or
+more citations.
+
+Example 1
+Input: @citations = (10,8,5,4,3)
+Output: 4
+
+Because the 4th publication has 4 citations and the 5th has only 3.
+Example 2
+Input: @citations = (25,8,5,3,3)
+Output: 3
+
+The H-Index is 3 because the fourth paper has only 3 citations.
+
+=end comment
+
+my @Test =
+ [ (), 0],
+ [ (0,), 0],
+ [ (0,0,0), 0],
+ [ (1,), 1],
+ [ (∞,), 1],
+ [ (∞,∞), 2],
+ [ (1,∞,1,∞,∞), 3],
+ [ (1,∞,∞,1), 2],
+ [ (1,∞,1,1), 1],
+ [ (10,), 1],
+ [ (0,0,1), 1],
+ [ (10,8,5,4,3), 4 ],
+ [ (25,8,5,3,3), 3 ],
+;
+plan +@Test;
+
+sub h-index( $l = (10,8,5,4,3) --> Int ) {
+ my $r = $l.sort({$^b <=> $^a})
+ .pairs
+ .List
+ .first( {.key ≤ .value -1}, :end)
+ .key;
+ // $r ?? $r + 1 !! 0;
+}
+
+for @Test -> ($t, $exp) {
+ is h-index($t), $exp, "$exp <- $t.Str()";
+}
+done-testing;
+my @citations = (0,0,1,0,3,2,7,∞,∞,∞);
+say "\nInput: @citations = @citations[]\nOutput: ", h-index(@citations);
+exit;