diff options
Diffstat (limited to 'challenge-207')
62 files changed, 1851 insertions, 90 deletions
diff --git a/challenge-207/bruce-gray/raku/ch-1.raku b/challenge-207/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..50e4a9acb8 --- /dev/null +++ b/challenge-207/bruce-gray/raku/ch-1.raku @@ -0,0 +1,37 @@ +# I usually would have phrased this as a Bool sub, +# but since the whole sub is just a match against a constant regex, +# and a Regex is a Matcher, I skipped that division. +# +# Also, I like this solution, because it easily allows +# for alternate keyboards that duplicate letters! +# +# FYI, unlike Perl, // in scalar assignment +# produces the Regex object itself, like Perl's `qr{}`. +# +# I am using `:i` to make the regex case-insensitive. +# I might have added `:ignoremark`, but that crashes on +# the ancient Rakudo on this laptop. +# +sub task1 ( @s --> Seq ) { + constant $all_in_one_row = / + :i + ^ [ <[qwertyuiop]>+ + | <[asdfghjkl]>+ + | <[zxcvbnm]>+ + ] + $ + /; + + return @s.grep: $all_in_one_row; +} + + +constant @tests = + ( <Hello Alaska Dad Peace> , <Alaska Dad> ), + ( <OMG Bye> , ().Seq ), +; +use Test; +plan +@tests; +for @tests -> ( $in, $expected ) { + is-deeply task1($in), $expected; +} diff --git a/challenge-207/bruce-gray/raku/ch-2.raku b/challenge-207/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..106e3cbb73 --- /dev/null +++ b/challenge-207/bruce-gray/raku/ch-2.raku @@ -0,0 +1,20 @@ +sub task2 ( @ns --> UInt ) { + # Most concise and clear + return +@ns.sort(-*).pairs.grep: { .value > .key }; + + # Faster, but less clear + # return @ns.sort(-*).pairs.first({ .value <= .key }).?key + # // @ns.elems; +} + + +constant @tests = + ( ( 10, 8, 5, 4, 3 ), 4 ), + ( ( 25, 8, 5, 3, 3 ), 3 ), + ( ( 2, 2, 2, 2, 2 ), 2 ), +; +use Test; +plan +@tests; +for @tests -> ( $in, $expected ) { + is task2($in), $expected; +} diff --git a/challenge-207/carlos-oliveira/raku/ch-1.raku b/challenge-207/carlos-oliveira/raku/ch-1.raku new file mode 100644 index 0000000000..1632a777c2 --- /dev/null +++ b/challenge-207/carlos-oliveira/raku/ch-1.raku @@ -0,0 +1,22 @@ +use Test; + +sub extract-keyboard-words (@words where all($_) ~~ Str:D) { + my @keyboard-rows = ( + Set.new(<q w e r t y u i o p>), + Set.new(<a s d f g h j k l>), + Set.new(<z x c v b n m>) + ); + + gather { + for @words -> $raw-word { + my $word = lc $raw-word; + for @keyboard-rows -> %keyboard-row { + take $raw-word and next if all(%keyboard-row{$word.comb('')}); + } + } + } +} + +is extract-keyboard-words(["Hello", "Alaska", "Dad", "Peace"]), ["Alaska", "Dad"]; +is extract-keyboard-words(["OMG", "Bye"]), []; +is extract-keyboard-words(["ABC"]), []; diff --git a/challenge-207/carlos-oliveira/raku/ch-2.raku b/challenge-207/carlos-oliveira/raku/ch-2.raku new file mode 100644 index 0000000000..a79b57c83c --- /dev/null +++ b/challenge-207/carlos-oliveira/raku/ch-2.raku @@ -0,0 +1,14 @@ +use Test; + +sub h-index (@citations where all($_) ~~ Int:D) { + my $h-index = 0; + my $some-invalid = @citations.sort.reverse.first: { $_ < ++$h-index }; + return $some-invalid ?? $h-index - 1 !! $h-index; +} + +is h-index([10, 8, 5, 4, 3]), 4; +is h-index([25, 8, 5, 3, 3]), 3; +is h-index([7, 7, 7]), 3; +is h-index([2, 1]), 1; +is h-index([1]), 1; +is h-index([]), 0; diff --git a/challenge-207/deadmarshal/cpp/ch-1.cpp b/challenge-207/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..e778f5824b --- /dev/null +++ b/challenge-207/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,33 @@ +#include<iostream> +#include<string> +#include<vector> +#include<algorithm> + +bool all_match(const std::string &s1, const std::string &s2) +{ + for(auto &c : s1) if(s2.find(c) == std::string::npos) return false; + return true; +} + +void keyboard_word(std::vector<std::string> &vec) +{ + const std::vector<std::string> + qwerty{"qwertyuiop","asdfghjkl","zxcvbnm"}; + for(auto &s : vec) + std::transform(s.begin(),s.end(),s.begin(), + [](unsigned char c){return std::tolower(c);}); + for(const auto &q : qwerty) + for(const auto &s : vec) + if(all_match(s,q)) std::cout << s << ' '; + std::cout << '\n'; +} + +int main() +{ + std::vector<std::string> vec1{"Hello","Alaska","Dad","Peace"}, + vec2{"OMG","Bye"}; + keyboard_word(vec1); + keyboard_word(vec2); + return 0; +} + diff --git a/challenge-207/deadmarshal/cpp/ch-2.cpp b/challenge-207/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..6af3160443 --- /dev/null +++ b/challenge-207/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,22 @@ +#include<iostream> +#include<vector> + +int h_index(const std::vector<int> &vec) +{ + int ret{}; + for(int i = 0; i < vec.size(); ++i) + if(i >= vec[i]) + { + ret = i; + break; + } + return ret; +} + +int main() +{ + std::cout << h_index({10,8,5,4,3}) << '\n'; + std::cout << h_index({25,8,5,3,3}) << '\n'; + return 0; +} + diff --git a/challenge-207/deadmarshal/d/ch1.d b/challenge-207/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..af05e1ba16 --- /dev/null +++ b/challenge-207/deadmarshal/d/ch1.d @@ -0,0 +1,28 @@ +import std.stdio:writeln,write; +import std.uni:toLower; +import std.algorithm:map,all,canFind; +import std.array:array; + +bool all_match(ref string s1, ref string s2) +{ + foreach(ref c;s1) if(!canFind(s2,c)) return false; + return true; +} + +void keyboard_word(ref string[] words) +{ + string[] qwerty = ["qwertyuiop","asdfghjkl","zxcvbnm"]; + words = words.map!(toLower).array; + foreach(ref q;qwerty) + foreach(ref w;words) if(all_match(w,q)) write(w,' '); + writeln; +} + +void main() +{ + string[] words1 = ["Hello","Alaska","Dad","Peace"]; + string[] words2 = ["OMG","Bye"]; + keyboard_word(words1); + keyboard_word(words2); +} + diff --git a/challenge-207/deadmarshal/d/ch2.d b/challenge-207/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..3dc2974bad --- /dev/null +++ b/challenge-207/deadmarshal/d/ch2.d @@ -0,0 +1,20 @@ +import std.stdio:writeln; + +int h_index(int[] arr) +{ + int ret = 0; + for(int i = 0; i < arr.length; ++i) + if(i >= arr[i]) + { + ret = i; + break; + } + return ret; +} + +void main() +{ + writeln(h_index([10,8,5,4,3])); + writeln(h_index([25,8,5,3,3])); +} + diff --git a/challenge-207/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-207/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..04ba90c5ef --- /dev/null +++ b/challenge-207/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,47 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT IO,Text,ASCII; + +VAR + A1:ARRAY[0..3] OF TEXT := ARRAY OF TEXT{"Hello","Alaska","Dad","Peace"}; + A2:ARRAY[0..1] OF TEXT := ARRAY OF TEXT{"OMG","Bye"}; + +PROCEDURE Lower(text:TEXT):TEXT = + VAR + Res:TEXT := ""; + BEGIN + FOR I := 0 TO Text.Length(text)-1 DO + Res := Text.Cat(Res, Text.FromChar(ASCII.Lower[Text.GetChar(text, I)])); + END; + RETURN Res; + END Lower; + +PROCEDURE AllMatch(VAR text:TEXT;VAR S:SET OF CHAR):BOOLEAN = + BEGIN + FOR I := 0 TO Text.Length(text)-1 DO + IF NOT Text.GetChar(text,I) IN S THEN RETURN FALSE END + END; + RETURN TRUE; + END AllMatch; + +PROCEDURE KeyboardWord(VAR A:ARRAY OF TEXT) = +VAR + S1:SET OF CHAR := SET OF CHAR{'q','w','e','r','t','y','u','i','o','p'}; + S2:SET OF CHAR := SET OF CHAR{'a','s','d','f','g','h','j','k','l'}; + S3:SET OF CHAR := SET OF CHAR{'z','x','c','v','b','n','m'}; +BEGIN + FOR I := FIRST(A) TO LAST(A) DO A[I] := Lower(A[I]) END; + FOR I := FIRST(A) TO LAST(A) DO + IF AllMatch(A[I],S1) OR AllMatch(A[I],S2) OR AllMatch(A[I],S3) THEN + IO.Put(A[I]); + IO.PutChar(' ') + END + END; + IO.Put("\n"); +END KeyboardWord; + +BEGIN + KeyboardWord(A1); + KeyboardWord(A2); +END Ch1. + diff --git a/challenge-207/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-207/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..0ee72d695b --- /dev/null +++ b/challenge-207/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch1") +program("ch1") + diff --git a/challenge-207/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-207/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..62f84618e9 --- /dev/null +++ b/challenge-207/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,20 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT IO; + +VAR + A1:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{10,8,5,4,3}; + A2:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{25,8,5,3,3}; + +PROCEDURE HIndex(VAR A:ARRAY OF INTEGER):INTEGER = + BEGIN + FOR I := FIRST(A) TO LAST(A) DO + IF I >= A[I] THEN RETURN I END + END; + END HIndex; + +BEGIN + IO.PutInt(HIndex(A1)); IO.Put("\n"); + IO.PutInt(HIndex(A2)); IO.Put("\n"); +END Ch2. + diff --git a/challenge-207/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-207/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..5c32bbc4bb --- /dev/null +++ b/challenge-207/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch2") +program("ch2") + diff --git a/challenge-207/deadmarshal/nim/ch1.nim b/challenge-207/deadmarshal/nim/ch1.nim new file mode 100644 index 0000000000..6ba9eaf314 --- /dev/null +++ b/challenge-207/deadmarshal/nim/ch1.nim @@ -0,0 +1,18 @@ +import std/[strutils] + +proc keyboardWord(strseq:var seq[string]):seq[string] = + var + s1:set[char] = {'q','w','e','r','t','y','u','i','o','p'} + s2:set[char] = {'a','s','d','f','g','h','j','k','l'} + s3:set[char] = {'z','x','c','v','b','n','m'} + for s in strseq.mitems: s = toLowerAscii(s) + for myset in [s1,s2,s3]: + for s in strseq: + if allCharsInSet(s,myset): result.add(s) +var + s1:seq[string] = @["Hello","Alaska","Dad","Peace"] + s2:seq[string] = @["OMG","Bye"] + +echo keyboardWord(s1) +echo keyboardWord(s2) + diff --git a/challenge-207/deadmarshal/nim/ch2.nim b/challenge-207/deadmarshal/nim/ch2.nim new file mode 100644 index 0000000000..b570160249 --- /dev/null +++ b/challenge-207/deadmarshal/nim/ch2.nim @@ -0,0 +1,7 @@ +proc hIndex(s:seq[int]):int = + for i in 0..<s.len: + if i >= s[i]: return i + +echo hIndex(@[10,8,5,4,3]) +echo hIndex(@[25,8,5,3,3]) + diff --git a/challenge-207/deadmarshal/oberon/Ch1.Mod b/challenge-207/deadmarshal/oberon/Ch1.Mod new file mode 100644 index 0000000000..1928ee8074 --- /dev/null +++ b/challenge-207/deadmarshal/oberon/Ch1.Mod @@ -0,0 +1,54 @@ +MODULE Ch1; + + IMPORT Out,Strings,ethStrings; + + TYPE + PArr = POINTER TO ARRAY OF ARRAY OF CHAR; + + VAR + A1,A2:PArr; + + PROCEDURE Init; + BEGIN + NEW(A1,4,7); NEW(A2,2,4); + COPY("Hello",A1[0]); COPY("Alaska",A1[1]); COPY("Dad",A1[2]); + COPY("Peace",A1[3]); + COPY("OMG",A2[0]); COPY("Bye",A2[1]); + END Init; + + PROCEDURE AllMatch(Haystack,Needle:ARRAY OF CHAR):BOOLEAN; + VAR + i:LONGINT; + s:ARRAY 2 OF CHAR; + BEGIN + s[1] := 0X; + FOR i := 0 TO Strings.Length(Needle) DO + s[0] := Needle[i]; + IF Strings.Pos(s,Haystack,0) = -1 THEN RETURN FALSE END + END; + RETURN TRUE + END AllMatch; + + PROCEDURE KeyboardWord(VAR A:PArr); + VAR + i,j:LONGINT; + qwerty:ARRAY 3,10 OF CHAR; + BEGIN + COPY("qwertyuiop",qwerty[0]); + COPY("asdfghjkl",qwerty[1]); + COPY("zxcvbnm",qwerty[2]); + FOR i := 0 TO LEN(A^)-1 DO ethStrings.Lower(A[i],A[i]) END; + FOR i := 0 TO LEN(qwerty)-1 DO + FOR j := 0 TO LEN(A^)-1 DO + IF AllMatch(qwerty[i],A[j]) THEN Out.String(A[j]); Out.Char(' ') END + END + END; + Out.Ln + END KeyboardWord; + +BEGIN + Init; + KeyboardWord(A1); + KeyboardWord(A2); +END Ch1. + diff --git a/challenge-207/deadmarshal/oberon/Ch2.Mod b/challenge-207/deadmarshal/oberon/Ch2.Mod new file mode 100644 index 0000000000..91ac5e1200 --- /dev/null +++ b/challenge-207/deadmarshal/oberon/Ch2.Mod @@ -0,0 +1,28 @@ +MODULE Ch2; + + IMPORT Out; + + VAR + A1,A2:ARRAY 5 OF INTEGER; + + PROCEDURE Init; + BEGIN + A1[0] := 10; A1[1] := 8; A1[2] := 5; A1[3] := 4; A1[4] := 3; + A2[0] := 25; A2[1] := 8; A2[2] := 5; A2[3] := 3; A2[4] := 3; + END Init; + + PROCEDURE HIndex(VAR A:ARRAY OF INTEGER):INTEGER; + VAR + i:LONGINT; + BEGIN + FOR i := 0 TO LEN(A)-1 DO + IF i >= A[i] THEN RETURN SHORT(i) END + END; + END HIndex; + +BEGIN + Init; + Out.Int(HIndex(A1),0); Out.Ln; + Out.Int(HIndex(A2),0); Out.Ln; +END Ch2. + diff --git a/challenge-207/deadmarshal/pascal/ch1.pas b/challenge-207/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..843d231a23 --- /dev/null +++ b/challenge-207/deadmarshal/pascal/ch1.pas @@ -0,0 +1,46 @@ +program Ch1; + +{$mode objfpc} + +uses + SysUtils,StrUtils,Types,GVector; + +type + TVec = specialize TVector<AnsiString>; + +var + I:Integer; + A1:TStringDynArray = ('Hello','Alaska','Dad','Peace'); + A2:TStringDynArray = ('ONG','Bye'); + Res1,Res2:TVec; + +function AllMatch(Needle,Haystack:AnsiString):Boolean; +var + I:Integer; +begin + for I := Low(Needle) to High(Needle) do + if FindPart(Needle[I],Haystack) = 0 then Exit(False); + Exit(True); +end; + +function KeyboardWord(var Arr:TStringDynArray):TVec; +var + I,J:Integer; + Qwerty:TStringDynArray = ('qwertyuiop','asdfghjkl','zxcvbnm'); +begin + Result := TVec.Create; + for I := Low(Arr) to High(Arr) do Arr[I] := LowerCase(Arr[I]); + for I := Low(Qwerty) to High(Qwerty) do + for J := Low(Arr) to High(Arr) do + if AllMatch(Arr[J],Qwerty[I]) then Result.PushBack(Arr[J]); +end; + +begin + Res1 := KeyboardWord(A1); + Res2 := KeyboardWord(A2); + for I := 0 to Pred(Res1.Size) do Write(Res1[I],' '); WriteLn; + for I := 0 to Pred(Res2.Size) do Write(Res2[I],' '); WriteLn; + FreeAndNil(Res1); + FreeAndNil(Res2); +end. + diff --git a/challenge-207/deadmarshal/pascal/ch2.pas b/challenge-207/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..8f54f5ea5a --- /dev/null +++ b/challenge-207/deadmarshal/pascal/ch2.pas @@ -0,0 +1,20 @@ +program Ch2; + +{$mode objfpc} + +uses + SysUtils,Types; + +function HIndex(A:TIntegerDynArray):Integer; +var + I:Integer; +begin + for I := Low(A) to High(A) do + if(I >= A[I]) then Exit(I); +end; + +begin + WriteLn(HIndex([10,8,5,4,3])); + WriteLn(HIndex([25,8,5,3,3])); +end. + diff --git a/challenge-207/deadmarshal/perl/ch-1.pl b/challenge-207/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..d0ee6bae07 --- /dev/null +++ b/challenge-207/deadmarshal/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(all); +use Set::Scalar; + +sub keyboard_word{ + my ($arr) = @_; + my ($s1,$s2,$s3) = (Set::Scalar->new(split '',"qwertyuiop"), + Set::Scalar->new(split '',"asdfghjkl"), + Set::Scalar->new(split '',"zxcvbnm")); + my @ret; + @$arr = map {lc} @$arr; + foreach my $str(@$arr){ + foreach my $set($s1,$s2,$s3){ + if(all {$set->has($_)} split '',$str){push @ret,$str} + } + } + @ret; +} + +printf "(%s)\n", join ' ', + keyboard_word(["Hello","Alaska","Dad","Peace"]); +printf "(%s)\n", join ' ', keyboard_word(["OMG","Bye"]); + diff --git a/challenge-207/deadmarshal/perl/ch-2.pl b/challenge-207/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..5308581fb1 --- /dev/null +++ b/challenge-207/deadmarshal/perl/ch-2.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub h_index{ + my ($arr) = @_; + map {return $_ if $_ >= $arr->[$_]} 0..$#$arr; +} + +printf "%d\n", h_index([10,8,5,4,3]); +printf "%d\n", h_index([25,8,5,3,3]); + diff --git a/challenge-207/deadmarshal/raku/ch-1.raku b/challenge-207/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..e6f911b917 --- /dev/null +++ b/challenge-207/deadmarshal/raku/ch-1.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku + +sub keyboard-word(@arr) +{ + my $s1 = set <q w e r t y y u i o p>; + my $s2 = set <a s d f g h j k l>; + my $s3 = set <z x c v b n m>; + my @ret; + @arr = @arr>>.lc; + for @arr -> $word { + for $s1,$s2,$s3 -> $set { + if ($word.comb ⊆ $set) { + @ret.push($word); + } + } + } + @ret; +} + +say keyboard-word(["Hello", "Alaska", "Dad", "Peace"]); |
