diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-04-22 16:47:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-22 16:47:16 +0100 |
| commit | 4145b59216c074e1caeca19ec1d06c4240d389bc (patch) | |
| tree | 5ee072fe2755c2e63b10b7d7a371636c50ee7df7 | |
| parent | 1ff9b2330948b35b5d3701ebfdefc438a35fe2b4 (diff) | |
| parent | 56d236e95770f6ea57c2acc7d766f2a15a904639 (diff) | |
| download | perlweeklychallenge-club-4145b59216c074e1caeca19ec1d06c4240d389bc.tar.gz perlweeklychallenge-club-4145b59216c074e1caeca19ec1d06c4240d389bc.tar.bz2 perlweeklychallenge-club-4145b59216c074e1caeca19ec1d06c4240d389bc.zip | |
Merge pull request #11920 from Firedrake/rogerbw-challenge-318
RogerBW solutions for challenge no. 318
23 files changed, 1279 insertions, 0 deletions
diff --git a/challenge-318/roger-bell-west/crystal/ch-1.cr b/challenge-318/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..d89c95f743 --- /dev/null +++ b/challenge-318/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,38 @@ +#! /usr/bin/crystal + +def groupposition(a) + mx = Array({Char, Int32}).new + lc = "z" + a.chars.each_with_index do |c, i| + if i == 0 + lc = (c.ord + 1).chr + end + if lc == c + mc = mx.size + mx[mc - 1] = {mx[mc - 1][0], mx[mc - 1][1] + 1} + else + mx.push({c, 1}) + lc = c + end + end + out = Array(String).new + mx.each do |ms| + if ms[1] >= 3 + out.push(ms[0].to_s * ms[1]) + end + end + out +end + +require "spec" +describe "groupposition" do + it "test_ex1" do + groupposition("abccccd").should eq ["cccc"] + end + it "test_ex2" do + groupposition("aaabcddddeefff").should eq ["aaa", "dddd", "fff"] + end + it "test_ex3" do + groupposition("abcdd").should eq [] of String + end +end diff --git a/challenge-318/roger-bell-west/crystal/ch-2.cr b/challenge-318/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..6905b9bdf4 --- /dev/null +++ b/challenge-318/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,32 @@ +#! /usr/bin/crystal + +def reverseequals(a, b) + if a == b + return true + end + 0.upto(a.size - 2) do |i| + (i + 1).upto(a.size - 1) do |j| + c = a.clone + i.upto(j) do |x| + c[x] = a[j - (x - i)] + end + if c == b + return true + end + end + end + false +end + +require "spec" +describe "reverseequals" do + it "test_ex1" do + reverseequals([3, 2, 1, 4], [1, 2, 3, 4]).should eq true + end + it "test_ex2" do + reverseequals([1, 3, 4], [4, 1, 3]).should eq false + end + it "test_ex3" do + reverseequals([2], [2]).should eq true + end +end diff --git a/challenge-318/roger-bell-west/javascript/ch-1.js b/challenge-318/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..44ed2e0e71 --- /dev/null +++ b/challenge-318/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,73 @@ +#! /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 groupposition(a) { + let mx = []; + let lc = "z"; + a.split("").forEach((c, i) => { + if (i == 0) { + lc = String.fromCharCode(c.charCodeAt(0) + 1); + } + if (lc == c) { + const mc = mx.length; + mx[mc - 1][1] += 1; + } else { + mx.push([c, 1]); + lc = c; + } + }); + let out = []; + for (let ms of mx) { + if (ms[1] >= 3) { + out.push(ms[0].repeat(ms[1])); + } + } + return out; +} + +if (deepEqual(groupposition('abccccd'), ['cccc'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(groupposition('aaabcddddeefff'), ['aaa', 'dddd', 'fff'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(groupposition('abcdd'), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-318/roger-bell-west/javascript/ch-2.js b/challenge-318/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..1f624480a6 --- /dev/null +++ b/challenge-318/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,52 @@ +#! /usr/bin/node + +"use strict" + +function arrcmp(a, b) { + if (a.length != b.length) { + return false; + } + for (let i = 0; i < a.length; i++) { + if (a[i] != b[i]) { + return false; + } + } + return true; +} + +function reverseequals(a, b) { + if (arrcmp(a, b)) { + return true; + } + for (let i = 0; i < a.length - 1; i++) { + for (let j = i + 1; j < a.length; j++) { + let c = [...a]; + for (let x = i; x <= j; x++) { + c[x] = a[j - (x - i)]; + } + if (arrcmp(c, b)) { + return true; + } + } + } + return false; +} + +if (reverseequals([3, 2, 1, 4], [1, 2, 3, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!reverseequals([1, 3, 4], [4, 1, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (reverseequals([2], [2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-318/roger-bell-west/kotlin/ch-1.kt b/challenge-318/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..d7d1d8eaa1 --- /dev/null +++ b/challenge-318/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,46 @@ +fun groupposition(a: String): List<String> { + var mx = ArrayList<Pair<Char, Int>>() + var lc = 'z' + a.toCharArray().forEachIndexed {i, c -> + if (i == 0) { + lc = (c.code + 1).toChar() + } + if (lc == c) { + val mc = mx.size + mx[mc - 1] = Pair(mx[mc - 1].first, mx[mc - 1].second + 1) + } else { + mx += Pair(c, 1) + lc = c + } + } + var out = ArrayList<String>() + for (ms in mx) { + if (ms.second >= 3) { + out.add(ms.first.toString().repeat(ms.second)) + } + } + return out.toList() +} + +fun main() { + + if (groupposition("abccccd") == listOf("cccc")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (groupposition("aaabcddddeefff") == listOf("aaa", "dddd", "fff")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (groupposition("abcdd") == emptyList<String>()) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-318/roger-bell-west/kotlin/ch-2.kt b/challenge-318/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..a34b23b880 --- /dev/null +++ b/challenge-318/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,40 @@ +fun reverseequals(a: List<Int>, b: List<Int>): Boolean { + if (a == b) { + return true + } + for (i in 0 .. a.size - 3) { + for (j in i + 1 .. a.size - 2) { + var c = ArrayList(a) + for (x in i .. j) { + c[x] = a[j - (x - i)] + } + if (c == b) { + return true + } + } + } + return false +} + +fun main() { + + if (reverseequals(listOf(3, 2, 1, 4), listOf(1, 2, 3, 4))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reverseequals(listOf(1, 3, 4), listOf(4, 1, 3))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reverseequals(listOf(2), listOf(2))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-318/roger-bell-west/lua/ch-1.lua b/challenge-318/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..f4ec9432b9 --- /dev/null +++ b/challenge-318/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,86 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +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 + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for _, k in ipairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function groupposition(a) + local mx = {} + local lc = "z" + for i, c in ipairs(split(a)) do + if i == 1 then + lc = string.char(string.byte(c) + 1) + end + if lc == c then + mx[#mx][2] = mx[#mx][2] + 1 + else + table.insert(mx, {c, 1}) + lc = c + end + end + local out = {} + for _, ms in ipairs(mx) do + if ms[2] >= 3 then + table.insert(out, string.rep(ms[1], ms[2])) + end + end + return out +end + +if recursive_compare(groupposition("abccccd"), {"cccc"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(groupposition("aaabcddddeefff"), {"aaa", "dddd", "fff"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(groupposition("abcdd"), {}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-318/roger-bell-west/lua/ch-2.lua b/challenge-318/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..54d13de17a --- /dev/null +++ b/challenge-318/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,94 @@ +#! /usr/bin/lua + +function dump(o) + if type(o) == 'table' then + local s = '{ ' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. '['..k..'] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end +end + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +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 + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for k, _ in pairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +function deepcopy(src) + local dst = {} + for k, v in pairs(src) do + if type(v) == "table" then + v = deepcopy(v) + end + dst[k] = v + end + return dst +end + +function reverseequals(a, b) + if recursive_compare(a, b) then + return true + end + for i = 1, #a - 1 do + for j = i + 1, #a do + local c = deepcopy(a) + for x = i, j do + c[x] = a[j - (x - i)] + end + if recursive_compare(c, b) then + return true + end + end + end + return false +end + +if reverseequals({3, 2, 1, 4}, {1, 2, 3, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not reverseequals({1, 3, 4}, {4, 1, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if reverseequals({2}, {2}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-318/roger-bell-west/perl/ch-1.pl b/challenge-318/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..6a20c007db --- /dev/null +++ b/challenge-318/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,36 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(groupposition('abccccd'), ['cccc'], 'example 1'); +is_deeply(groupposition('aaabcddddeefff'), ['aaa', 'dddd', 'fff'], 'example 2'); +is_deeply(groupposition('abcdd'), [], 'example 3'); + +sub groupposition($a) { + my @mx; + my $lc = 'z'; + my @a = split '', $a; + while (my ($i, $c) = each @a) { + if ($i == 0) { + $lc = chr(ord($c) + 1); + } + if ($lc eq $c) { + my $mc = scalar @mx; + $mx[$mc - 1][1] += 1; + } else { + push @mx, [$c, 1]; + $lc = $c; + } + } + my @out; + foreach my $ms (@mx) { + if ($ms->[1] >= 3) { + push @out, $ms->[0] x $ms->[1]; + } + } + \@out; +} diff --git a/challenge-318/roger-bell-west/perl/ch-2.pl b/challenge-318/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..64b7ef33a9 --- /dev/null +++ b/challenge-318/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,35 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(reverseequals([3, 2, 1, 4], [1, 2, 3, 4]), 1, 'example 1'); +is(reverseequals([1, 3, 4], [4, 1, 3]), 0, 'example 2'); +is(reverseequals([2], [2]), 1, 'example 3'); + +use Storable qw(dclone); + +sub stringify($a) { + join(';', map {"$_"} @{$a}); +} + +sub reverseequals($a, $b) { + if (stringify($a) eq stringify($b)) { + return 1; + } + foreach my $i (0 .. $#{$a} - 1) { + foreach my $j ($i + 1 .. $#{$a}) { + my $c = dclone($a); + foreach my $x ($i .. $j) { + $c->[$x] = $a->[$j - ($x - $i)]; + } + if (stringify($c) eq stringify($b)) { + return 1; + } + } + } + 0; +} diff --git a/challenge-318/roger-bell-west/postscript/ch-1.ps b/challenge-318/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..a93f136901 --- /dev/null +++ b/challenge-318/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,151 @@ +%!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 + +/s2a { + [ exch { } 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 + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + 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 + + +% end included library code + +/groupposition { + 0 dict begin + /a exch def + /lc 65 def + [ + [ + a s2a enumerate.array { + aload pop + /c exch def + /i exch def + i 0 eq { + /lc c 1 add def + } if + lc c eq { + dup dup 1 get 1 add 1 exch put + } { + [ c 1 ] + /lc c def + } ifelse + } forall + ] { + /ms exch def + ms 1 get 3 ge { + /s ms 1 get string def + 0 1 ms 1 get 1 sub { + /i exch def + s i ms 0 get put + } for + s + } if + } forall + ] + end +} bind def + +(groupposition) test.start +(abccccd) groupposition [(cccc)] deepeq test +(aaabcddddeefff) groupposition [(aaa) (dddd) (fff)] deepeq test +(abcdd) groupposition [] deepeq test +test.end diff --git a/challenge-318/roger-bell-west/postscript/ch-2.ps b/challenge-318/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..9c115fc4c2 --- /dev/null +++ b/challenge-318/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,165 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/deepcopy { + 2 dict begin + /a exch def + a type /dicttype eq { + << + a keys { + /k exch def + k + a k get deepcopy + } forall + >> + } { + a type /arraytype eq { + [ + a { + deepcopy + } forall + ] + } { + a type /stringtype eq { + a dup length string cvs + } { + a + } ifelse + } ifelse + } ifelse + end +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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.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 { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/reverseequals { + 0 dict begin + /b exch def + /a exch def + a b deepeq { + true + } { + false + 0 1 a length 2 sub { + /i exch def + i 1 add 1 a length 1 sub { + /j exch def + /c a deepcopy def + i 1 j { + /x exch def + c x a j x i sub sub get put + } for + c b deepeq { + pop true + exit + } if + } for + } for + } ifelse + end +} bind def + +(reverseequals) test.start +[3 2 1 4] [1 2 3 4] reverseequals test +[1 3 4] [4 1 3] reverseequals not test +[2] [2] reverseequals test +test.end diff --git a/challenge-318/roger-bell-west/python/ch-1.py b/challenge-318/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..c995d12441 --- /dev/null +++ b/challenge-318/roger-bell-west/python/ch-1.py @@ -0,0 +1,34 @@ +#! /usr/bin/python3 + +def groupposition(a): + mx = [] + lc = "z" + for i, c in enumerate(a): + if i == 0: + lc = chr(ord(c) + 1) + if lc == c: + mc = len(mx) + mx[mc - 1][1] += 1 + else: + mx.append([c, 1]) + lc = c + out = [] + for ms in mx: + if ms[1] >= 3: + out.append(ms[0] * ms[1]) + return out + +import unittest + +class TestGroupposition(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(groupposition("abccccd"), ["cccc"], 'example 1') + + def test_ex2(self): + self.assertEqual(groupposition("aaabcddddeefff"), ["aaa", "dddd", "fff"], 'example 2') + + def test_ex3(self): + self.assertEqual(groupposition("abcdd"), [], 'example 3') + +unittest.main() diff --git a/challenge-318/roger-bell-west/python/ch-2.py b/challenge-318/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..85c5617c73 --- /dev/null +++ b/challenge-318/roger-bell-west/python/ch-2.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +def arreq(a, b): + if len(a) != len(b): + return False + for i, x in enumerate(a): + if x != b[i]: + return False + return True + +def reverseequals(a, b): + if arreq(a, b): + return True + for i in range(len(a) - 1): + for j in range(i + 1, len(a)): + c = a.copy() + for x in range(i, j + 1): + c[x] = a[j - (x - i)] + if arreq(c, b): + return True + return False + +import unittest + +class TestReverseequals(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(reverseequals([3, 2, 1, 4], [1, 2, 3, 4]), True, 'example 1') + + def test_ex2(self): + self.assertEqual(reverseequals([1, 3, 4], [4, 1, 3]), False, 'example 2') + + def test_ex3(self): + self.assertEqual(reverseequals([2], [2]), True, 'example 3') + +unittest.main() diff --git a/challenge-318/roger-bell-west/raku/ch-1.p6 b/challenge-318/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000 |
