diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-02-05 00:37:11 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-02-05 00:37:11 +0000 |
| commit | 75b4431f9b5341f7eee34d3e221b91cdfae599f5 (patch) | |
| tree | d5186f979c0f4bda95c77b03bf849a23742f3193 /challenge-202 | |
| parent | bb49ce568aa7451728b149870379bbc8dbf6632d (diff) | |
| parent | de3436acaff3500ff305442118f65ed711106512 (diff) | |
| download | perlweeklychallenge-club-75b4431f9b5341f7eee34d3e221b91cdfae599f5.tar.gz perlweeklychallenge-club-75b4431f9b5341f7eee34d3e221b91cdfae599f5.tar.bz2 perlweeklychallenge-club-75b4431f9b5341f7eee34d3e221b91cdfae599f5.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-202')
19 files changed, 1137 insertions, 0 deletions
diff --git a/challenge-202/roger-bell-west/javascript/ch-1.js b/challenge-202/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..65d6dc79a2 --- /dev/null +++ b/challenge-202/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,43 @@ +#! /usr/bin/node + +"use strict" + +function consecutiveodds(a) { + let i = 0; + for (let v of a) { + if (v % 2 == 1) { + i++; + if (i >= 3) { + return true; + } + } else { + i = 0; + } + } + return false; +} + +if (consecutiveodds([1, 5, 3, 6])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!consecutiveodds([2, 6, 3, 5])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!consecutiveodds([1, 2, 3, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (consecutiveodds([2, 3, 5, 7])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-202/roger-bell-west/javascript/ch-2.js b/challenge-202/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..737ffcebd3 --- /dev/null +++ b/challenge-202/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,93 @@ +#! /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 widestvalley(a) { + let av = []; + let ac = []; + let l = -1; + for (let v of a) { + if (v == l) { + ac[ac.length - 1]++; + } else { + av.push(v); + ac.push(1); + l = v; + } + } + let s = []; + let e = [] + let c = 0; + for (let i = 0; i < av.length; i++) { + if (i == 0 || i == av.length - 1 || (av[i - 1] < av[i] && av[i] > av[i + 1])) { + s.push(c); + e.push(c + ac[i] - 1); + } + c += ac[i]; + } + let out = []; + for (let i = 0; i < s.length - 1; i++) { + if (e[i + 1] - s[i] + 1 > out.length) { + out = a.slice(s[i], e[i + 1] + 1); + } + } + return out; +} + +if (deepEqual(widestvalley([1, 5, 5, 2, 8]), [5, 5, 2, 8])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(widestvalley([2, 6, 8, 5]), [2, 6, 8])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(widestvalley([9, 8, 13, 13, 2, 2, 15, 17]), [13, 13, 2, 2, 15, 17])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(widestvalley([2, 1, 2, 1, 3]), [2, 1, 2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(widestvalley([1, 3, 3, 2, 1, 2, 3, 3, 2]), [3, 3, 2, 1, 2, 3, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-202/roger-bell-west/kotlin/ch-1.kt b/challenge-202/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..84eaf2418f --- /dev/null +++ b/challenge-202/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,43 @@ +fun consecutiveodds(a: List<Int>): Boolean { + var i = 0 + for (v in a) { + if (v % 2 == 1) { + i += 1 + if (i >= 3) { + return true + } + } else { + i = 0 + } + } + return false +} + +fun main() { + + if (consecutiveodds(listOf(1, 5, 3, 6))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!consecutiveodds(listOf(2, 6, 3, 5))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!consecutiveodds(listOf(1, 2, 3, 4))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (consecutiveodds(listOf(2, 3, 5, 7))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-202/roger-bell-west/kotlin/ch-2.kt b/challenge-202/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..edb5ba31cc --- /dev/null +++ b/challenge-202/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,66 @@ +fun widestvalley(a: List<Int>): List<Int> { + var av = ArrayList<Int>() + var ac = ArrayList<Int>() + var l = -1 + for (v in a) { + if (v == l) { + ac[ac.size - 1] += 1 + } else { + av.add(v) + ac.add(1) + l = v + } + } + var s = ArrayList<Int>() + var e = ArrayList<Int>() + var c = 0 + for (i in 0 .. av.size-1) { + if (i == 0 || i == av.size - 1 || (av[i - 1] < av[i] && av[i] > av[i + 1])) { + s.add(c) + e.add(c + ac[i] - 1) + } + c += ac[i] + } + var out = emptyList<Int>() + for (i in 0 .. s.size - 2) { + if (e[i + 1] - s[i] + 1 > out.size) { + out = a.slice(s[i] .. e[i + 1]).toList() + } + } + return out +} + +fun main() { + + if (widestvalley(listOf(1, 5, 5, 2, 8)) == listOf(5, 5, 2, 8)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (widestvalley(listOf(2, 6, 8, 5)) == listOf(2, 6, 8)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (widestvalley(listOf(9, 8, 13, 13, 2, 2, 15, 17)) == listOf(13, 13, 2, 2, 15, 17)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (widestvalley(listOf(2, 1, 2, 1, 3)) == listOf(2, 1, 2)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (widestvalley(listOf(1, 3, 3, 2, 1, 2, 3, 3, 2)) == listOf(3, 3, 2, 1, 2, 3, 3)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-202/roger-bell-west/lua/ch-1.lua b/challenge-202/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..a90926276f --- /dev/null +++ b/challenge-202/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,45 @@ +#! /usr/bin/lua + +function consecutiveodds(a) + local i = 0 + for _dummy,v in ipairs(a) do + if v % 2 == 1 then + i = i + 1 + if i >= 3 then + return true + end + else + i = 0 + end + end + return 0 +end + +if consecutiveodds({1, 5, 3, 6}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if ~consecutiveodds({2, 6, 3, 5}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if ~consecutiveodds({1, 2, 3, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if consecutiveodds({2, 3, 5, 7}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-202/roger-bell-west/lua/ch-2.lua b/challenge-202/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..830adac8d2 --- /dev/null +++ b/challenge-202/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,103 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +function recursive_compare(t1,t2) + if t1==t2 then return true end + if (type(t1)~="table") then return false end + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + return true +end + +function widestvalley(a) + local av = {} + local ac = {} + local l = -1 + for i,v in ipairs(a) do + if v == l then + ac[#ac] = ac[#ac] + 1 + else + table.insert(av, v) + table.insert(ac, 1) + l = v + end + end + local s = {} + local e = {} + local c = 1 + for i,v in ipairs(av) do + local peak = false + if i == 1 or i == #av then + peak = true + end + if not peak then + if (av[i - 1] < v and v > av[i + 1]) then + peak = true + end + end + if peak then + table.insert(s, c) + table.insert(e, c + ac[i] - 1) + end + c = c + ac[i] + end + local out = {} + for i,v in ipairs(s) do + if i == #s then + break + end + if e[i + 1] - s[i] + 1 > #out then + out = {} + for j = s[i], e[i+1] do + table.insert(out, a[j]) + end + end + end + return out +end + +if recursive_compare(widestvalley({1, 5, 5, 2, 8}), {5, 5, 2, 8}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(widestvalley({2, 6, 8, 5}), {2, 6, 8}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(widestvalley({9, 8, 13, 13, 2, 2, 15, 17}), {13, 13, 2, 2, 15, 17}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(widestvalley({2, 1, 2, 1, 3}), {2, 1, 2}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(widestvalley({1, 3, 3, 2, 1, 2, 3, 3, 2}), {3, 3, 2, 1, 2, 3, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-202/roger-bell-west/perl/ch-1.pl b/challenge-202/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..6add302b31 --- /dev/null +++ b/challenge-202/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 => 4; + +is(consecutiveodds([1, 5, 3, 6]), 1, 'example 1'); +is(consecutiveodds([2, 6, 3, 5]), 0, 'example 2'); +is(consecutiveodds([1, 2, 3, 4]), 0, 'example 3'); +is(consecutiveodds([2, 3, 5, 7]), 1, 'example 4'); + +sub consecutiveodds($a) { + my $i = 0; + foreach my $v (@{$a}) { + if ($v % 2 == 1) { + $i++; + if ($i >= 3) { + return 1; + } + } else { + $i = 0; + } + } + return 0; +} diff --git a/challenge-202/roger-bell-west/perl/ch-2.pl b/challenge-202/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..b81795010b --- /dev/null +++ b/challenge-202/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,45 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is_deeply(widestvalley([1, 5, 5, 2, 8]), [5, 5, 2, 8], 'example 1'); +is_deeply(widestvalley([2, 6, 8, 5]), [2, 6, 8], 'example 2'); +is_deeply(widestvalley([9, 8, 13, 13, 2, 2, 15, 17]), [13, 13, 2, 2, 15, 17], 'example 3'); +is_deeply(widestvalley([2, 1, 2, 1, 3]), [2, 1, 2], 'example 4'); +is_deeply(widestvalley([1, 3, 3, 2, 1, 2, 3, 3, 2]), [3, 3, 2, 1, 2, 3, 3], 'example 5'); + +sub widestvalley($a) { + my @av; + my @ac; + my $l = -1; + foreach my $v (@{$a}) { + if ($v == $l) { + $ac[-1]++; + } else { + push @av, $v; + push @ac, 1; + $l = $v; + } + } + my @s; + my @e; + my $c = 0; + foreach my $i (0..$#av) { + if ($i == 0 || $i == $#av || ($av[$i - 1] < $av[$i] && $av[$i] > $av[$i + 1])) { + push @s, $c; + push @e, $c + $ac[$i] - 1; + } + $c += $ac[$i]; + } + my @out; + foreach my $i (0..$#s - 1) { + if ($e[$i + 1] - $s[$i] + 1 > scalar @out) { + @out = @{$a}[$s[$i]..$e[$i + 1]]; + } + } + return \@out; +} diff --git a/challenge-202/roger-bell-west/postscript/ch-1.ps b/challenge-202/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..ece56b49ac --- /dev/null +++ b/challenge-202/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,63 @@ +%!PS + +% begin included library code +% see https://github.com/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 + +/consecutiveodds { + 2 dict begin + /i 0 def + /r false def + { + 2 mod 1 eq { + /i i 1 add def + i 3 ge { + /r true def + exit + } if + } { + /i 0 def + } ifelse + } forall + r + end +} bind def + +(consecutiveodds) test.start +[1 5 3 6] consecutiveodds test +[2 6 3 5] consecutiveodds not test +[1 2 3 4] consecutiveodds not test +[2 3 5 7] consecutiveodds test +test.end diff --git a/challenge-202/roger-bell-west/postscript/ch-2.ps b/challenge-202/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..25ac7f7689 --- /dev/null +++ b/challenge-202/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,158 @@ +%!PS + +% begin included library code +% see https://github.com/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 + +/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 + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + + +% end included library code + +/widestvalley { + 11 dict begin + /a exch def + /av 0 array def + /ac 0 array def + /l -1 def + a { + /v exch def + v l eq { + ac dup dup length 1 sub dup 3 1 roll + get 1 add put + } { + /av av v apush.right def + /ac ac 1 apush.right def + /l v def + } ifelse + } forall + /s 0 array def + /e 0 array def + /c 0 def + 0 1 av length 1 sub { + /i exch def + /peak false def + i 0 eq i av length 1 sub eq or { + /peak true def + } if + peak not { + av i 1 sub get av i get lt + av i get av i 1 add get gt and { + /peak true def + } if + } if + peak { + /s s c apush.right def + /e e c ac i get add 1 sub apush.right def + } if + /c c ac i get add def + } for + /out 0 array def + 0 1 s length 2 sub { + /i exch def + /v s i get def + e i 1 add get s i get sub 1 add out length gt { + /out a s i get dup e i 1 add get exch sub 1 add getinterval def + } if + } for + out + end +} bind def + +(widestvalley) test.start +[1 5 5 2 8] widestvalley [5 5 2 8] deepeq test +[2 6 8 5] widestvalley [2 6 8] deepeq test +[9 8 13 13 2 2 15 17] widestvalley [13 13 2 2 15 17] deepeq test +[2 1 2 1 3] widestvalley [2 1 2] deepeq test +[1 3 3 2 1 2 3 3 2] widestvalley [3 3 2 1 2 3 3] deepeq test +test.end diff --git a/challenge-202/roger-bell-west/python/ch-1.py b/challenge-202/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..1700e2824b --- /dev/null +++ b/challenge-202/roger-bell-west/python/ch-1.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +import unittest + +def consecutiveodds(a): + i = 0 + for v in a: + if v % 2 == 1: + i += 1 + if i >= 3: + return True + else: + i = 0 + return False + +class TestConsecutiveodds(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(consecutiveodds([1, 5, 3, 6]), True, 'example 1') + + def test_ex2(self): + self.assertEqual(consecutiveodds([2, 6, 3, 5]), False, 'example 2') + + def test_ex3(self): + self.assertEqual(consecutiveodds([1, 2, 3, 4]), False, 'example 3') + + def test_ex4(self): + self.assertEqual(consecutiveodds([2, 3, 5, 7]), True, 'example 4') + +unittest.main() diff --git a/challenge-202/roger-bell-west/python/ch-2.py b/challenge-202/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..447a12d6f9 --- /dev/null +++ b/challenge-202/roger-bell-west/python/ch-2.py @@ -0,0 +1,48 @@ +#! /usr/bin/python3 + +import unittest + +def widestvalley(a): + av = [] + ac = [] + l = -1 + for v in a: + if v == l: + ac[-1] += 1 + else: + av.append(v) + ac.append(1) + l = v + s = [] + e = [] + c = 0 + for i in range(len(av)): + if i ==0 or i == len(av) - 1 or (av[i - 1] < av[i] and av[i] > av[i + 1]): + s.append(c) + e.append(c + ac[i] - 1) + c += ac[i] + out = [] + for i in range(len(s) - 1): + if e[i+1] - s[i] + 1 > len(out): + out = a[s[i] : e[i+1] + 1]; + return out + + +class TestWidestvalley(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(widestvalley([1, 5, 5, 2, 8]), [5, 5, 2, 8], 'example 1') + + def test_ex2(self): + self.assertEqual(widestvalley([2, 6, 8, 5]), [2, 6, 8], 'example 2') + + def test_ex3(self): + self.assertEqual(widestvalley([9, 8, 13, 13, 2, 2, 15, 17]), [13, 13, 2, 2, 15, 17], 'example 3') + + def test_ex4(self): + self.assertEqual(widestvalley([2, 1, 2, 1, 3]), [2, 1, 2], 'example 4') + + def test_ex5(self): + self.assertEqual(widestvalley([1, 3, 3, 2, 1, 2, 3, 3, 2]), [3, 3, 2, 1, 2, 3, 3], 'example 5') + +unittest.main() diff --git a/challenge-202/roger-bell-west/raku/ch-1.p6 b/challenge-202/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..a64adbff5b --- /dev/null +++ b/challenge-202/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,25 @@ +#! /usr/bin/raku + +use Test; + +plan 4; + +is(consecutiveodds([1, 5, 3, 6]), True, 'example 1'); +is(consecutiveodds([2, 6, 3, 5]), False, 'example 2'); +is(consecutiveodds([1, 2, 3, 4]), False, 'example 3'); +is(consecutiveodds([2, 3, 5, 7]), True, 'example 4'); + +sub consecutiveodds(@a) { + my $i = 0; + for (@a) -> $v { + if ($v % 2 == 1) { + $i++; + if ($i >= 3) { + return True; + } + } else { + $i = 0; + } + } + return False; +} diff --git a/challenge-202/roger-bell-west/raku/ch-2.p6 b/challenge-202/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..7b7869c590 --- /dev/null +++ b/challenge-202/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,44 @@ +#! /usr/bin/raku + +use Test; + +#plan 5; +plan 5; + +is-deeply(widestvalley([1, 5, 5, 2, 8]), [5, 5, 2, 8], 'example 1'); +is-deeply(widestvalley([2, 6, 8, 5]), [2, 6, 8], 'example 2'); +is-deeply(widestvalley([9, 8, 13, 13, 2, 2, 15, 17]), [13, 13, 2, 2, 15, 17], 'example 3'); +is-deeply(widestvalley([2, 1, 2, 1, 3]), [2, 1, 2], 'example 4'); +is-deeply(widestvalley([1, 3, 3, 2, 1, 2, 3, 3, 2]), [3, 3, 2, 1, 2, 3, 3], 'example 5'); + +sub widestvalley(@a) { + my @av; + my @ac; + my $l = -1; + for (@a) -> $v { + if ($v == $l) { + @ac[*-1]++; + } else { + @av.push($v); + @ac.push(1); + $l = $v; + } + } + my @s; + my @e; + my $c = 0; + for (0..@av.end) -> $i { + if ($i == 0 || $i == @av.end || (@av[$i - 1] < @av[$i] && @av[$i] > @av[$i + 1])) { + @s.push($c); + @e.push($c + @ac[$i] - 1); + } + $c += @ac[$i]; + } + my @out; + for (0..@s.end - 1) -> $i { + if (@e[$i + 1] - @s[$i] + 1 > @out.elems) { + @out = @a[@s[$i]..@e[$i + 1]]; + } + } + return @out; +} diff --git a/challenge-202/roger-bell-west/ruby/ch-1.rb b/challenge-202/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..0b4d240caa --- /dev/null +++ b/challenge-202/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,38 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def consecutiveodds(a) + i = 0 + a.each do |v| + if v % 2 == 1 then + i += 1 + if i >= 3 then + return true + end + else + i = 0 + end + end + return false +end + +class TestConsecutiveodds < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, consecutiveodds([1, 5, 3, 6])) + end + + def test_ex2 + assert_equal(false, consecutiveodds([2, 6, 3, 5])) + end + + def test_ex3 + assert_equal(false, consecutiveodds([1, 2, 3, 4])) + end + + def test_ex4 + assert_equal(true, consecutiveodds([2, 3, 5, 7])) + end + +end diff --git a/challenge-202/roger-bell-west/ruby/ch-2.rb b/challenge-202/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..5ca02192e9 --- /dev/null +++ b/challenge-202/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,61 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def widestvalley(a) + av = [] + ac = [] + l = -1 + a.each do |v| + if v == l then + ac[-1] += 1 + else + av.push(v) + ac.push(1) + l = v + end + end + s = [] + e = [] + c = 0 + 0.upto(av.length - 1) do |i| + if i ==0 || + i == av.length - 1 || + (av[i - 1] < av[i] && av[i] > av[i + 1]) then + s.push(c) + e.push(c + ac[i] - 1) + end + c += ac[i] + end + out = [] + 0.upto(s.length - 2) do |i| + if e[i+1] - s[i] + 1 > out.length then + out = a[s[i] .. e[i+1]]; + end + end + return out +end + +class TestWidestvalley < Test::Unit::TestCase + + def test_ex1 + assert_equal([5, 5, 2, 8], widestvalley([1, 5, 5, 2, 8])) + end + + def test_ex2 + assert_equal([2, 6, 8], widestvalley([2, 6, 8, 5])) + end + + def test_ex3 + assert_equal([13, 13, 2, 2, 15, 17], widestvalley([9, 8, 13, 13, 2, 2, 15, 17])) + end + + def test_ex4 + assert_equal([2, 1, 2], widestvalley([2, 1, 2, 1, 3])) + end + + def test_ex5 + assert_equal([3, 3, 2, 1, 2, 3, 3], widestvalley([1, 3, 3, 2, 1, 2, 3, 3, 2])) + end + +end |
