From 32ef6ceed6fcb350e87bfa2455a7baf6b2ab5445 Mon Sep 17 00:00:00 2001 From: Pok Date: Tue, 13 May 2025 21:41:00 +1000 Subject: pwc321 solution in python --- challenge-321/pokgopun/python/ch-1.py | 71 +++++++++++++++++++++++++++++++++ challenge-321/pokgopun/python/ch-2.py | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 challenge-321/pokgopun/python/ch-1.py create mode 100644 challenge-321/pokgopun/python/ch-2.py diff --git a/challenge-321/pokgopun/python/ch-1.py b/challenge-321/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..d5ccc2fe7d --- /dev/null +++ b/challenge-321/pokgopun/python/ch-1.py @@ -0,0 +1,71 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-321/ +""" + +Task 1: Distinct Average + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of numbers with even length. + + Write a script to return the count of distinct average. The average is + calculate by removing the minimum and the maximum, then average of the + two. + +Example 1 + +Input: @nums = (1, 2, 4, 3, 5, 6) +Output: 1 + +Step 1: Min = 1, Max = 6, Avg = 3.5 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 1. + +Example 2 + +Input: @nums = (0, 2, 4, 8, 3, 5) +Output: 2 + +Step 1: Min = 0, Max = 8, Avg = 4 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 2. + +Example 3 + +Input: @nums = (7, 3, 1, 0, 5, 9) +Output: 2 + +Step 1: Min = 0, Max = 9, Avg = 4.5 +Step 2: Min = 1, Max = 7, Avg = 4 +Step 3: Min = 3, Max = 5, Avg = 4 + +The count of distinct average is 2. + +Task 2: Backspace Compare +""" +### solution by pokgopun@gmail.com + +def da(ints: tuple[int]) -> int: + l = len(ints) + lst = sorted(ints) + avgs = [] + for i in range(int(l/2)): + avgs.append((lst[i]+lst[l-1-i])/2) + return len(set(avgs)) + +import unittest + +class TestDa(unittest.TestCase): + def test(self): + for inpt, otpt in { + (1, 2, 4, 3, 5, 6): 1, + (0, 2, 4, 8, 3, 5): 2, + (7, 3, 1, 0, 5, 9): 2, + }.items(): + self.assertEqual(da(inpt), otpt) + +unittest.main() diff --git a/challenge-321/pokgopun/python/ch-2.py b/challenge-321/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..befadf0ef2 --- /dev/null +++ b/challenge-321/pokgopun/python/ch-2.py @@ -0,0 +1,74 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-321/ +""" + +Task 2: Backspace Compare + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings containing zero or more #. + + Write a script to return true if the two given strings are same by + treating # as backspace. + +Example 1 + +Input: $str1 = "ab#c" + $str2 = "ad#c" +Output: true + +For first string, we remove "b" as it is followed by "#". +For second string, we remove "d" as it is followed by "#". +In the end both strings became the same. + +Example 2 + +Input: $str1 = "ab##" + $str2 = "a#b#" +Output: true + +Example 3 + +Input: $str1 = "a#b" + $str2 = "c" +Output: false + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 18th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def bc(str1: str, str2: str) -> bool: + return ab(str1) == ab(str2) + +def ab(string: str) -> str: + lst = list(string) + l = len(lst) + i = 0 + while i < l: + if lst[i] == "#": + lst.pop(i) + l -= 1 + if i > 0: + lst.pop(i-1) + l -= 1 + i -= 1 + else: + i += 1 + return "".join(lst) + +import unittest + +class TestBc(unittest.TestCase): + def test(self): + for (str1, str2), otpt in { + ("ab#c", "ad#c"): True, + ("ab##", "a#b#"): True, + ("a#b", "c"): False, + }.items(): + self.assertEqual(bc(str1,str2),otpt) + +unittest.main() -- cgit From 7c3ee687c1e483f37869f932aebd08a0472ac7bb Mon Sep 17 00:00:00 2001 From: Pok Date: Tue, 13 May 2025 22:36:32 +1000 Subject: pwc321 solution in go --- challenge-321/pokgopun/go/ch-1.go | 91 +++++++++++++++++++++++++++++++++++++ challenge-321/pokgopun/go/ch-2.go | 95 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 challenge-321/pokgopun/go/ch-1.go create mode 100644 challenge-321/pokgopun/go/ch-2.go diff --git a/challenge-321/pokgopun/go/ch-1.go b/challenge-321/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..baffd55378 --- /dev/null +++ b/challenge-321/pokgopun/go/ch-1.go @@ -0,0 +1,91 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-321/ +/*# + +Task 1: Distinct Average + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of numbers with even length. + + Write a script to return the count of distinct average. The average is + calculate by removing the minimum and the maximum, then average of the + two. + +Example 1 + +Input: @nums = (1, 2, 4, 3, 5, 6) +Output: 1 + +Step 1: Min = 1, Max = 6, Avg = 3.5 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 1. + +Example 2 + +Input: @nums = (0, 2, 4, 8, 3, 5) +Output: 2 + +Step 1: Min = 0, Max = 8, Avg = 4 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 2. + +Example 3 + +Input: @nums = (7, 3, 1, 0, 5, 9) +Output: 2 + +Step 1: Min = 0, Max = 9, Avg = 4.5 +Step 2: Min = 1, Max = 7, Avg = 4 +Step 3: Min = 3, Max = 5, Avg = 4 + +The count of distinct average is 2. + +Task 2: Backspace Compare +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type qr struct { + q, r int +} + +type input []int + +func (in input) process() int { + l := len(in) + slices.Sort(in) + m := make(map[qr]bool) + for i := range l / 2 { + sm := (in[i] + in[l-1-i]) + m[qr{sm / 2, sm % 2}] = true + } + //fmt.Println(in, m) + return len(m) +} + +func main() { + for _, data := range []struct { + input input + output int + }{ + {input{1, 2, 4, 3, 5, 6}, 1}, + {input{0, 2, 4, 8, 3, 5}, 2}, + {input{7, 3, 1, 0, 5, 9}, 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-321/pokgopun/go/ch-2.go b/challenge-321/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9ea092effd --- /dev/null +++ b/challenge-321/pokgopun/go/ch-2.go @@ -0,0 +1,95 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-321/ +/*# + +Task 2: Backspace Compare + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings containing zero or more #. + + Write a script to return true if the two given strings are same by + treating # as backspace. + +Example 1 + +Input: $str1 = "ab#c" + $str2 = "ad#c" +Output: true + +For first string, we remove "b" as it is followed by "#". +For second string, we remove "d" as it is followed by "#". +In the end both strings became the same. + +Example 2 + +Input: $str1 = "ab##" + $str2 = "a#b#" +Output: true + +Example 3 + +Input: $str1 = "a#b" + $str2 = "c" +Output: false + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 18th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type input struct { + str1, str2 string +} + +func (in input) process() bool { + //fmt.Printf("%v, '%s', '%s'\n", in, ab(in.str1), ab(in.str2)) + return ab(in.str1) == ab(in.str2) +} + +func ab(str string) string { + l := len(str) + bs := []byte(str) + i := 0 + for i < l { + if bs[i] == byte('#') { + if i > 0 { + slices.Delete(bs, i-1, i+1) + l -= 2 + i-- + } else { + slices.Delete(bs, i, i+1) + l-- + } + } else { + i++ + } + } + return string(bs) +} + +func main() { + for _, data := range []struct { + input input + output bool + }{ + {input{"ab#c", "ad#c"}, true}, + {input{"ab##", "a#b#"}, true}, + {input{"a#b", "c"}, false}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} -- cgit From aa6afc31ada526c649d8490e9bdcb416027686f2 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 13 May 2025 14:06:50 +0100 Subject: RogerBW solutions for challenge no. 321 --- challenge-321/roger-bell-west/crystal/ch-1.cr | 24 ++++ challenge-321/roger-bell-west/crystal/ch-2.cr | 30 +++++ challenge-321/roger-bell-west/javascript/ch-1.js | 36 ++++++ challenge-321/roger-bell-west/javascript/ch-2.js | 38 ++++++ challenge-321/roger-bell-west/kotlin/ch-1.kt | 33 +++++ challenge-321/roger-bell-west/kotlin/ch-2.kt | 38 ++++++ challenge-321/roger-bell-west/lua/ch-1.lua | 42 +++++++ challenge-321/roger-bell-west/lua/ch-2.lua | 58 +++++++++ challenge-321/roger-bell-west/perl/ch-1.pl | 21 ++++ challenge-321/roger-bell-west/perl/ch-2.pl | 27 ++++ challenge-321/roger-bell-west/postscript/ch-1.ps | 153 +++++++++++++++++++++++ challenge-321/roger-bell-west/postscript/ch-2.ps | 120 ++++++++++++++++++ challenge-321/roger-bell-west/python/ch-1.py | 25 ++++ challenge-321/roger-bell-west/python/ch-2.py | 28 +++++ challenge-321/roger-bell-west/raku/ch-1.p6 | 19 +++ challenge-321/roger-bell-west/raku/ch-2.p6 | 25 ++++ challenge-321/roger-bell-west/ruby/ch-1.rb | 31 +++++ challenge-321/roger-bell-west/ruby/ch-2.rb | 35 ++++++ challenge-321/roger-bell-west/rust/ch-1.rs | 30 +++++ challenge-321/roger-bell-west/rust/ch-2.rs | 33 +++++ challenge-321/roger-bell-west/scala/ch-1.scala | 35 ++++++ challenge-321/roger-bell-west/scala/ch-2.scala | 40 ++++++ challenge-321/roger-bell-west/tests.json | 44 +++++++ challenge-321/roger-bell-west/typst/ch-1.typ | 27 ++++ challenge-321/roger-bell-west/typst/ch-2.typ | 33 +++++ 25 files changed, 1025 insertions(+) create mode 100755 challenge-321/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-321/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-321/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-321/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-321/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-321/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-321/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-321/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-321/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-321/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-321/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-321/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-321/roger-bell-west/python/ch-1.py create mode 100755 challenge-321/roger-bell-west/python/ch-2.py create mode 100755 challenge-321/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-321/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-321/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-321/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-321/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-321/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-321/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-321/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-321/roger-bell-west/tests.json create mode 100644 challenge-321/roger-bell-west/typst/ch-1.typ create mode 100644 challenge-321/roger-bell-west/typst/ch-2.typ diff --git a/challenge-321/roger-bell-west/crystal/ch-1.cr b/challenge-321/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..6b6a947474 --- /dev/null +++ b/challenge-321/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,24 @@ +#! /usr/bin/crystal + +def distinctaverage(a0) + a = a0.sort + offset = a.size - 1 + res = Set(Int32).new + 0.upto(a.size / 2 - 1) do |i| + res.add(a[i] + a[offset - i]) + end + res.size +end + +require "spec" +describe "distinctaverage" do + it "test_ex1" do + distinctaverage([1, 2, 4, 3, 5, 6]).should eq 1 + end + it "test_ex2" do + distinctaverage([0, 2, 4, 8, 3, 5]).should eq 2 + end + it "test_ex3" do + distinctaverage([7, 3, 1, 0, 5, 9]).should eq 2 + end +end diff --git a/challenge-321/roger-bell-west/crystal/ch-2.cr b/challenge-321/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..2797e896d0 --- /dev/null +++ b/challenge-321/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,30 @@ +#! /usr/bin/crystal + +def backspacecompare(a, b) + sa = Array(String).new + [a, b].each do |i| + oa = Array(Char).new + i.chars.each do |c| + if c == '#' + oa.pop + else + oa.push(c) + end + end + sa.push(oa.join("")) + end + sa[0] == sa[1] +end + +require "spec" +describe "backspacecompare" do + it "test_ex1" do + backspacecompare("ab#c", "ad#c").should eq true + end + it "test_ex2" do + backspacecompare("ab##", "a#b#").should eq true + end + it "test_ex3" do + backspacecompare("a#b", "c").should eq false + end +end diff --git a/challenge-321/roger-bell-west/javascript/ch-1.js b/challenge-321/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..d6f078281e --- /dev/null +++ b/challenge-321/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,36 @@ +#! /usr/bin/node + +"use strict" + +function distinctaverage(a0) { + let a = [...a0]; + a.sort(function(a,b) { + return a-b; + }); + const offset = a.length - 1; + const limit = Math.floor(a.length / 2); + let res = new Set; + for (let i = 0; i < limit; i++) { + res.add(a[i] + a[offset - i]); + } + return res.size; +} + +if (distinctaverage([1, 2, 4, 3, 5, 6]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (distinctaverage([0, 2, 4, 8, 3, 5]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (distinctaverage([7, 3, 1, 0, 5, 9]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-321/roger-bell-west/javascript/ch-2.js b/challenge-321/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..1a58b683d0 --- /dev/null +++ b/challenge-321/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,38 @@ +#! /usr/bin/node + +"use strict" + +function backspacecompare(a, b) { + let sa = []; + for (let i of [a, b]) { + let oa = []; + i.split("").forEach((c, _) => { + if (c == '#') { + oa.pop(); + } else { + oa.push(c); + } + }); + sa.push(oa.join('')); + } + return sa[0] == sa[1]; +} + +if (backspacecompare('ab#c', 'ad#c')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (backspacecompare('ab##', 'a#b#')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!backspacecompare('a#b', 'c')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-321/roger-bell-west/kotlin/ch-1.kt b/challenge-321/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..639e145135 --- /dev/null +++ b/challenge-321/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,33 @@ +fun distinctaverage(a0: List): Int { + val a = a0.sorted() + val offset = a.size - 1 + val limit = a.size / 2 + var res = mutableSetOf() + for (i in 0 .. limit - 1) { + res.add(a[i] + a[offset - i]) + } + return res.size +} + +fun main() { + + if (distinctaverage(listOf(1, 2, 4, 3, 5, 6)) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (distinctaverage(listOf(0, 2, 4, 8, 3, 5)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (distinctaverage(listOf(7, 3, 1, 0, 5, 9)) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-321/roger-bell-west/kotlin/ch-2.kt b/challenge-321/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..41f1d76487 --- /dev/null +++ b/challenge-321/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,38 @@ +fun backspacecompare(a: String, b: String): Boolean { + var sa = ArrayList() + for (i in listOf(a, b)) { + var oa = ArrayList() + i.toCharArray().forEach() {c -> + if (c == '#') { + oa.removeLast() + } else { + oa.add(c) + } + } + sa.add(oa.joinToString("")) + } + return sa[0] == sa[1] +} + +fun main() { + + if (backspacecompare("ab#c", "ad#c")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (backspacecompare("ab##", "a#b#")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!backspacecompare("a#b", "c")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-321/roger-bell-west/lua/ch-1.lua b/challenge-321/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..1445c9ad90 --- /dev/null +++ b/challenge-321/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,42 @@ +#! /usr/bin/lua + +function propersize(t) + local l=0 + for k,v in pairs(t) do + l = l + 1 + end + return l +end + +function distinctaverage(a0) + local a = a0 + table.sort(a,function (aa, bb) return aa < bb end) + local offset = #a + 1 + local res = {} + for i = 1, #a / 2 do + res[a[i] + a[offset - i]] = true + end + return propersize(res) +end + +if distinctaverage({1, 2, 4, 3, 5, 6}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if distinctaverage({0, 2, 4, 8, 3, 5}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if distinctaverage({7, 3, 1, 0, 5, 9}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-321/roger-bell-west/lua/ch-2.lua b/challenge-321/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..60d4d80aa7 --- /dev/null +++ b/challenge-321/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,58 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function join(t) + local out="" + for i, v in ipairs(t) do + out = out .. v + end + return out +end + +function backspacecompare(a, b) + local sa = {} + for _, i in ipairs({a, b}) do + local oa = {} + for _n, c in ipairs(split(i)) do + if c == "#" then + table.remove(oa, #oa) + else + table.insert(oa, c) + end + end + table.insert(sa, join(oa)) + end + return sa[1] == sa[2] +end + +if backspacecompare("ab#c", "ad#c") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if backspacecompare("ab##", "a#b#") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not backspacecompare("a#b", "c") then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-321/roger-bell-west/perl/ch-1.pl b/challenge-321/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..7463ba8611 --- /dev/null +++ b/challenge-321/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(distinctaverage([1, 2, 4, 3, 5, 6]), 1, 'example 1'); +is(distinctaverage([0, 2, 4, 8, 3, 5]), 2, 'example 2'); +is(distinctaverage([7, 3, 1, 0, 5, 9]), 2, 'example 3'); + +sub distinctaverage($a0) { + my @a = sort { $::a <=> $::b } @{$a0}; + my $offset = scalar @a - 1; + my %res; + foreach my $i (0 .. (scalar @a) / 2) { + $res{$a[$i] + $a[$offset - $i]} = 1; + } + scalar keys %res; +} diff --git a/challenge-321/roger-bell-west/perl/ch-2.pl b/challenge-321/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..391a91eb6f --- /dev/null +++ b/challenge-321/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,27 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(backspacecompare('ab#c', 'ad#c'), 1, 'example 1'); +is(backspacecompare('ab##', 'a#b#'), 1, 'example 2'); +is(backspacecompare('a#b', 'c'), 0, 'example 3'); + +sub backspacecompare($a, $b) { + my @sa; + foreach my $i ($a, $b) { + my @oa; + foreach my $c (split '', $i) { + if ($c eq '#') { + pop @oa; + } else { + push @oa, $c; + } + } + push @sa, join('', @oa); + } + ($sa[0] eq $sa[1])?1:0; +} diff --git a/challenge-321/roger-bell-west/postscript/ch-1.ps b/challenge-321/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..827ad4ffed --- /dev/null +++ b/challenge-321/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,153 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } 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 + +/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 + +/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 cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + 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 + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} 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 + +/distinctaverage { + 0 dict begin + quicksort + /a exch def + /offset a length 1 sub def + /res 0 dict def + 0 1 a length 2 idiv 1 sub { + /i exch def + res a i get a offset i sub get add true put + } for + res keys length + end +} bind def + +(distinctaverage) test.start +[1 2 4 3 5 6] distinctaverage 1 eq test +[0 2 4 8 3 5] distinctaverage 2 eq test +[7 3 1 0 5 9] distinctaverage 2 eq test +test.end diff --git a/challenge-321/roger-bell-west/postscript/ch-2.ps b/challenge-321/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..992dc0afb0 --- /dev/null +++ b/challenge-321/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,120 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/s2a { + [ exch { } forall ] +} 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 + +/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 { + /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 + +/backspacecompare { + 2 array astore + { + [ exch + s2a { + dup + 35 eq { + pop pop + } if + } forall + ] + } forall + deepeq +} bind def + +(backspacecompare) test.start +(ab#c) (ad#c) backspacecompare test +(ab##) (a#b#) backspacecompare test +(a#b) (c) backspacecompare not test +test.end diff --git a/challenge-321/roger-bell-west/python/ch-1.py b/challenge-321/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..f7482985c5 --- /dev/null +++ b/challenge-321/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def distinctaverage(a0): + a = a0.copy() + a.sort() + offset = len(a) - 1 + res = set() + for i in range(int(len(a) / 2)): + res.add(a[i] + a[offset - i]) + return len(res) + +import unittest + +class TestDistinctaverage(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(distinctaverage([1, 2, 4, 3, 5, 6]), 1, 'example 1') + + def test_ex2(self): + self.assertEqual(distinctaverage([0, 2, 4, 8, 3, 5]), 2, 'example 2') + + def test_ex3(self): + self.assertEqual(distinctaverage([7, 3, 1, 0, 5, 9]), 2, 'example 3') + +unittest.main() diff --git a/challenge-321/roger-bell-west/python/ch-2.py b/challenge-321/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..baf1c7a1c7 --- /dev/null +++ b/challenge-321/roger-bell-west/python/ch-2.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +def backspacecompare(a, b): + sa = [] + for i in [a, b]: + oa = [] + for c in i: + if c == "#": + oa.pop() + else: + oa.append(c) + sa.append("".join(oa)) + return sa[0] == sa[1] + +import unittest + +class TestBackspacecompare(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(backspacecompare("ab#c", "ad#c"), True, 'example 1') + + def test_ex2(self): + self.assertEqual(backspacecompare("ab##", "a#b#"), True, 'example 2') + + def test_ex3(self): + self.assertEqual(backspacecompare("a#b", "c"), False, 'example 3') + +unittest.main() diff --git a/challenge-321/roger-bell-west/raku/ch-1.p6 b/challenge-321/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..dca5b24b7f --- /dev/null +++ b/challenge-321/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(distinctaverage([1, 2, 4, 3, 5, 6]), 1, 'example 1'); +is(distinctaverage([0, 2, 4, 8, 3, 5]), 2, 'example 2'); +is(distinctaverage([7, 3, 1, 0, 5, 9]), 2, 'example 3'); + +sub distinctaverage(@a0) { + my @a = @a0.sort({ $^a <=> $^b }); + my $offset = @a.elems - 1; + my %res = SetHash.new; + for 0 ..^ floor(@a.elems / 2) -> $i { + %res{@a[$i] + @a[$offset - $i]}++; + } + %res.elems; +} diff --git a/challenge-321/roger-bell-west/raku/ch-2.p6 b/challenge-321/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..5b68c96366 --- /dev/null +++ b/challenge-321/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(backspacecompare('ab#c', 'ad#c'), True, 'example 1'); +is(backspacecompare('ab##', 'a#b#'), True, 'example 2'); +is(backspacecompare('a#b', 'c'), False, 'example 3'); + +sub backspacecompare($a, $b) { + my @sa; + for [$a, $b] -> $i { + my @oa; + for $i.comb -> $c { + if $c eq '#' { + @oa.pop; + } else { + @oa.push($c); + } + } + @sa.push(@oa.join('')); + } + @sa[0] eq @sa[1]; +} diff --git a/challenge-321/roger-bell-west/ruby/ch-1.rb b/challenge-321/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..aa5eb3e0cd --- /dev/null +++ b/challenge-321/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +require 'set' + +def distinctaverage(a0) + a = a0.sort + offset = a.size - 1 + res = Set.new + 0.upto(a.size / 2 - 1) do |i| + res.add(a[i] + a[offset - i]) + end + res.size +end + +require 'test/unit' + +class TestDistinctaverage < Test::Unit::TestCase + + def test_ex1 + assert_equal(1, distinctaverage([1, 2, 4, 3, 5, 6])) + end + + def test_ex2 + assert_equal(2, distinctaverage([0, 2, 4, 8, 3, 5])) + end + + def test_ex3 + assert_equal(2, distinctaverage([7, 3, 1, 0, 5, 9])) + end + +end diff --git a/challenge-321/roger-bell-west/ruby/ch-2.rb b/challenge-321/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..9df57e641c --- /dev/null +++ b/challenge-321/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,35 @@ +#! /usr/bin/ruby + +def backspacecompare(a, b) + sa = Array.new + [a, b].each do |i| + oa = Array.new + i.chars.each do |c| + if c == '#' + oa.pop + else + oa.push(c) + end + end + sa.push(oa.join("")) + end + sa[0] == sa[1] +end + +require 'test/unit' + +class TestBackspacecompare < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, backspacecompare('ab#c', 'ad#c')) + end + + def test_ex2 + assert_equal(true, backspacecompare('ab##', 'a#b#')) + end + + def test_ex3 + assert_equal(false, backspacecompare('a#b', 'c')) + end + +end diff --git a/challenge-321/roger-bell-west/rust/ch-1.rs b/challenge-321/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..829a240e6d --- /dev/null +++ b/challenge-321/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,30 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!(distinctaverage(vec![1, 2, 4, 3, 5, 6]), 1); +} + +#[test] +fn test_ex2() { + assert_eq!(distinctaverage(vec![0, 2, 4, 8, 3, 5]), 2); +} + +#[test] +fn test_ex3() { + assert_eq!(distinctaverage(vec![7, 3, 1, 0, 5, 9]), 2); +} + +fn distinctaverage(a0: Vec) -> usize { + let mut a = a0.clone(); + a.sort(); + let offset = a.len() - 1; + let mut res = HashSet::new(); + for i in 0 .. a.len() / 2 { + res.insert(a[i] + a[offset - i]); + } + res.len() +} diff --git a/challenge-321/roger-bell-west/rust/ch-2.rs b/challenge-321/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..3111355721 --- /dev/null +++ b/challenge-321/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,33 @@ +#! /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!(backspacecompare("ab#c", "ad#c"), true); +} + +#[test] +fn test_ex2() { + assert_eq!(backspacecompare("ab##", "a#b#"), true); +} + +#[test] +fn test_ex3() { + assert_eq!(backspacecompare("a#b", "c"), false); +} + +fn backspacecompare(a: &str, b: &str) -> bool { + let mut sa: Vec = Vec::new(); + for i in [a, b] { + let mut oa = Vec::new(); + for c in i.chars() { + if c == '#' { + oa.pop(); + } else { + oa.push(c) + } + } + sa.push(oa.iter().collect()); + } + sa[0] == sa[1] +} diff --git a/challenge-321/roger-bell-west/scala/ch-1.scala b/challenge-321/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..765aa14dc1 --- /dev/null +++ b/challenge-321/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,35 @@ +import scala.collection.mutable + +object Distinctaverage { + def distinctaverage(a0: List[Int]): Int = { + val a = a0.sortWith(_ < _) + val offset = a.size - 1 + val limit = a.size / 2 + var res = mutable.Set.empty[Int] + for (i <- 0 to limit - 1) { + res += (a(i) + a(offset - i)) + } + res.size + } + def main(args: Array[String]) { + if (distinctaverage(List(1, 2, 4, 3, 5, 6)) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (distinctaverage(List(0, 2, 4, 8, 3, 5)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (distinctaverage(List(7, 3, 1, 0, 5, 9)) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-321/roger-bell-west/scala/ch-2.scala b/challenge-321/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..d434a2ac52 --- /dev/null +++ b/challenge-321/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,40 @@ +import scala.collection.mutable.ListBuffer + +object Backspacecompare { + def backspacecompare(a: String, b: String): Boolean = { + var sa = new ListBuffer[String] + for (i <- List(a, b)) { + var oa = new ListBuffer[Char] + i.toCharArray.foreach(c => { + if (c == '#') { + oa = oa.dropRight(1) + } else { + oa += c + } + }) + sa += oa.toString + } + sa(0) == sa(1) + } + def main(args: Array[String]) { + if (backspacecompare("ab#c", "ad#c")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (backspacecompare("ab##", "a#b#")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!backspacecompare("a#b", "c")) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-321/roger-bell-west/tests.json b/challenge-321/roger-bell-west/tests.json new file mode 100644 index 0000000000..a15bb1042f --- /dev/null +++ b/challenge-321/roger-bell-west/tests.json @@ -0,0 +1,44 @@ +{ + "ch-1" : [ + { + "function" : "distinctaverage", + "arguments" : [1, 2, 4, 3, 5, 6], + "result" : 1 + }, + { + "arguments" : [0, 2, 4, 8, 3, 5], + "result" : 2 + }, + { + "arguments" : [7, 3, 1, 0, 5, 9], + "result" : 2 + } + ], + "ch-2" : [ + { + "function" : "backspacecompare", + "multiarg" : true, + "arguments" : [ + "ab#c", + "ad#c" + ], + "result" : true + }, + { + "multiarg" : true, + "arguments" : [ + "ab##", + "a#b#" + ], + "result" : true + }, + { + "multiarg" : true, + "arguments" : [ + "a#b", + "c" + ], + "result" : false + } + ] +} diff --git a/challenge-321/roger-bell-west/typst/ch-1.typ b/challenge-321/roger-bell-west/typst/ch-1.typ new file mode 100644 index 0000000000..30244c9c96 --- /dev/null +++ b/challenge-321/roger-bell-west/typst/ch-1.typ @@ -0,0 +1,27 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let distinctaverage(a0) = { + let a = a0.sorted() + let offset = a.len() - 1 + let res = ().to-dict() + for i in range(int(a.len() / 2)) { + res.insert(str(a.at(i) + a.at(offset - i)), true) + } + res.len() +} + +Test 1: + #testresult(distinctaverage((1, 2, 4, 3, 5, 6)) == 1) + +Test 2: + #testresult(distinctaverage((0, 2, 4, 8, 3, 5)) == 2) + +Test 3: + #testresult(distinctaverage((7, 3, 1, 0, 5, 9)) == 2) + diff --git a/challenge-321/roger-bell-west/typst/ch-2.typ b/challenge-321/roger-bell-west/typst/ch-2.typ new file mode 100644 index 0000000000..21f1414ae6 --- /dev/null +++ b/challenge-321/roger-bell-west/typst/ch-2.typ @@ -0,0 +1,33 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let backspacecompare(a, b) = { + let sa = () + for i in (a, b) { + let oa = () + for c in i { + if c == "#" { + let _ = oa.pop() + } else { + oa.push(c) + } + } + sa.push(oa.join(none)) + } + sa.at(0) == sa.at(1) +} + +Test 1: + #testresult(backspacecompare("ab#c", "ad#c")) + +Test 2: + #testresult(backspacecompare("ab##", "a#b#")) + +Test 3: + #testresult(not backspacecompare("a#b", "c")) + -- cgit