From 994cc826d23c6cdd8c55b28a49ecc7dac9c54145 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 4 Nov 2025 11:48:22 +0000 Subject: RogerBW solutions for challenge no. 346 --- challenge-346/roger-bell-west/crystal/ch-1.cr | 49 ++++++++++++ challenge-346/roger-bell-west/javascript/ch-1.js | 62 +++++++++++++++ challenge-346/roger-bell-west/javascript/ch-2.js | 97 ++++++++++++++++++++++++ challenge-346/roger-bell-west/lua/ch-1.lua | 77 +++++++++++++++++++ challenge-346/roger-bell-west/perl/ch-1.pl | 44 +++++++++++ challenge-346/roger-bell-west/perl/ch-2.pl | 48 ++++++++++++ challenge-346/roger-bell-west/postscript/ch-1.ps | 79 +++++++++++++++++++ challenge-346/roger-bell-west/ruby/ch-1.rb | 56 ++++++++++++++ challenge-346/roger-bell-west/ruby/ch-2.rb | 66 ++++++++++++++++ challenge-346/roger-bell-west/rust/ch-1.rs | 56 ++++++++++++++ challenge-346/roger-bell-west/scala/ch-1.scala | 63 +++++++++++++++ challenge-346/roger-bell-west/tests.json | 53 +++++++++++++ challenge-346/roger-bell-west/typst/ch-1.typ | 51 +++++++++++++ 13 files changed, 801 insertions(+) create mode 100755 challenge-346/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-346/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-346/roger-bell-west/javascript/ch-2.js create mode 100755 challenge-346/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-346/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-346/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-346/roger-bell-west/postscript/ch-1.ps create mode 100755 challenge-346/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-346/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-346/roger-bell-west/rust/ch-1.rs create mode 100644 challenge-346/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-346/roger-bell-west/tests.json create mode 100644 challenge-346/roger-bell-west/typst/ch-1.typ diff --git a/challenge-346/roger-bell-west/crystal/ch-1.cr b/challenge-346/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..85c58abf93 --- /dev/null +++ b/challenge-346/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,49 @@ +#! /usr/bin/crystal + +def longestparenthesis(aa) + a = aa.chars + ml = 0 + 0.upto(a.size - 1) do |l| + l.upto(a.size - 1) do |r| + depth = 0 + valid = true + l.upto(r) do |i| + if a[i] == '(' + depth += 1 + else + depth -= 1 + if depth < 0 + valud = false + break + end + end + end + if depth != 0 + valid = false + end + if valid + ml = [ml, r - l + 1].max + end + end + end + ml +end + +require "spec" +describe "longestparenthesis" do + it "test_ex1" do + longestparenthesis("(()())").should eq 6 + end + it "test_ex2" do + longestparenthesis(")()())").should eq 4 + end + it "test_ex3" do + longestparenthesis("((()))()(((()").should eq 8 + end + it "test_ex4" do + longestparenthesis("))))((()(").should eq 2 + end + it "test_ex5" do + longestparenthesis("()(()").should eq 2 + end +end diff --git a/challenge-346/roger-bell-west/javascript/ch-1.js b/challenge-346/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..4b01d7710c --- /dev/null +++ b/challenge-346/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,62 @@ +#! /usr/bin/node + +"use strict" + +function longestparenthesis(aa) { + const a = aa.split(''); + let ml = 0; + for (let l = 0 ; l < a.length; l++) { + for (let r = l; r < a.length; r++) { + let depth = 0; + let constid = true; + for (let i = l; i <= r; i++) { + if (a[i] == '(') { + depth += 1; + } else { + depth -= 1; + if (depth < 0) { + constid = false; + } + } + } + if (depth != 0) { + constid = false; + } + if (constid) { + ml = Math.max(ml, r - l + 1); + } + } + } + return ml; +} + +if (longestparenthesis('(()())') == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (longestparenthesis(')()())') == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (longestparenthesis('((()))()(((()') == 8) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (longestparenthesis('))))((()(') == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (longestparenthesis('()(()') == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-346/roger-bell-west/javascript/ch-2.js b/challenge-346/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..1486aee2ab --- /dev/null +++ b/challenge-346/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,97 @@ +#! /usr/bin/node + +"use strict" + +function magicexpression(number, target) { + const n = number.split(''); + const l = number.length - 1; + let counter = new Array(l).fill(0); + let out = []; + const rx = new RegExp("(^|[^0-9])0[0-9]"); + LOOP: + while (true) { + let i = 0; + counter[i]++; + while (counter[i] == 4) { + counter[i] = 0; + i++; + if (i < l) { + counter[i]++; + } else { + break LOOP; + } + } + let ex = ""; + for (let i = 0; i < l; i++) { + ex += n[i]; + ex += ["", '+', '-', '*'][counter[i]]; + } + ex += n[l]; + if (ex.search(rx) != -1) { + continue LOOP; + } + if (eval(ex) == target) { + out.push(ex); + } + } + out.sort(); + return out; +} + +// 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; + } +} + +if (deepEqual(magicexpression('123', 6), ['1*2*3', '1+2+3'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(magicexpression('105', 5), ['1*0+5', '10-5'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(magicexpression('232', 8), ['2*3+2', '2+3*2'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(magicexpression('1234', 10), ['1*2*3+4', '1+2+3+4'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(magicexpression('1001', 2), ['1+0*0+1', '1+0+0+1', '1+0-0+1', '1-0*0+1', '1-0+0+1', '1-0-0+1'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-346/roger-bell-west/lua/ch-1.lua b/challenge-346/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..38386eddd9 --- /dev/null +++ b/challenge-346/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,77 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function longestparenthesis(aa) + local a = split(aa) + local ml = 0 + for l = 1, #a do + for r = l, #a do + local depth = 0 + local valid = true + for i = l, r do + if a[i] == "(" then + depth = depth + 1 + else + depth = depth - 1 + if depth < 0 then + valid = false + break + end + end + end + if depth ~= 0 then + valid = false + end + if valid then + ml = math.max(ml, r - l + 1) + end + end + end + return ml +end + +if longestparenthesis("(()())") == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if longestparenthesis(")()())") == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if longestparenthesis("((()))()(((()") == 8 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if longestparenthesis("))))((()(") == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if longestparenthesis("()(()") == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-346/roger-bell-west/perl/ch-1.pl b/challenge-346/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..38d9e1f2ad --- /dev/null +++ b/challenge-346/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,44 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(longestparenthesis('(()())'), 6, 'example 1'); +is(longestparenthesis(')()())'), 4, 'example 2'); +is(longestparenthesis('((()))()(((()'), 8, 'example 3'); +is(longestparenthesis('))))((()('), 2, 'example 4'); +is(longestparenthesis('()(()'), 2, 'example 5'); + +use List::Util qw(max); + +sub longestparenthesis($aa) { + my @a = split '', $aa; + my $ml = 0; + foreach my $l (0 .. $#a) { + foreach my $r ($l .. $#a) { + my $depth = 0; + my $valid = 1; + foreach my $i ($l .. $r) { + if ($a[$i] eq '(') { + $depth++; + } else { + $depth--; + if ($depth < 0) { + $valid = 0; + last; + } + } + } + if ($depth != 0) { + $valid = 0; + } + if ($valid) { + $ml = max($ml, $r - $l + 1); + } + } + } + $ml; +} diff --git a/challenge-346/roger-bell-west/perl/ch-2.pl b/challenge-346/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..eb3b3c8a58 --- /dev/null +++ b/challenge-346/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,48 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is_deeply(magicexpression('123', 6), ['1*2*3', '1+2+3'], 'example 1'); +is_deeply(magicexpression('105', 5), ['1*0+5', '10-5'], 'example 2'); +is_deeply(magicexpression('232', 8), ['2*3+2', '2+3*2'], 'example 3'); +is_deeply(magicexpression('1234', 10), ['1*2*3+4', '1+2+3+4'], 'example 4'); +is_deeply(magicexpression('1001', 2), ['1+0*0+1', '1+0+0+1', '1+0-0+1', '1-0*0+1', '1-0+0+1', '1-0-0+1'], 'example 5'); + +sub magicexpression($number, $target) { + my @n = split '', $number; + my $l = length($number) - 1; + my @counter = (0) x $l; + my @out; + LOOP: + while (1) { + my $i = 0; + $counter[$i]++; + while ($counter[$i] == 4) { + $counter[$i] = 0; + $i++; + if ($i < $l) { + $counter[$i]++; + } else { + last LOOP; + } + } + my $ex = ''; + foreach my $i (0 .. $l - 1) { + $ex .= $n[$i]; + $ex .= ['', '+', '-', '*']->[$counter[$i]]; + } + $ex .= $n[$l]; + if ($ex =~ /(^|\D)0\d/) { + next LOOP; + } + if (eval($ex) == $target) { + push @out, $ex; + } + } + @out = sort @out; + \@out; +} diff --git a/challenge-346/roger-bell-west/postscript/ch-1.ps b/challenge-346/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..30dc0bfc54 --- /dev/null +++ b/challenge-346/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,79 @@ +%!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 { + /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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/longestparenthesis { + 0 dict begin + /a exch def + /ml 0 def + 0 1 a length 1 sub { + /l exch def + l 1 add 1 a length 1 sub { + /r exch def + /depth 0 def + /valid true def + l 1 r { + /i exch def + a i get 40 eq { + /depth depth 1 add def + } { + /depth depth 1 sub def + depth 0 lt { + /valid false def + exit + } if + } ifelse + } for + depth 0 ne { + /valid false def + } if + valid { + /ml ml r l sub 1 add max def + } if + } for + } for + ml + end +} bind def + +(longestparenthesis) test.start +(\(\(\)\(\)\)) longestparenthesis 6 eq test +(\)\(\)\(\)\)) longestparenthesis 4 eq test +(\(\(\(\)\)\)\(\)\(\(\(\(\)) longestparenthesis 8 eq test +(\)\)\)\)\(\(\(\)\() longestparenthesis 2 eq test +(\(\)\(\(\)) longestparenthesis 2 eq test +test.end diff --git a/challenge-346/roger-bell-west/ruby/ch-1.rb b/challenge-346/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..8ff61a5d1f --- /dev/null +++ b/challenge-346/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,56 @@ +#! /usr/bin/ruby + +def longestparenthesis(aa) + a = aa.chars + ml = 0 + 0.upto(a.size - 1) do |l| + l.upto(a.size - 1) do |r| + depth = 0 + valid = true + l.upto(r) do |i| + if a[i] == '(' + depth += 1 + else + depth -= 1 + if depth < 0 + valud = false + break + end + end + end + if depth != 0 + valid = false + end + if valid + ml = [ml, r - l + 1].max + end + end + end + ml +end + +require 'test/unit' + +class TestLongestparenthesis < Test::Unit::TestCase + + def test_ex1 + assert_equal(6, longestparenthesis('(()())')) + end + + def test_ex2 + assert_equal(4, longestparenthesis(')()())')) + end + + def test_ex3 + assert_equal(8, longestparenthesis('((()))()(((()')) + end + + def test_ex4 + assert_equal(2, longestparenthesis('))))((()(')) + end + + def test_ex5 + assert_equal(2, longestparenthesis('()(()')) + end + +end diff --git a/challenge-346/roger-bell-west/ruby/ch-2.rb b/challenge-346/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..1e19fb7432 --- /dev/null +++ b/challenge-346/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,66 @@ +#! /usr/bin/ruby + +def magicexpression(number, target) + n = number.chars + l = n.size - 1 + counter = Array.new(l, 0) + out = Array.new + loop do + i = 0 + counter[i] += 1 + brk = false + while counter[i] == 4 + counter[i] = 0 + i += 1 + if i < l + counter[i] += 1 + else + brk = true + break + end + end + if brk + break + end + ex = "" + 0.upto(l - 1) do |i| + ex += n[i] + ex += ["", "+", "-", "*"][counter[i]] + end + ex += n[l] + if ex =~ /(^|\D)0\d/ + next + end + if eval(ex) == target + out.push(ex) + end + end + out.sort! + out +end + +require 'test/unit' + +class TestMagicexpression < Test::Unit::TestCase + + def test_ex1 + assert_equal(['1*2*3', '1+2+3'], magicexpression('123', 6)) + end + + def test_ex2 + assert_equal(['1*0+5', '10-5'], magicexpression('105', 5)) + end + + def test_ex3 + assert_equal(['2*3+2', '2+3*2'], magicexpression('232', 8)) + end + + def test_ex4 + assert_equal(['1*2*3+4', '1+2+3+4'], magicexpression('1234', 10)) + end + + def test_ex5 + assert_equal(['1+0*0+1', '1+0+0+1', '1+0-0+1', '1-0*0+1', '1-0+0+1', '1-0-0+1'], magicexpression('1001', 2)) + end + +end diff --git a/challenge-346/roger-bell-west/rust/ch-1.rs b/challenge-346/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..cebaffa2e4 --- /dev/null +++ b/challenge-346/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,56 @@ +#! /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!(longestparenthesis("(()())"), 6); +} + +#[test] +fn test_ex2() { + assert_eq!(longestparenthesis(")()())"), 4); +} + +#[test] +fn test_ex3() { + assert_eq!(longestparenthesis("((()))()(((()"), 8); +} + +#[test] +fn test_ex4() { + assert_eq!(longestparenthesis("))))((()("), 2); +} + +#[test] +fn test_ex5() { + assert_eq!(longestparenthesis("()(()"), 2); +} + +fn longestparenthesis(aa: &str) -> usize { + let a = aa.chars().collect::>(); + let mut ml = 0; + for l in 0 .. a.len() { + for r in l .. a.len() { + let mut depth = 0i32; + let mut valid = true; + for i in l ..= r { + if a[i] == '(' { + depth += 1; + } else { + depth -= 1; + if depth < 0 { + valid = false; + break; + } + } + } + if depth != 0 { + valid = false; + } + if valid { + ml = std::cmp::max(ml, r - l + 1); + } + } + } + ml +} diff --git a/challenge-346/roger-bell-west/scala/ch-1.scala b/challenge-346/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..f402c7b997 --- /dev/null +++ b/challenge-346/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,63 @@ + +object Longestparenthesis { + def longestparenthesis(aa: String): Int = { + val a = aa.toList + var ml = 0 + for (l <- 0 until a.size) { + for (r <- l until a.size) { + var depth = 0 + var valid = true + for (i <- l to r) { + if (a(i) == '(') { + depth += 1; + } else { + depth -= 1 + if (depth < 0) { + valid = false + } + } + } + if (depth != 0) { + valid = false + } + if (valid) { + ml = List(ml, r - l + 1).max + } + } + } + ml + } + def main(args: Array[String]) { + if (longestparenthesis("(()())") == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (longestparenthesis(")()())") == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (longestparenthesis("((()))()(((()") == 8) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (longestparenthesis("))))((()(") == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (longestparenthesis("()(()") == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-346/roger-bell-west/tests.json b/challenge-346/roger-bell-west/tests.json new file mode 100644 index 0000000000..346a8c66c7 --- /dev/null +++ b/challenge-346/roger-bell-west/tests.json @@ -0,0 +1,53 @@ +{ + "ch-1" : [ + { + "function" : "longestparenthesis", + "arguments" : "(()())", + "result" : 6 + }, + { + "arguments" : ")()())", + "result" : 4 + }, + { + "arguments" : "((()))()(((()", + "result" : 8 + }, + { + "arguments" : "))))((()(", + "result" : 2 + }, + { + "arguments" : "()(()", + "result" : 2 + } + ], + "ch-2" : [ + { + "function" : "magicexpression", + "multiarg" : true, + "arguments" : [ "123", 6 ], + "result" : [ "1*2*3", "1+2+3" ] + }, + { + "multiarg" : true, + "arguments" : [ "105", 5 ], + "result" : [ "1*0+5", "10-5" ] + }, + { + "multiarg" : true, + "arguments" : [ "232", 8 ], + "result" : [ "2*3+2", "2+3*2" ] + }, + { + "multiarg" : true, + "arguments" : [ "1234", 10 ], + "result" : [ "1*2*3+4", "1+2+3+4" ] + }, + { + "multiarg" : true, + "arguments" : [ "1001", 2 ], + "result" : [ "1+0*0+1", "1+0+0+1", "1+0-0+1", "1-0*0+1", "1-0+0+1", "1-0-0+1" ] + } + ] +} diff --git a/challenge-346/roger-bell-west/typst/ch-1.typ b/challenge-346/roger-bell-west/typst/ch-1.typ new file mode 100644 index 0000000000..7e825f49e1 --- /dev/null +++ b/challenge-346/roger-bell-west/typst/ch-1.typ @@ -0,0 +1,51 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let longestparenthesis(a) = { + let ml = 0 + for l in range(a.len()) { + for r in range(l, a.len()) { + let depth = 0 + let valid = true + for i in range(l, r + 1) { + if a.at(i) == "(" { + depth += 1 + } else { + depth -= 1 + if depth < 0 { + valid = false + break + } + } + } + if depth != 0 { + valid = false + } + if valid { + ml = calc.max(ml, r - l + 1) + } + } + } + ml +} + +Test 1: + #testresult(longestparenthesis("(()())") == 6) + +Test 2: + #testresult(longestparenthesis(")()())") == 4) + +Test 3: + #testresult(longestparenthesis("((()))()(((()") == 8) + +Test 4: + #testresult(longestparenthesis("))))((()(") == 2) + +Test 5: + #testresult(longestparenthesis("()(()") == 2) + -- cgit