diff options
19 files changed, 758 insertions, 0 deletions
diff --git a/challenge-207/roger-bell-west/javascript/ch-1.js b/challenge-207/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..645a60fb99 --- /dev/null +++ b/challenge-207/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,54 @@ +#! /usr/bin/node + +"use strict" + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function keyboardword(wl) { + let re = /^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$/i; + let out = [] + for (let w of wl) { + if (w.search(re) > -1) { + out.push(w); + } + } + return out; +} + +if (deepEqual(keyboardword(['Hello', 'Alaska', 'Dad', 'Peace']), ['Alaska', 'Dad'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(keyboardword(['OMG', 'Bye']), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-207/roger-bell-west/javascript/ch-2.js b/challenge-207/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..c18eeb7ed6 --- /dev/null +++ b/challenge-207/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,29 @@ +#! /usr/bin/node + +"use strict" + +function h_index(c0) { + let c = c0.sort(function(a, b) { + return b-a; + }); + let h = 0; + c.forEach((x, i) => { + if (i + 1 <= x) { + h = i + 1; + } + }); + return h; +} + +if (h_index([10, 8, 5, 4, 3]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (h_index([25, 8, 5, 3, 3]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-207/roger-bell-west/kotlin/ch-1.kt b/challenge-207/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..fe6efa68d7 --- /dev/null +++ b/challenge-207/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,27 @@ +fun keyboardword(wl: List<String>): List<String> { + val re = "^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$".toRegex(RegexOption.IGNORE_CASE) + var out = ArrayList<String>() + for (w in wl) { + if (re.containsMatchIn(w)) { + out.add(w) + } + } + return out.toList() +} + +fun main() { + + if (keyboardword(listOf("Hello", "Alaska", "Dad", "Peace")) == listOf("Alaska", "Dad")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (keyboardword(listOf("OMG", "Bye")) == emptyList<String>()) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-207/roger-bell-west/kotlin/ch-2.kt b/challenge-207/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..571dea79e8 --- /dev/null +++ b/challenge-207/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,31 @@ +fun h_index(c0: List<Int>): Int { + var c = ArrayList(c0) + c.sort() + c.reverse() + var h = 0 + for ((i, x) in c.withIndex()) { + if (i + 1 <= x) { + h = i + 1 + } else { + break + } + } + return h +} + +fun main() { + + if (h_index(listOf(10, 8, 5, 4, 3)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (h_index(listOf(25, 8, 5, 3, 3)) == 3) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-207/roger-bell-west/lua/ch-1.lua b/challenge-207/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..375300a8b9 --- /dev/null +++ b/challenge-207/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,48 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +function recursive_compare(t1,t2) + if t1==t2 then return true end + if (type(t1)~="table") then return false end + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + return true +end + +function keyboardword(wl) + local out = {} + for i, w in ipairs(wl) do + local wx = string.lower(w) + if string.match(wx, "^[qwertyuiop]+$") or + string.match(wx, "^[asdfghjkl]+$") or + string.match(wx, "^[zxcvbnm]+$") then + table.insert(out, w) + end + end + return out +end + +if recursive_compare(keyboardword({"Hello", "Alaska", "Dad", "Peace"}), {"Alaska", "Dad"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(keyboardword({"OMG", "Bye"}), {}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-207/roger-bell-west/lua/ch-2.lua b/challenge-207/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..75e9bc7e26 --- /dev/null +++ b/challenge-207/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,30 @@ +#! /usr/bin/lua + +function h_index(c0) + local c = c0 + table.sort(c, function(a, b) return b < a end) + local h = 0 + for i, x in ipairs(c) do + if i <= x then + h = i + else + break + end + end + return h +end + +if h_index({10, 8, 5, 4, 3}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if h_index({25, 8, 5, 3, 3}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-207/roger-bell-west/perl/ch-1.pl b/challenge-207/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..bbadb782c0 --- /dev/null +++ b/challenge-207/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,14 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(keyboardword(['Hello', 'Alaska', 'Dad', 'Peace']), ['Alaska', 'Dad'], 'example 1'); +is_deeply(keyboardword(['OMG', 'Bye']), [], 'example 2'); + +sub keyboardword($wl) { + return [grep /^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$/i, @{$wl}]; +} diff --git a/challenge-207/roger-bell-west/perl/ch-2.pl b/challenge-207/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..0b61dac224 --- /dev/null +++ b/challenge-207/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,23 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(h_index([10, 8, 5, 4, 3]), 4, 'example 1'); +is(h_index([25, 8, 5, 3, 3]), 3, 'example 2'); + +sub h_index($c0) { + my @c = sort {$b <=> $a} @{$c0}; + my $h = 0; + foreach my $i (0..$#c) { + if ($i + 1 <= $c[$i]) { + $h = $i + 1; + } else { + last; + } + } + return $h; +} diff --git a/challenge-207/roger-bell-west/postscript/ch-1.ps b/challenge-207/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..74acb364f8 --- /dev/null +++ b/challenge-207/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,141 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + + +% end included library code + +/keyboardword { + 2 dict begin + /letters 26 dict def + (qwerytyiop) { + letters exch 1 put + } forall + (asdfghjkl) { + letters exch 2 put + } forall + (zxcvbnm) { + letters exch 3 put + } forall + [ exch + { + /row 3 dict def + dup + { + dup dup 65 ge exch 90 le and { + 32 add + } if + dup letters exch known { + letters exch get row exch true put + } { + pop + } ifelse + } forall + row keys length 1 ne { + pop + } if + } forall + ] +} bind def + +(keyboardword) test.start +[(Hello) (Alaska) (Dad) (Peace)] keyboardword [(Alaska) (Dad)] deepeq test +[(OMG) (Bye)] keyboardword [] deepeq test +test.end diff --git a/challenge-207/roger-bell-west/postscript/ch-2.ps b/challenge-207/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..05898f50a8 --- /dev/null +++ b/challenge-207/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,150 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + end +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} bind def + +/quicksort { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + + +% end included library code + +/h_index { + 2 dict begin + 0 exch + quicksort reverse enumerate.array { + aload pop + /x exch def + 1 add /i1 exch def + i1 x le { + pop i1 + } { + exit + } ifelse + } forall +} bind def + +(h_index) test.start +[10 8 5 4 3] h_index 4 eq test +[25 8 5 3 3] h_index 3 eq test +test.end diff --git a/challenge-207/roger-bell-west/python/ch-1.py b/challenge-207/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..2742cbdd6f --- /dev/null +++ b/challenge-207/roger-bell-west/python/ch-1.py @@ -0,0 +1,19 @@ +#! /usr/bin/python3 + +import re + +def keyboardword(wl): + rl = re.compile(r"^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$", re.IGNORECASE) + return [w for w in wl if re.match(rl, w)] + +import unittest + +class TestKeyboardword(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(keyboardword(["Hello", "Alaska", "Dad", "Peace"]), ["Alaska", "Dad"], 'example 1') + + def test_ex2(self): + self.assertEqual(keyboardword(["OMG", "Bye"]), [], 'example 2') + +unittest.main() diff --git a/challenge-207/roger-bell-west/python/ch-2.py b/challenge-207/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..4b7d778b75 --- /dev/null +++ b/challenge-207/roger-bell-west/python/ch-2.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def h_index(c0): + c = c0 + c.sort() + c.reverse() + h = 0 + for i, x in enumerate(c): + if i + 1 <= x: + h = i + 1 + else: + break + return h + +import unittest + +class TestH_index(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(h_index([10, 8, 5, 4, 3]), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(h_index([25, 8, 5, 3, 3]), 3, 'example 2') + +unittest.main() diff --git a/challenge-207/roger-bell-west/raku/ch-1.p6 b/challenge-207/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..002579c2d1 --- /dev/null +++ b/challenge-207/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,12 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(keyboardword(['Hello', 'Alaska', 'Dad', 'Peace']), ['Alaska', 'Dad'], 'example 1'); +is-deeply(keyboardword(['OMG', 'Bye']), [], 'example 2'); + +sub keyboardword(@wl) { + return [grep rx:i/^(<[qwertyuiop]>+|<[asdfghjkl]>+|<[zxcvbnm]>+)$/, @wl]; +} diff --git a/challenge-207/roger-bell-west/raku/ch-2.p6 b/challenge-207/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..12071cc1b2 --- /dev/null +++ b/challenge-207/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(h_index([10, 8, 5, 4, 3]), 4, 'example 1'); +is(h_index([25, 8, 5, 3, 3]), 3, 'example 2'); + +sub h_index(@c0) { + my @c = @c0.sort.reverse; + my $h = 0; + for (0..@c.end) -> $i { + if ($i + 1 <= @c[$i]) { + $h = $i + 1; + } else { + last; + } + } + return $h; +} diff --git a/challenge-207/roger-bell-west/ruby/ch-1.rb b/challenge-207/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..5f60c96ddf --- /dev/null +++ b/challenge-207/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,20 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def keyboardword(wl) + re = Regexp.new("(?i)^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$", Regexp::IGNORECASE) + return wl.find_all{|w| re.match(w)} +end + +class TestKeyboardword < Test::Unit::TestCase + + def test_ex1 + assert_equal(['Alaska', 'Dad'], keyboardword(['Hello', 'Alaska', 'Dad', 'Peace'])) + end + + def test_ex2 + assert_equal([], keyboardword(['OMG', 'Bye'])) + end + +end diff --git a/challenge-207/roger-bell-west/ruby/ch-2.rb b/challenge-207/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..38203a6cc8 --- /dev/null +++ b/challenge-207/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,28 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def h_index(c0) + c = c0.sort.reverse + h = 0 + c.each_with_index do |x, i| + if i + 1 <= x then + h = i + 1 + else + break + end + end + return h +end + +class TestH_index < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, h_index([10, 8, 5, 4, 3])) + end + + def test_ex2 + assert_equal(3, h_index([25, 8, 5, 3, 3])) + end + +end diff --git a/challenge-207/roger-bell-west/rust/ch-1.rs b/challenge-207/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..f81c457e53 --- /dev/null +++ b/challenge-207/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,26 @@ +// [dependencies] +// regex = "1" + +use regex::Regex; + +#[test] +fn test_ex1() { + assert_eq!( + keyboardword(vec!["Hello", "Alaska", "Dad", "Peace"]), + vec!["Alaska", "Dad"] + ); +} + +#[test] +fn test_ex2() { + assert_eq!(keyboardword(vec!["OMG", "Bye"]), Vec::<String>::new()); +} + +fn keyboardword(wl: Vec<&str>) -> Vec<String> { + let re = + Regex::new(r"(?i)^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$").unwrap(); + wl.into_iter() + .filter(|w| re.is_match(w)) + .map(|w| w.to_string()) + .collect::<Vec<String>>() +} diff --git a/challenge-207/roger-bell-west/rust/ch-2.rs b/challenge-207/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..960dd4cb3f --- /dev/null +++ b/challenge-207/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,27 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(h_index(vec![10, 8, 5, 4, 3]), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(h_index(vec![25, 8, 5, 3, 3]), 3); +} + +fn h_index(c0: Vec<usize>) -> usize { + let mut c = c0.clone(); + c.sort(); + c.reverse(); + let mut h = 0; + for (i, x) in c.iter().enumerate() { + if i + 1 <= *x { + h = i + 1; + } else { + break; + } + } + h +} diff --git a/challenge-207/roger-bell-west/tests.yaml b/challenge-207/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..089ba62e35 --- /dev/null +++ b/challenge-207/roger-bell-west/tests.yaml @@ -0,0 +1,33 @@ +--- +ch-1: + - function: keyboardword + arguments: + - Hello + - Alaska + - Dad + - Peace + result: + - Alaska + - Dad + - function: keyboardword + arguments: + - OMG + - Bye + result: [] +ch-2: + - function: h_index + arguments: + - 10 + - 8 + - 5 + - 4 + - 3 + result: 4 + - function: h_index + arguments: + - 25 + - 8 + - 5 + - 3 + - 3 + result: 3 |
