diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-28 12:03:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-28 12:03:54 +0000 |
| commit | 7b914f6dc785f00871fe60bc020ed4501b8cd616 (patch) | |
| tree | 49934cd14a232a835beb39587698d997c44fec57 | |
| parent | d8ed05c6e43da9455384b77f0882b01bba0dd717 (diff) | |
| parent | fac3be395c92af4f61654ace4870990beb341295 (diff) | |
| download | perlweeklychallenge-club-7b914f6dc785f00871fe60bc020ed4501b8cd616.tar.gz perlweeklychallenge-club-7b914f6dc785f00871fe60bc020ed4501b8cd616.tar.bz2 perlweeklychallenge-club-7b914f6dc785f00871fe60bc020ed4501b8cd616.zip | |
Merge pull request #7174 from Firedrake/rogerbw-challenge-193
Solutions for challenge #193
18 files changed, 762 insertions, 0 deletions
diff --git a/challenge-193/roger-bell-west/javascript/ch-1.js b/challenge-193/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..31d8f7fdcc --- /dev/null +++ b/challenge-193/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,51 @@ +#! /usr/bin/node + +"use strict" + +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 binarystring(n) { + let o = []; + let t = 1 << n; + for (let a = 0; a < t; a++) { + o.push(a.toString(2).padStart(n, "0")); + } + return o; +} + +if (deepEqual(binarystring(2), ["00", "01", "10", "11"])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(binarystring(3), ["000", "001", "010", "011", "100", "101", "110", "111"])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-193/roger-bell-west/javascript/ch-2.js b/challenge-193/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..4d3db17e38 --- /dev/null +++ b/challenge-193/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,38 @@ +#! /usr/bin/node + +"use strict" + +function oddstring(ss) { + let t = ss[0].length - 1; + for (let i = 0; i < t; i++) { + let tab = new Map(); + for (let s of ss) { + let v = s.charCodeAt(i+1) - s.charCodeAt(i); + let ll = []; + if (tab.has(v)) { + ll = tab.get(v); + } + ll.push(s); + tab.set(v,ll); + } + for (let j of tab.values()) { + if (j.length == 1) { + return j[0]; + } + } + } + return ""; +} + +if (oddstring(["adc", "wzy", "abc"]) == "abc") { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddstring(["aaa", "bob", "ccc", "ddd"]) == "bob") { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-193/roger-bell-west/kotlin/ch-1.kt b/challenge-193/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..b859674709 --- /dev/null +++ b/challenge-193/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,22 @@ +fun binarystring(n: Int): List<String> { + var o = ArrayList<String>() + for (a in 0..(1 shl n)-1) { + o.add(Integer.toBinaryString(a).padStart(n, '0')) + } + return o.toList() +} + +fun main() { + if (binarystring(2) == listOf("00", "01", "10", "11")) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (binarystring(3) == listOf("000", "001", "010", "011", "100", "101", "110", "111")) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-193/roger-bell-west/kotlin/ch-2.kt b/challenge-193/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..c7edac6e64 --- /dev/null +++ b/challenge-193/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,34 @@ +import kotlin.math.* + +fun oddstring(ss: List<String>): String { + for (i in 0..ss[0].length-2) { + var tab = mutableMapOf<Int, ArrayList<String>>() + for (s in ss) { + val v = s[i+1].code - s[i].code + var ll = tab.getOrDefault(v, ArrayList<String>()) + ll.add(s) + tab.put(v, ll) + } + for (j in tab.values) { + if (j.size == 1) { + return j[0] + } + } + } + return "" +} + +fun main() { + if (oddstring(listOf("adc", "wzy", "abc")) == "abc") { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (oddstring(listOf("aaa", "bob", "ccc", "ddd")) == "bob") { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-193/roger-bell-west/lua/ch-1.lua b/challenge-193/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..59d2e8ee69 --- /dev/null +++ b/challenge-193/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,59 @@ +#! /usr/bin/lua + +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + + -- Check each key-value pair + -- We have to do this both ways in case we miss some. + -- TODO: Could probably be smarter and not check those we've + -- already checked though! + 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 tobase(val0, len, base) + local s = "" + local val = val0 + for i = 1, len do + s = tostring(val % base) .. s + val = val // base + end + return s +end + +function binarystring(n) + local o = {} + for a = 0,(1 << n)-1 do + table.insert(o,tobase(a, n, 2)) + end + return o +end + +if recursive_compare(binarystring(2), {"00", "01", "10", "11"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(binarystring(3), {"000", "001", "010", "011", "100", "101", "110", "111"}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-193/roger-bell-west/lua/ch-2.lua b/challenge-193/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..dc3c034b72 --- /dev/null +++ b/challenge-193/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,35 @@ +#! /usr/bin/lua + +function oddstring(ss) + for i = 1,#(ss[1])-1 do + local tab = {} + for p,s in ipairs(ss) do + local v = string.byte(s, i+1) - string.byte(s, i) + if tab[v] == nil then + tab[v] = { s } + else + table.insert(tab[v], s) + end + end + for i,j in pairs(tab) do + if #j == 1 then + return j[1] + end + end + end + return "" +end + +if oddstring({"adc", "wzy", "abc"}) == "abc" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddstring({"aaa", "bob", "ccc", "ddd"}) == "bob" then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-193/roger-bell-west/perl/ch-1.pl b/challenge-193/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..8228f91103 --- /dev/null +++ b/challenge-193/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,18 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(binarystring(2), ["00", "01", "10", "11"], 'example 1'); +is_deeply(binarystring(3), ["000", "001", "010", "011", "100", "101", "110", "111"], 'example 2'); + +sub binarystring($n) { + my @o; + foreach my $a (0..(1 << $n)-1) { + push @o,sprintf('%0'.$n.'b', $a); + } + return \@o; +} diff --git a/challenge-193/roger-bell-west/perl/ch-2.pl b/challenge-193/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..855f08d616 --- /dev/null +++ b/challenge-193/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,26 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(oddstring(["adc", "wzy", "abc"]), "abc", 'example 1'); +is(oddstring(["aaa", "bob", "ccc", "ddd"]), "bob", 'example 2'); + +sub oddstring($ss) { + foreach my $i (0..length($ss->[0])-2) { + my %tab; + foreach my $s (@{$ss}) { + my $v = ord(substr($s,$i+1,1)) - ord(substr($s,$i,1)); + push @{$tab{$v}}, $s; + } + foreach my $j (values %tab) { + if (scalar @{$j} == 1) { + return $j->[0]; + } + } + } + return ''; +} diff --git a/challenge-193/roger-bell-west/postscript/ch-1.ps b/challenge-193/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..573bc3af0f --- /dev/null +++ b/challenge-193/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,133 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/safestring { + dup length string cvs +} 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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/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 + + +% end included library code + +/binarystring { + 5 dict begin + /n exch def + /zero n string def + 0 1 n 1 sub { + zero exch 48 put + } for + [ + 0 1 1 n bitshift 1 sub { + /a exch def + /i n 1 sub def + /str zero safestring def + { + a 0 gt { + a 1 and 0 gt { + str i 49 put + } if + /i i 1 sub def + /a a -1 bitshift def + } { + exit + } ifelse + } loop + str + } for + ] + end +} bind def + +(binarystring) test.start +2 binarystring [(00) (01) (10) (11)] deepeq test +3 binarystring [(000) (001) (010) (011) (100) (101) (110) (111)] deepeq test +test.end diff --git a/challenge-193/roger-bell-west/postscript/ch-2.ps b/challenge-193/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..3368c453b5 --- /dev/null +++ b/challenge-193/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,152 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/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 + +/values { % dict -> array of dict values + [ exch + { + exch pop + } forall + ] +} bind def + +/safestring { + dup length string cvs +} 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 + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + + +% end included library code + +/oddstring { + 6 dict begin + /ss exch def + /ex () def + 0 1 ss 0 get length 2 sub { + /i exch def + /tab 0 dict def + ss { + /s exch def + /v s i 1 add get s i get sub def + tab v known { + tab v get + } { + 0 array + } ifelse + s safestring apush.right + tab exch v exch put + } forall + tab values { + dup length 1 eq { + 0 get + /ex exch def + exit + } { + pop + } ifelse + } forall + ex () deepeq not { + exit + } if + } for + ex + end +} bind def + +(oddstring) test.start +[ (adc) (wzy) (abc) ] oddstring (abc) deepeq test +[ (aaa) (bob) (ccc) (ddd) ] oddstring (bob) deepeq test +test.end diff --git a/challenge-193/roger-bell-west/python/ch-1.py b/challenge-193/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..473c8977c9 --- /dev/null +++ b/challenge-193/roger-bell-west/python/ch-1.py @@ -0,0 +1,19 @@ +#! /usr/bin/python3 + +import unittest + +def binarystring(n): + o = [] + for a in range(1 << n): + o.append("{0:0>{w}b}".format(a, w=n)) + return o + +class TestBinarystring(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(binarystring(2), ["00", "01", "10", "11"], 'example 1') + + def test_ex2(self): + self.assertEqual(binarystring(3), ["000", "001", "010", "011", "100", "101", "110", "111"], 'example 2') + +unittest.main() diff --git a/challenge-193/roger-bell-west/python/ch-2.py b/challenge-193/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..e68c819eec --- /dev/null +++ b/challenge-193/roger-bell-west/python/ch-2.py @@ -0,0 +1,24 @@ +#! /usr/bin/python3 + +import unittest +from collections import defaultdict + +def oddstring(ss): + for i in range(len(ss[0])-1): + tab = defaultdict(list) + for s in ss: + tab[ord(s[i+1]) - ord(s[i])].append(s) + for j in tab.values(): + if len(j) == 1: + return j[0] + return "" + +class TestOddstring(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(oddstring(["adc", "wzy", "abc"]), "abc", "example 1") + + def test_ex2(self): + self.assertEqual(oddstring(["aaa", "bob", "ccc", "ddd"]), "bob", "example 2") + +unittest.main() diff --git a/challenge-193/roger-bell-west/raku/ch-1.p6 b/challenge-193/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..3adaa2675e --- /dev/null +++ b/challenge-193/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,16 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is-deeply(binarystring(2), ["00", "01", "10", "11"], 'example 1'); +is-deeply(binarystring(3), ["000", "001", "010", "011", "100", "101", "110", "111"], 'example 2'); + +sub binarystring($n) { + my @o; + for (0..(1 +< $n)-1) -> $a { + @o.push(sprintf('%0' ~ $n ~ 'b', $a)); + } + return @o; +} diff --git a/challenge-193/roger-bell-west/raku/ch-2.p6 b/challenge-193/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..dd692bdf29 --- /dev/null +++ b/challenge-193/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,24 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(oddstring(["adc", "wzy", "abc"]), "abc", 'example 1'); +is(oddstring(["aaa", "bob", "ccc", "ddd"]), "bob", 'example 2'); + +sub oddstring(@ss) { + for (0..chars(@ss[0])-2) -> $i { + my %tab; + for @ss -> $s { + my $v = ord(substr($s,$i+1,1)) - ord(substr($s,$i,1)); + %tab{$v}.push($s); + } + for (%tab.values) -> @j { + if (@j.elems == 1) { + return @j[0]; + } + } + } + return ''; +} diff --git a/challenge-193/roger-bell-west/ruby/ch-1.rb b/challenge-193/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..aeefefa41d --- /dev/null +++ b/challenge-193/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,25 @@ +#! /usr/bin/ruby + +require 'test/unit' + +require 'set' + +def binarystring(n) + fmt = "%0" + n.to_s + "b" + o = [] + 0.upto((1 << n)-1) do |a| + o.push(sprintf(fmt, a)) + end + return o +end + +class TestBinarystring < Test::Unit::TestCase + + def test_ex1 + assert_equal(["00", "01", "10", "11"], binarystring(2)); + end + + def test_ex2 + assert_equal(["000", "001", "010", "011", "100", "101", "110", "111"], binarystring(3)); + end +end diff --git a/challenge-193/roger-bell-west/ruby/ch-2.rb b/challenge-193/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..fefb1c4d50 --- /dev/null +++ b/challenge-193/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,35 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def oddstring(ss) + 0.upto(ss[0].length-2) do |i| + tab = Hash.new + ss.each do |s| + v = s[i+1].ord - s[i].ord + l = [] + if tab.has_key?(v) then + l = tab[v] + end + l.push(s) + tab[v] = l + end + tab.values().each do |j| + if j.length == 1 then + return j[0] + end + end + end + return "" +end + +class TestOddstring < Test::Unit::TestCase + + def test_ex1 + assert_equal("abc", oddstring(["adc", "wzy", "abc"])) + end + + def test_ex2 + assert_equal("bob", oddstring(["aaa", "bob", "ccc", "ddd"])) + end +end diff --git a/challenge-193/roger-bell-west/rust/ch-1.rs b/challenge-193/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..77f3c1d208 --- /dev/null +++ b/challenge-193/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,20 @@ +#! /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!(binarystring(2), vec!["00", "01", "10", "11"]); +} + +#[test] +fn test_ex2() { + assert_eq!(binarystring(3), vec!["000", "001", "010", "011", "100", "101", "110", "111"]); +} + +fn binarystring(n: usize) -> Vec<String> { + let mut o = Vec::new(); + for a in 0..(1<<n) { + o.push(format!("{:0width$b}", a, width = n)); + } + o +} diff --git a/challenge-193/roger-bell-west/rust/ch-2.rs b/challenge-193/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..b43addc64c --- /dev/null +++ b/challenge-193/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,31 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashMap; + +#[test] +fn test_ex1() { + assert_eq!(oddstring(vec!["adc", "wzy", "abc"]), "abc"); +} + +#[test] +fn test_ex2() { + assert_eq!(oddstring(vec!["aaa", "bob", "ccc", "ddd"]), "bob"); +} + +fn oddstring(ss: Vec<&str>) -> String { + for i in 0..=ss[0].len()-2 { + let mut tab: HashMap<usize, Vec<&str>> = HashMap::new(); + for s in &ss { + let v = (s.chars().nth(i+1).unwrap() as usize) - (s.chars().nth(i).unwrap() as usize); + let l = tab.entry(v).or_insert(Vec::new()); + l.push(s); + } + for j in tab.values() { + if j.len() == 1 { + return j[0].to_string(); + } + } + } + "".to_string() +} |
