diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-20 14:41:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-20 14:41:13 +0100 |
| commit | fe6fcae9cf6a18b1fdcb2067432e48164076a39a (patch) | |
| tree | ae63151ac4f72040ea957fe76d127522326a0161 | |
| parent | 915a726318ccc10377273565e0940be2f2427218 (diff) | |
| parent | ffdd725a4bb663027e35ba2eed3448851e9bb122 (diff) | |
| download | perlweeklychallenge-club-fe6fcae9cf6a18b1fdcb2067432e48164076a39a.tar.gz perlweeklychallenge-club-fe6fcae9cf6a18b1fdcb2067432e48164076a39a.tar.bz2 perlweeklychallenge-club-fe6fcae9cf6a18b1fdcb2067432e48164076a39a.zip | |
Merge pull request #8736 from Firedrake/rogerbw-challenge-235
RogerBW solutions for challenge no. 235
21 files changed, 915 insertions, 0 deletions
diff --git a/challenge-235/roger-bell-west/javascript/ch-1.js b/challenge-235/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..bbfae8ae09 --- /dev/null +++ b/challenge-235/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,46 @@ +#! /usr/bin/node + +"use strict" + +// by VLAZ +// https://stackoverflow.com/a/59322890 +function toWindows(inputArray, size) { + return Array.from( + {length: inputArray.length - (size - 1)}, //get the appropriate length + (_, index) => inputArray.slice(index, index+size) //create the windows + ) +} + +function removeone(a) { + let ec = 0; + let le = 1 + a[1] - a[0]; + for (let s of toWindows(a, 2)) { + if (s[1] <= s[0]) { + ec++; + if (ec > 1 || s[0] - s[1] >= le) { + return false; + } + } + le = s[1] - s[0]; + } + return true; +} + +if (removeone([0, 2, 9, 4, 6])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!removeone([5, 1, 3, 2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (removeone([2, 2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-235/roger-bell-west/javascript/ch-2.js b/challenge-235/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..2ccebafd92 --- /dev/null +++ b/challenge-235/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,66 @@ +#! /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 duplicatezeros(a) { + let out = []; + for (let t of a) { + out.push(t); + if (t == 0) { + out.push(t); + } + if (out.length >= a.length) { + break; + } + } + if (out.length > a.length) { + out.length = a.length; + } + return out; +} + +if (deepEqual(duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]), [1, 0, 0, 2, 3, 0, 0, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(duplicatezeros([1, 2, 3]), [1, 2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(duplicatezeros([0, 3, 0, 4, 5]), [0, 0, 3, 0, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-235/roger-bell-west/kotlin/ch-1.kt b/challenge-235/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..fa5b415886 --- /dev/null +++ b/challenge-235/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,37 @@ +fun removeone(a: List<Int>): Boolean { + var ec = 0 + var le = 1 + a[1] - a[0] + for (s in a.windowed(size = 2)) { + if (s[1] <= s[0]) { + ec += 1 + if (ec > 1 || s[0] - s[1] >= le) { + return false + } + } + le = s[1] - s[0] + } + return true +} + +fun main() { + + if (removeone(listOf(0, 2, 9, 4, 6))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!removeone(listOf(5, 1, 3, 2))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (removeone(listOf(2, 2, 3))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-235/roger-bell-west/kotlin/ch-2.kt b/challenge-235/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..e340e7c6e9 --- /dev/null +++ b/challenge-235/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,39 @@ +fun duplicatezeros(a: List<Int>): List<Int> { + var out = ArrayList<Int>() + for (t in a) { + out.add(t) + if (t == 0) { + out.add(t) + } + if (out.size >= a.size) { + break + } + } + if (out.size > a.size) { + out = ArrayList(out.dropLast(out.size - a.size)) + } + return out.toList() +} + +fun main() { + + if (duplicatezeros(listOf(1, 0, 2, 3, 0, 4, 5, 0)) == listOf(1, 0, 0, 2, 3, 0, 0, 4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(listOf(1, 2, 3)) == listOf(1, 2, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(listOf(0, 3, 0, 4, 5)) == listOf(0, 0, 3, 0, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-235/roger-bell-west/lua/ch-1.lua b/challenge-235/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..1d352b8c3d --- /dev/null +++ b/challenge-235/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,41 @@ +#! /usr/bin/lua + +function removeone(a) + local ec = 0 + local le = 1 + a[2] - a[1] + for i, s0 in ipairs(a) do + if i < #a then + local s1 = a[i + 1] + if s1 <= s0 then + ec = ec + 1 + if ec > 1 or (s0 - s1 >= le) then + return false + end + end + le = s1 - s0 + end + end + return true +end + +if removeone({0, 2, 9, 4, 6}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not removeone({5, 1, 3, 2}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if removeone({2, 2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-235/roger-bell-west/lua/ch-2.lua b/challenge-235/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..c3b26f4a4c --- /dev/null +++ b/challenge-235/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,69 @@ +#! /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 duplicatezeros(a) + local out = {} + for i, t in ipairs(a) do + table.insert(out, t) + if t == 0 then + table.insert(out, t) + end + if #out >= #a then + break + end + end + while #out > #a do + table.remove(out) + end + return out +end + +if recursive_compare(duplicatezeros({1, 0, 2, 3, 0, 4, 5, 0}), {1, 0, 0, 2, 3, 0, 0, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(duplicatezeros({1, 2, 3}), {1, 2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(duplicatezeros({0, 3, 0, 4, 5}), {0, 0, 3, 0, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-235/roger-bell-west/perl/ch-1.pl b/challenge-235/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..d1e42d64a6 --- /dev/null +++ b/challenge-235/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,27 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(removeone([0, 2, 9, 4, 6]), 1, 'example 1'); +is(removeone([5, 1, 3, 2]), 0, 'example 2'); +is(removeone([2, 2, 3]), 1, 'example 3'); + +sub removeone($a) { + my $ec = 0; + my $le = 1 + $a->[1] - $a->[0]; + foreach my $i (0..$#{$a}-1) { + my @s = ($a->[$i], $a->[$i+1]); + if ($s[1] <= $s[0]) { + $ec++; + if ($ec > 1 || $s[0] - $s[1] >= $le) { + return 0; + } + } + $le = $s[1] - $s[0]; + } + return 1; +} diff --git a/challenge-235/roger-bell-west/perl/ch-2.pl b/challenge-235/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..058e919f05 --- /dev/null +++ b/challenge-235/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,28 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]), [1, 0, 0, 2, 3, 0, 0, 4], 'example 1'); +is_deeply(duplicatezeros([1, 2, 3]), [1, 2, 3], 'example 2'); +is_deeply(duplicatezeros([0, 3, 0, 4, 5]), [0, 0, 3, 0, 0], 'example 3'); + +sub duplicatezeros($a) { + my @out; + foreach my $t (@{$a}) { + push @out, $t; + if ($t == 0) { + push @out, $t; + } + if ($#out >= $#{$a}) { + last; + } + } + if ($#out > $#{$a}) { + splice @out, scalar @{$a}; + } + return \@out; +} diff --git a/challenge-235/roger-bell-west/postscript/ch-1.ps b/challenge-235/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..cd1c908045 --- /dev/null +++ b/challenge-235/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,63 @@ +%!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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/removeone { + 0 dict begin + /a exch def + /ec 0 def + /lx 0 def + true + 0 1 a length 2 sub { + a exch 2 getinterval /s exch def + s 1 get s 0 get le { + /ec ec 1 add def + ec 1 gt s 0 get s 1 get sub lx ge or { + pop false + exit + } if + } if + /lx s 1 get s 0 get sub def + } for + end +} bind def + +(removeone) test.start +[0 2 9 4 6] removeone test +[5 1 3 2] removeone not test +[2 2 2] removeone not test +test.end diff --git a/challenge-235/roger-bell-west/postscript/ch-2.ps b/challenge-235/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..aa8499f97a --- /dev/null +++ b/challenge-235/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,120 @@ +%!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 + +/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 + +/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 + + +% end included library code + +/duplicatezeros { + 0 dict begin + /a exch def + /out + [ + a { + dup + 0 eq { + dup + } if + } forall + ] def + out length a length gt { + /out out 0 a length getinterval def + } if + out + end +} bind def + +(duplicatezeros) test.start +[1 0 2 3 0 4 5 0] duplicatezeros [1 0 0 2 3 0 0 4] deepeq test +[1 2 3] duplicatezeros [1 2 3] deepeq test +[0 3 0 4 5] duplicatezeros [0 0 3 0 0] deepeq test +test.end diff --git a/challenge-235/roger-bell-west/python/ch-1.py b/challenge-235/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..3b060b3899 --- /dev/null +++ b/challenge-235/roger-bell-west/python/ch-1.py @@ -0,0 +1,29 @@ +#! /usr/bin/python3 + +from itertools import pairwise + +def removeone(a): + ec = 0 + le = 1 + a[1] - a[0] + for s in pairwise(a): + if s[1] <= s[0]: + ec += 1 + if ec > 1 or s[0] - s[1] >= le: + return False + le = s[1] - s[0] + return True + +import unittest + +class TestRemoveone(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(removeone([0, 2, 9, 4, 6]), True, 'example 1') + + def test_ex2(self): + self.assertEqual(removeone([5, 1, 3, 2]), False, 'example 2') + + def test_ex3(self): + self.assertEqual(removeone([2, 2, 3]), True, 'example 3') + +unittest.main() diff --git a/challenge-235/roger-bell-west/python/ch-2.py b/challenge-235/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..e5f1bc39fc --- /dev/null +++ b/challenge-235/roger-bell-west/python/ch-2.py @@ -0,0 +1,29 @@ +#! /usr/bin/python3 + +def duplicatezeros(a): + out = [] + for t in a: + out.append(t) + if t == 0: + out.append(t) + if len(out) >= len(a): + break + if len(out) > len(a): + out = out[0:len(a)] + return out + + +import unittest + +class TestDuplicatezeros(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]), [1, 0, 0, 2, 3, 0, 0, 4], 'example 1') + + def test_ex2(self): + self.assertEqual(duplicatezeros([1, 2, 3]), [1, 2, 3], 'example 2') + + def test_ex3(self): + self.assertEqual(duplicatezeros([0, 3, 0, 4, 5]), [0, 0, 3, 0, 0], 'example 3') + +unittest.main() diff --git a/challenge-235/roger-bell-west/raku/ch-1.p6 b/challenge-235/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..0f657a401d --- /dev/null +++ b/challenge-235/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,24 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(removeone([0, 2, 9, 4, 6]), True, 'example 1'); +is(removeone([5, 1, 3, 2]), False, 'example 2'); +is(removeone([2, 2, 3]), True, 'example 3'); + +sub removeone(@a) { + my $ec = 0; + my $le = 1 + @a[1] - @a[0]; + for @a.rotor(2 => -1) -> @s { + if (@s[1] <= @s[0]) { + $ec++; + if ($ec > 1 || @s[0] - @s[1] >= $le) { + return False; + } + } + $le = @s[1] - @s[0]; + } + return True; +} diff --git a/challenge-235/roger-bell-west/raku/ch-2.p6 b/challenge-235/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..fa555a4e79 --- /dev/null +++ b/challenge-235/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]), [1, 0, 0, 2, 3, 0, 0, 4], 'example 1'); +is-deeply(duplicatezeros([1, 2, 3]), [1, 2, 3], 'example 2'); +is-deeply(duplicatezeros([0, 3, 0, 4, 5]), [0, 0, 3, 0, 0], 'example 3'); + +sub duplicatezeros(@a) { + my @out; + for @a -> $t { + @out.push($t); + if ($t == 0) { + @out.push($t) + } + if (@out.elems >= @a.elems) { + last; + } + } + if (@out.elems > @a.elems) { + @out.splice(@a.elems); + } + return @out; +} diff --git a/challenge-235/roger-bell-west/ruby/ch-1.rb b/challenge-235/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..b14816ca1f --- /dev/null +++ b/challenge-235/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,34 @@ +#! /usr/bin/ruby + +def removeone(a) + ec = 0 + le = 1 + a[1] - a[0] + a.each_cons(2) do |s| + if s[1] <= s[0] then + ec += 1 + if ec > 1 || s[0] - s[1] >= le then + return false + end + end + le = s[1] - s[0] + end + return true +end + +require 'test/unit' + +class TestRemoveone < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, removeone([0, 2, 9, 4, 6])) + end + + def test_ex2 + assert_equal(false, removeone([5, 1, 3, 2])) + end + + def test_ex3 + assert_equal(true, removeone([2, 2, 3])) + end + +end diff --git a/challenge-235/roger-bell-west/ruby/ch-2.rb b/challenge-235/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..7c3f88b4fd --- /dev/null +++ b/challenge-235/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,33 @@ +#! /usr/bin/ruby + +def duplicatezeros(a) + out = [] + a.each do |t| + out.push(t) + if t == 0 then + out.push(t) + end + if out.length >= a.length then + break + end + end + return out[0, a.length] +end + +require 'test/unit' + +class TestDuplicatezeros < Test::Unit::TestCase + + def test_ex1 + assert_equal([1, 0, 0, 2, 3, 0, 0, 4], duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0])) + end + + def test_ex2 + assert_equal([1, 2, 3], duplicatezeros([1, 2, 3])) + end + + def test_ex3 + assert_equal([0, 0, 3, 0, 0], duplicatezeros([0, 3, 0, 4, 5])) + end + +end diff --git a/challenge-235/roger-bell-west/rust/ch-1.rs b/challenge-235/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..cfa39d5082 --- /dev/null +++ b/challenge-235/roger-bell-west/rust/ch-1.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!(removeone(vec![0, 2, 9, 4, 6]), true); +} + +#[test] +fn test_ex2() { + assert_eq!(removeone(vec![5, 1, 3, 2]), false); +} + +#[test] +fn test_ex3() { + assert_eq!(removeone(vec![2, 2, 3]), true); +} + +fn removeone(a: Vec<i64>) -> bool { + let mut ec = 0u32; + let mut le = 1 + a[1] - a[0]; + for s in a.windows(2) { + if s[1] <= s[0] { + ec += 1; + if ec > 1 || s[0] - s[1] >= le { + return false; + } + } + le = s[1] - s[0]; + } + true +} diff --git a/challenge-235/roger-bell-west/rust/ch-2.rs b/challenge-235/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..85048d5ed9 --- /dev/null +++ b/challenge-235/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,35 @@ +#! /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!( + duplicatezeros(vec![1, 0, 2, 3, 0, 4, 5, 0]), + vec![1, 0, 0, 2, 3, 0, 0, 4] + ); +} + +#[test] +fn test_ex2() { + assert_eq!(duplicatezeros(vec![1, 2, 3]), vec![1, 2, 3]); +} + +#[test] +fn test_ex3() { + assert_eq!(duplicatezeros(vec![0, 3, 0, 4, 5]), vec![0, 0, 3, 0, 0]); +} + +fn duplicatezeros(a: Vec<u32>) -> Vec<u32> { + let mut out = Vec::new(); + for t in &a { + out.push(*t); + if *t == 0 { + out.push(*t); + } + if out.len() >= a.len() { + break; + } + } + out.resize(a.len(), out[0]); + out +} diff --git a/challenge-235/roger-bell-west/scala/ch-1.scala b/challenge-235/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..b244ec15ec --- /dev/null +++ b/challenge-235/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,38 @@ +object Removeone { + def removeone(a: List[Int]): Boolean = { + var ec = 0 + var le = 1 + a(1) - a(0) + for (s <- a.sliding(2)) { + if (s(1) <= s(0)) { + ec += 1 + if (ec > 1 || s(0) - s(1) >= le) { + return false + } + } + le = s(1) - s(0) + } + return true + } + + def main(args: Array[String]) { + if (removeone(List(0, 2, 9, 4, 6))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!removeone(List(5, 1, 3, 2))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (removeone(List(2, 2, 3))) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-235/roger-bell-west/scala/ch-2.scala b/challenge-235/roger-bell-west/scala/ch-2.scala new file mode 100755 index 0000000000..2cebbe97ad --- /dev/null +++ b/challenge-235/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,38 @@ +import scala.collection.mutable.ListBuffer + +object Duplicatezeros { + def duplicatezeros(a: List[Int]): List[Int] = { + var out = new ListBuffer[Int] + for (t <- a) { + out += t + if (t == 0) { + out += t + } + } + if (out.length > a.length) { + out = out.dropRight(out.length - a.length) + } + return out.toList + } + + def main(args: Array[String]) { + if (duplicatezeros(List(1, 0, 2, 3, 0, 4, 5, 0)) == List(1, 0, 0, 2, 3, 0, 0, 4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(List(1, 2, 3)) == List(1, 2, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(List(0, 3, 0, 4, 5)) == List(0, 0, 3, 0, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + } +} diff --git a/challenge-235/roger-bell-west/tests.yaml b/challenge-235/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..ad55ebfae6 --- /dev/null +++ b/challenge-235/roger-bell-west/tests.yaml @@ -0,0 +1,61 @@ +--- +ch-1: + - function: removeone + arguments: + - 0 + - 2 + - 9 + - 4 + - 6 + result: true + - arguments: + - 5 + - 1 + - 3 + - 2 + result: false + - arguments: + - 2 + - 2 + - 3 + result: true +ch-2: + - function: duplicatezeros + arguments: + - 1 + - 0 + - 2 + - 3 + - 0 + - 4 + - 5 + - 0 + result: + - 1 + - 0 + - 0 + - 2 + - 3 + - 0 + - 0 + - 4 + - arguments: + - 1 + - 2 + - 3 + result: + - 1 + - 2 + - 3 + - arguments: + - 0 + - 3 + - 0 + |
