diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-10 18:01:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-10 18:01:50 +0100 |
| commit | b8ff5bd1dc6c6ddb36299de56d74348e8f0b95fd (patch) | |
| tree | 4e0e072c74e5acaf42bdb3066becc97ab6c26b31 | |
| parent | a9f099b09f5cd637d39b54abbd42dd0d49fe497d (diff) | |
| parent | 20ce1cadd1f73bebb9c7ad6490d98b70f82592be (diff) | |
| download | perlweeklychallenge-club-b8ff5bd1dc6c6ddb36299de56d74348e8f0b95fd.tar.gz perlweeklychallenge-club-b8ff5bd1dc6c6ddb36299de56d74348e8f0b95fd.tar.bz2 perlweeklychallenge-club-b8ff5bd1dc6c6ddb36299de56d74348e8f0b95fd.zip | |
Merge pull request #12164 from Firedrake/rogerbw-challenge-325
RogerBW solutions for challenge no. 325
25 files changed, 1125 insertions, 0 deletions
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>): Int { + var h = mutableMapOf<Int, Int>() + 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<Int>): List<Int> { + var out = ArrayList<Int>() + 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] < |
