From 20ce1cadd1f73bebb9c7ad6490d98b70f82592be Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 10 Jun 2025 09:48:27 +0100 Subject: RogerBW solutions for challenge no. 325 --- challenge-325/roger-bell-west/crystal/ch-1.cr | 35 ++++++ challenge-325/roger-bell-west/crystal/ch-2.cr | 29 +++++ challenge-325/roger-bell-west/javascript/ch-1.js | 43 +++++++ challenge-325/roger-bell-west/javascript/ch-2.js | 64 +++++++++++ challenge-325/roger-bell-west/kotlin/ch-1.kt | 43 +++++++ challenge-325/roger-bell-west/kotlin/ch-2.kt | 37 ++++++ challenge-325/roger-bell-west/lua/ch-1.lua | 54 +++++++++ challenge-325/roger-bell-west/lua/ch-2.lua | 67 +++++++++++ challenge-325/roger-bell-west/perl/ch-1.pl | 33 ++++++ challenge-325/roger-bell-west/perl/ch-2.pl | 26 +++++ challenge-325/roger-bell-west/postscript/ch-1.ps | 105 +++++++++++++++++ challenge-325/roger-bell-west/postscript/ch-2.ps | 137 +++++++++++++++++++++++ challenge-325/roger-bell-west/python/ch-1.py | 32 ++++++ challenge-325/roger-bell-west/python/ch-2.py | 27 +++++ challenge-325/roger-bell-west/raku/ch-1.p6 | 30 +++++ challenge-325/roger-bell-west/raku/ch-2.p6 | 24 ++++ challenge-325/roger-bell-west/ruby/ch-1.rb | 40 +++++++ challenge-325/roger-bell-west/ruby/ch-2.rb | 34 ++++++ challenge-325/roger-bell-west/rust/ch-1.rs | 40 +++++++ challenge-325/roger-bell-west/rust/ch-2.rs | 32 ++++++ challenge-325/roger-bell-west/scala/ch-1.scala | 45 ++++++++ challenge-325/roger-bell-west/scala/ch-2.scala | 47 ++++++++ challenge-325/roger-bell-west/tests.json | 32 ++++++ challenge-325/roger-bell-west/typst/ch-1.typ | 37 ++++++ challenge-325/roger-bell-west/typst/ch-2.typ | 32 ++++++ 25 files changed, 1125 insertions(+) create mode 100755 challenge-325/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-325/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-325/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-325/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-325/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-325/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-325/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-325/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-325/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-325/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-325/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-325/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-325/roger-bell-west/python/ch-1.py create mode 100755 challenge-325/roger-bell-west/python/ch-2.py create mode 100755 challenge-325/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-325/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-325/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-325/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-325/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-325/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-325/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-325/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-325/roger-bell-west/tests.json create mode 100644 challenge-325/roger-bell-west/typst/ch-1.typ create mode 100644 challenge-325/roger-bell-west/typst/ch-2.typ diff --git a/challenge-325/roger-bell-west/crystal/ch-1.cr b/challenge-325/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..c62555f697 --- /dev/null +++ b/challenge-325/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,35 @@ +#! /usr/bin/crystal + +def consecutiveone(a) + h = Hash(Int32, Int32).new + h[0] = 0 + latch = 0 + latched = false + a.each_with_index do |n, i| + if n == 1 && !latched + latched = true + latch = i + end + if n == 0 && latched + latched = false + h[latch] = i - latch + end + end + if latched + h[latch] = a.size - latch + end + h.values.max +end + +require "spec" +describe "consecutiveone" do + it "test_ex1" do + consecutiveone([0, 1, 1, 0, 1, 1, 1]).should eq 3 + end + it "test_ex2" do + consecutiveone([0, 0, 0, 0]).should eq 0 + end + it "test_ex3" do + consecutiveone([1, 0, 1, 0, 1, 1]).should eq 2 + end +end diff --git a/challenge-325/roger-bell-west/crystal/ch-2.cr b/challenge-325/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..38cd58d203 --- /dev/null +++ b/challenge-325/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,29 @@ +#! /usr/bin/crystal + +def finalprice(a) + out = Array(Int32).new + a.each_with_index do |n, i| + discount = 0 + (i + 1).upto(a.size - 1) do |mi| + if a[mi] <= n + discount = a[mi] + break + end + end + out.push(n - discount) + end + out +end + +require "spec" +describe "finalprice" do + it "test_ex1" do + finalprice([8, 4, 6, 2, 3]).should eq [4, 2, 4, 2, 3] + end + it "test_ex2" do + finalprice([1, 2, 3, 4, 5]).should eq [1, 2, 3, 4, 5] + end + it "test_ex3" do + finalprice([7, 1, 1, 5]).should eq [6, 0, 1, 5] + end +end diff --git a/challenge-325/roger-bell-west/javascript/ch-1.js b/challenge-325/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..f56af754e9 --- /dev/null +++ b/challenge-325/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,43 @@ +#! /usr/bin/node + +"use strict" + +function consecutiveone(a) { + let h = new Map; + h.set(0, 0); + let latch = 0; + let latched = false; + a.forEach((n, i) => { + if (n == 1 && !latched) { + latched = true; + latch = i; + } + if (n == 0 && latched) { + latched = false; + h.set(latch, i - latch); + } + }); + if (latched) { + h.set(latch, a.length - latch); + } + return Math.max(...h.values()); +} + +if (consecutiveone([0, 1, 1, 0, 1, 1, 1]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (consecutiveone([0, 0, 0, 0]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (consecutiveone([1, 0, 1, 0, 1, 1]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-325/roger-bell-west/javascript/ch-2.js b/challenge-325/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..5fd6f5116e --- /dev/null +++ b/challenge-325/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,64 @@ +#! /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 finalprice(a) { + let out = []; + a.forEach((n, i) => { + let discount = 0; + for (let mi = i + 1; mi < a.length; mi++) { + if (a[mi] <= n) { + discount = a[mi]; + break; + } + } + out.push(n - discount); + }); + return out; +} + +if (deepEqual(finalprice([8, 4, 6, 2, 3]), [4, 2, 4, 2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(finalprice([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(finalprice([7, 1, 1, 5]), [6, 0, 1, 5])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-325/roger-bell-west/kotlin/ch-1.kt b/challenge-325/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..65f28685c6 --- /dev/null +++ b/challenge-325/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,43 @@ +fun consecutiveone(a: List): Int { + var h = mutableMapOf() + h[0] = 0 + var latch = 0 + var latched = false + a.forEachIndexed {i, n -> + if (n == 1 && !latched) { + latched = true + latch = i + } + if (n == 0 && latched) { + latched = false + h[latch] = i - latch + } + } + if (latched) { + h[latch] = a.size - latch + } + return h.values.maxOrNull()!! +} + +fun main() { + + if (consecutiveone(listOf(0, 1, 1, 0, 1, 1, 1)) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (consecutiveone(listOf(0, 0, 0, 0)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (consecutiveone(listOf(1, 0, 1, 0, 1, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-325/roger-bell-west/kotlin/ch-2.kt b/challenge-325/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..c6239bf943 --- /dev/null +++ b/challenge-325/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,37 @@ +fun finalprice(a: List): List { + var out = ArrayList() + a.forEachIndexed {i, n -> + var discount = 0 + for (mi in i + 1 .. a.size - 1) { + if (a[mi] <= n) { + discount = a[mi] + break + } + } + out.add(n - discount) + } + return out.toList() +} + +fun main() { + + if (finalprice(listOf(8, 4, 6, 2, 3)) == listOf(4, 2, 4, 2, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (finalprice(listOf(1, 2, 3, 4, 5)) == listOf(1, 2, 3, 4, 5)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (finalprice(listOf(7, 1, 1, 5)) == listOf(6, 0, 1, 5)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-325/roger-bell-west/lua/ch-1.lua b/challenge-325/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..82f30060b8 --- /dev/null +++ b/challenge-325/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,54 @@ +#! /usr/bin/lua + +function values(t) + local a = {} + for k, v in pairs(t) do + table.insert(a, v) + end + return a +end + +function consecutiveone(a) + local h = {} + h[0] = 0 + local latch = 0 + local latched = false + for i, n in ipairs(a) do + if n == 1 and not latched then + latched = true + latch = i + end + if n == 0 and latched then + latched = false + h[latch] = i - latch + end + end + if latched then + h[latch] = #a + 1 - latch + end + local tt = values(h) + table.sort(tt) + return tt[#tt] +end + +if consecutiveone({0, 1, 1, 0, 1, 1, 1}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if consecutiveone({0, 0, 0, 0}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if consecutiveone({1, 0, 1, 0, 1, 1}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-325/roger-bell-west/lua/ch-2.lua b/challenge-325/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..04f54172b1 --- /dev/null +++ b/challenge-325/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,67 @@ +#! /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 finalprice(a) + local out = {} + for i, n in ipairs(a) do + local discount = 0 + for mi = i + 1, #a do + if a[mi] <= n then + discount = a[mi] + break + end + end + table.insert(out, n - discount) + end + return out +end + +if recursive_compare(finalprice({8, 4, 6, 2, 3}), {4, 2, 4, 2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(finalprice({1, 2, 3, 4, 5}), {1, 2, 3, 4, 5}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(finalprice({7, 1, 1, 5}), {6, 0, 1, 5}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-325/roger-bell-west/perl/ch-1.pl b/challenge-325/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..403997bd7a --- /dev/null +++ b/challenge-325/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,33 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(consecutiveone([0, 1, 1, 0, 1, 1, 1]), 3, 'example 1'); +is(consecutiveone([0, 0, 0, 0]), 0, 'example 2'); +is(consecutiveone([1, 0, 1, 0, 1, 1]), 2, 'example 3'); + +use List::Util qw(max); + +sub consecutiveone($a) { + my %h = (0, 0); + my $latch = 0; + my $latched = 0; + while (my ($i, $n) = each @{$a}) { + if ($n == 1 && !$latched) { + $latched = 1; + $latch = $i; + } + if ($n == 0 && $latched) { + $latched = 0; + $h{$latch} = $i - $latch; + } + } + if ($latched) { + $h{$latch} = scalar @{$a} - $latch; + } + max(values(%h)); +} diff --git a/challenge-325/roger-bell-west/perl/ch-2.pl b/challenge-325/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..451bc1ce35 --- /dev/null +++ b/challenge-325/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 => 3; + +is_deeply(finalprice([8, 4, 6, 2, 3]), [4, 2, 4, 2, 3], 'example 1'); +is_deeply(finalprice([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5], 'example 2'); +is_deeply(finalprice([7, 1, 1, 5]), [6, 0, 1, 5], 'example 3'); + +sub finalprice($a) { + my @out; + while (my ($i, $n) = each @{$a}) { + my $discount = 0; + foreach my $mi ($i + 1 .. scalar @{$a} - 1) { + if ($a->[$mi] <= $n) { + $discount = $a->[$mi]; + last; + } + } + push @out, $n - $discount; + } + \@out; +} diff --git a/challenge-325/roger-bell-west/postscript/ch-1.ps b/challenge-325/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..e20c48344c --- /dev/null +++ b/challenge-325/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,105 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/listmax { + { max } reduce +} bind def + +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + end +} bind def + +/values { % dict -> array of dict values + [ exch + { + exch pop + } 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 { + /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 + +/consecutiveone { + 0 dict begin + /a exch def + /h 0 dict def + h 0 0 put + /latch 0 def + /latched false def + a enumerate.array { + aload pop + /n exch def + /i exch def + n 1 eq latched not and { + /latched true def + /latch i def + } if + n 0 eq latched and { + /latched false def + h latch i latch sub put + } if + } forall + latched { + h latch a length latch sub put + } if + h values listmax + end +} bind def + +(consecutiveone) test.start +[0 1 1 0 1 1 1] consecutiveone 3 eq test +[0 0 0 0] consecutiveone 0 eq test +[1 0 1 0 1 1] consecutiveone 2 eq test +test.end diff --git a/challenge-325/roger-bell-west/postscript/ch-2.ps b/challenge-325/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..1bb49341ef --- /dev/null +++ b/challenge-325/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,137 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/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 + + +% end included library code + +/finalprice { + 0 dict begin + /a exch def + [ + a enumerate.array { + aload pop + /n exch def + /i exch def + 0 + i 1 add 1 a length 1 sub { + /mi exch def + a mi get + dup + n le { + exch pop + exit + } if + pop + } for + n exch sub + } forall + ] + end +} bind def + +(finalprice) test.start +[8 4 6 2 3] finalprice [4 2 4 2 3] deepeq test +[1 2 3 4 5] finalprice [1 2 3 4 5] deepeq test +[7 1 1 5] finalprice [6 0 1 5] deepeq test +test.end diff --git a/challenge-325/roger-bell-west/python/ch-1.py b/challenge-325/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..87cc81ae31 --- /dev/null +++ b/challenge-325/roger-bell-west/python/ch-1.py @@ -0,0 +1,32 @@ +#! /usr/bin/python3 + +def consecutiveone(a): + h = dict() + h[0] = 0 + latch = 0 + latched = False + for i, n in enumerate(a): + if n == 1 and not latched: + latched = True + latch = i + if n == 0 and latched: + latched = False + h[latch] = i - latch + if latched: + h[latch] = len(a) - latch + return max(h.values()) + +import unittest + +class TestConsecutiveone(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(consecutiveone([0, 1, 1, 0, 1, 1, 1]), 3, 'example 1') + + def test_ex2(self): + self.assertEqual(consecutiveone([0, 0, 0, 0]), 0, 'example 2') + + def test_ex3(self): + self.assertEqual(consecutiveone([1, 0, 1, 0, 1, 1]), 2, 'example 3') + +unittest.main() diff --git a/challenge-325/roger-bell-west/python/ch-2.py b/challenge-325/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..9a6a8f82d2 --- /dev/null +++ b/challenge-325/roger-bell-west/python/ch-2.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +def finalprice(a): + out = [] + for i, n in enumerate(a): + discount = 0 + for mi in range(i + 1, len(a)): + if a[mi] <= n: + discount = a[mi] + break + out.append(n - discount) + return out + +import unittest + +class TestFinalprice(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(finalprice([8, 4, 6, 2, 3]), [4, 2, 4, 2, 3], 'example 1') + + def test_ex2(self): + self.assertEqual(finalprice([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5], 'example 2') + + def test_ex3(self): + self.assertEqual(finalprice([7, 1, 1, 5]), [6, 0, 1, 5], 'example 3') + +unittest.main() diff --git a/challenge-325/roger-bell-west/raku/ch-1.p6 b/challenge-325/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..9e4d7b9b07 --- /dev/null +++ b/challenge-325/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,30 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(consecutiveone([0, 1, 1, 0, 1, 1, 1]), 3, 'example 1'); +is(consecutiveone([0, 0, 0, 0]), 0, 'example 2'); +is(consecutiveone([1, 0, 1, 0, 1, 1]), 2, 'example 3'); + +sub consecutiveone(@a) { + my %h; + %h{0} = 0; + my $latch = 0; + my $latched = False; + for @a.kv -> $i, $n { + if ($n == 1 && !$latched) { + $latched = True; + $latch = $i; + } + if ($n == 0 && $latched) { + $latched = False; + %h{$latch} = $i - $latch; + } + } + if ($latched) { + %h{$latch} = @a.elems - $latch; + } + %h.values().max(); +} diff --git a/challenge-325/roger-bell-west/raku/ch-2.p6 b/challenge-325/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..8ba366b153 --- /dev/null +++ b/challenge-325/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,24 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(finalprice([8, 4, 6, 2, 3]), [4, 2, 4, 2, 3], 'example 1'); +is-deeply(finalprice([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5], 'example 2'); +is-deeply(finalprice([7, 1, 1, 5]), [6, 0, 1, 5], 'example 3'); + +sub finalprice(@a) { + my @out; + for @a.kv -> $i, $n { + my $discount = 0; + for $i + 1 .. @a.end -> $mi { + if (@a[$mi] <= $n) { + $discount = @a[$mi]; + last; + } + } + @out.push($n - $discount); + } + @out; +} diff --git a/challenge-325/roger-bell-west/ruby/ch-1.rb b/challenge-325/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..1d61023c90 --- /dev/null +++ b/challenge-325/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,40 @@ +#! /usr/bin/ruby + +def consecutiveone(a) + h = Hash.new + h[0] = 0 + latch = 0 + latched = false + a.each_with_index do |n, i| + if n == 1 && !latched + latched = true + latch = i + end + if n == 0 && latched + latched = false + h[latch] = i - latch + end + end + if latched + h[latch] = a.length - latch + end + h.values.max +end + +require 'test/unit' + +class TestConsecutiveone < Test::Unit::TestCase + + def test_ex1 + assert_equal(3, consecutiveone([0, 1, 1, 0, 1, 1, 1])) + end + + def test_ex2 + assert_equal(0, consecutiveone([0, 0, 0, 0])) + end + + def test_ex3 + assert_equal(2, consecutiveone([1, 0, 1, 0, 1, 1])) + end + +end diff --git a/challenge-325/roger-bell-west/ruby/ch-2.rb b/challenge-325/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..29d7add4de --- /dev/null +++ b/challenge-325/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,34 @@ +#! /usr/bin/ruby + +def finalprice(a) + out = [] + a.each_with_index do |n, i| + discount = 0 + (i + 1).upto(a.length - 1) do |mi| + if a[mi] <= n + discount = a[mi] + break + end + end + out.push(n - discount) + end + out +end + +require 'test/unit' + +class TestFinalprice < Test::Unit::TestCase + + def test_ex1 + assert_equal([4, 2, 4, 2, 3], finalprice([8, 4, 6, 2, 3])) + end + + def test_ex2 + assert_equal([1, 2, 3, 4, 5], finalprice([1, 2, 3, 4, 5])) + end + + def test_ex3 + assert_equal([6, 0, 1, 5], finalprice([7, 1, 1, 5])) + end + +end diff --git a/challenge-325/roger-bell-west/rust/ch-1.rs b/challenge-325/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..a43c578deb --- /dev/null +++ b/challenge-325/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,40 @@ +#! /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!(consecutiveone(vec![0, 1, 1, 0, 1, 1, 1]), 3); +} + +#[test] +fn test_ex2() { + assert_eq!(consecutiveone(vec![0, 0, 0, 0]), 0); +} + +#[test] +fn test_ex3() { + assert_eq!(consecutiveone(vec![1, 0, 1, 0, 1, 1]), 2); +} + +fn consecutiveone(a: Vec) -> usize { + let mut h = HashMap::new(); + h.insert(0, 0); + let mut latch = 0; + let mut latched = false; + for (i, n) in a.iter().enumerate() { + if *n == 1 && !latched { + latched = true; + latch = i; + } + if *n == 0 && latched { + latched = false; + h.insert(latch, i - latch); + } + } + if latched { + h.insert(latch, a.len() - latch); + } + *h.values().max().unwrap() +} diff --git a/challenge-325/roger-bell-west/rust/ch-2.rs b/challenge-325/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..8e81e56e8b --- /dev/null +++ b/challenge-325/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,32 @@ +#! /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!(finalprice(vec![8, 4, 6, 2, 3]), vec![4, 2, 4, 2, 3]); +} + +#[test] +fn test_ex2() { + assert_eq!(finalprice(vec![1, 2, 3, 4, 5]), vec![1, 2, 3, 4, 5]); +} + +#[test] +fn test_ex3() { + assert_eq!(finalprice(vec![7, 1, 1, 5]), vec![6, 0, 1, 5]); +} + +fn finalprice(a: Vec) -> Vec { + let mut out = Vec::new(); + for (i, n) in a.iter().enumerate() { + let mut discount = 0; + for m in a.iter().skip(i + 1) { + if m <= n { + discount = *m; + break; + } + } + out.push(*n - discount); + } + out +} diff --git a/challenge-325/roger-bell-west/scala/ch-1.scala b/challenge-325/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..215976990e --- /dev/null +++ b/challenge-325/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,45 @@ +import scala.collection.mutable + +object Consecutiveone { + def consecutiveone(a: List[Int]): Int = { + var h = mutable.Map.empty[Int, Int] + h += ((0, 0)) + var latch = 0 + var latched = false + for ((n, i) <- a.zipWithIndex) { + if (n == 1 && !latched) { + latched = true + latch = i + } + if (n == 0 && latched) { + latched = false + h += ((latch, i - latch)) + } + } + if (latched) { + h += ((latch, a.size - latch)) + } + h.values.max + } + def main(args: Array[String]) { + if (consecutiveone(List(0, 1, 1, 0, 1, 1, 1)) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (consecutiveone(List(0, 0, 0, 0)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (consecutiveone(List(1, 0, 1, 0, 1, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-325/roger-bell-west/scala/ch-2.scala b/challenge-325/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..0018128bc9 --- /dev/null +++ b/challenge-325/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,47 @@ +import scala.collection.mutable.ListBuffer + +object Finalprice { + def finalprice(a: List[Int]): List[Int] = { + var out = new ListBuffer[Int] + for ((n, i) <- a.zipWithIndex) { + var discount = 0 + var mi = i + 1 + if (mi < a.size) { + var lp = true + while (lp) { + if (a(mi) <= n) { + discount = a(mi) + lp = false + } + mi += 1 + if (mi >= a.size) { + lp = false + } + } + } + out += (n - discount) + } + out.toList + } + def main(args: Array[String]) { + if (finalprice(List(8, 4, 6, 2, 3)) == List(4, 2, 4, 2, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (finalprice(List(1, 2, 3, 4, 5)) == List(1, 2, 3, 4, 5)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (finalprice(List(7, 1, 1, 5)) == List(6, 0, 1, 5)) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-325/roger-bell-west/tests.json b/challenge-325/roger-bell-west/tests.json new file mode 100644 index 0000000000..189b575a8b --- /dev/null +++ b/challenge-325/roger-bell-west/tests.json @@ -0,0 +1,32 @@ +{ + "ch-1" : [ + { + "function" : "consecutiveone", + "arguments" : [ 0, 1, 1, 0, 1, 1, 1 ], + "result" : 3 + }, + { + "arguments" : [ 0, 0, 0, 0 ], + "result" : 0 + }, + { + "arguments" : [ 1, 0, 1, 0, 1, 1 ], + "result" : 2 + } + ], + "ch-2" : [ + { + "function" : "finalprice", + "arguments" : [ 8, 4, 6, 2, 3 ], + "result" : [ 4, 2, 4, 2, 3 ] + }, + { + "arguments" : [ 1, 2, 3, 4, 5 ], + "result" : [ 1, 2, 3, 4, 5 ] + }, + { + "arguments" : [ 7, 1, 1, 5 ], + "result" : [ 6, 0, 1, 5 ] + } + ] +} diff --git a/challenge-325/roger-bell-west/typst/ch-1.typ b/challenge-325/roger-bell-west/typst/ch-1.typ new file mode 100644 index 0000000000..ef65d8f279 --- /dev/null +++ b/challenge-325/roger-bell-west/typst/ch-1.typ @@ -0,0 +1,37 @@ +#let consecutiveone(a) = { + let h = ("0": 0) + let latch = 0 + let latched = false + for (i, n) in a.enumerate() { + if n == 1 and not latched { + latched = true + latch = i + } + if n == 0 and latched { + latched = false + h.insert(str(latch), i - latch) + } + } + if latched { + h.insert(str(latch), a.len() - latch) + } + calc.max(.. h.values()) +} + +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +Test 1: + #testresult(consecutiveone((0, 1, 1, 0, 1, 1, 1)) == 3) + +Test 2: + #testresult(consecutiveone((0, 0, 0, 0)) == 0) + +Test 3: + #testresult(consecutiveone((1, 0, 1, 0, 1, 1)) == 2) + diff --git a/challenge-325/roger-bell-west/typst/ch-2.typ b/challenge-325/roger-bell-west/typst/ch-2.typ new file mode 100644 index 0000000000..0db5fdb61f --- /dev/null +++ b/challenge-325/roger-bell-west/typst/ch-2.typ @@ -0,0 +1,32 @@ +#let finalprice(a) = { + let out = () + for (i, n) in a.enumerate() { + let discount = 0 + for mi in range(i + 1, a.len()) { + if a.at(mi) <= n { + discount = a.at(mi) + break + } + } + out.push(n - discount) + } + out +} + +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +Test 1: + #testresult(finalprice((8, 4, 6, 2, 3)) == (4, 2, 4, 2, 3)) + +Test 2: + #testresult(finalprice((1, 2, 3, 4, 5)) == (1, 2, 3, 4, 5)) + +Test 3: + #testresult(finalprice((7, 1, 1, 5)) == (6, 0, 1, 5)) + -- cgit