diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-16 22:44:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-16 22:44:08 +0100 |
| commit | fe361ee89d238b53b5b93c43e69abb92b8686d41 (patch) | |
| tree | f943834472be00f64b586bd162229976eed2731c | |
| parent | 65fdce07ae9b197000d4c64b9f1699cef16befe8 (diff) | |
| parent | a8e5e4c3646743778a0d7146e26b0a4308891ad6 (diff) | |
| download | perlweeklychallenge-club-fe361ee89d238b53b5b93c43e69abb92b8686d41.tar.gz perlweeklychallenge-club-fe361ee89d238b53b5b93c43e69abb92b8686d41.tar.bz2 perlweeklychallenge-club-fe361ee89d238b53b5b93c43e69abb92b8686d41.zip | |
Merge pull request #12687 from Firedrake/rogerbw-challenge-339
RogerBW solutions for challenge no. 339
25 files changed, 1053 insertions, 0 deletions
diff --git a/challenge-339/roger-bell-west/crystal/ch-1.cr b/challenge-339/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..5f13f204b3 --- /dev/null +++ b/challenge-339/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,39 @@ +#! /usr/bin/crystal + +def maxdiff(a) + md = 0 + 0.upto(a.size - 2) do |w| + (w + 1).upto(a.size - 1) do |x| + (w + 1).upto(a.size - 2) do |y| + if y != x + (y + 1).upto(a.size - 1) do |z| + if z != x + v = (a[w] * a[x] - a[y] * a[z]).abs + md = [md, v].max + end + end + end + end + end + end + md +end + +require "spec" +describe "maxdiff" do + it "test_ex1" do + maxdiff([5, 9, 3, 4, 6]).should eq 42 + end + it "test_ex2" do + maxdiff([1, -2, 3, -4]).should eq 10 + end + it "test_ex3" do + maxdiff([-3, -1, -2, -4]).should eq 10 + end + it "test_ex4" do + maxdiff([10, 2, 0, 5, 1]).should eq 50 + end + it "test_ex5" do + maxdiff([7, 8, 9, 10, 10]).should eq 44 + end +end diff --git a/challenge-339/roger-bell-west/crystal/ch-2.cr b/challenge-339/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..60b0892ca7 --- /dev/null +++ b/challenge-339/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,30 @@ +#! /usr/bin/crystal + +def peakpoint(a) + h = 0 + o = 0 + a.each do |p| + h += p + o = [o, h].max + end + o +end + +require "spec" +describe "peakpoint" do + it "test_ex1" do + peakpoint([-5, 1, 5, -9, 2]).should eq 1 + end + it "test_ex2" do + peakpoint([10, 10, 10, -25]).should eq 30 + end + it "test_ex3" do + peakpoint([3, -4, 2, 5, -6, 1]).should eq 6 + end + it "test_ex4" do + peakpoint([-1, -2, -3, -4]).should eq 0 + end + it "test_ex5" do + peakpoint([-10, 15, 5]).should eq 10 + end +end diff --git a/challenge-339/roger-bell-west/javascript/ch-1.js b/challenge-339/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..7097a84470 --- /dev/null +++ b/challenge-339/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,53 @@ +#! /usr/bin/node + +function maxdiff(a) { + let md = 0; + for (let w = 0; w < a.length - 1; w++) { + for (let x = w + 1; x < a.length; x++) { + for (let y = w + 1; y < a.length - 1; y++) { + if (y != x) { + for (let z = y + 1; z < a.length; z++) { + if (z != x) { + const v = Math.abs(a[w] * a[x] - a[y] * a[z]); + md = Math.max(md, v) + } + } + } + } + } + } + return md; +}- + +"use strict" + +if (maxdiff([5, 9, 3, 4, 6]) == 42) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxdiff([1, -2, 3, -4]) == 10) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxdiff([-3, -1, -2, -4]) == 10) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxdiff([10, 2, 0, 5, 1]) == 50) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxdiff([7, 8, 9, 10, 10]) == 44) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-339/roger-bell-west/javascript/ch-2.js b/challenge-339/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..cf5a69d9ab --- /dev/null +++ b/challenge-339/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,44 @@ +#! /usr/bin/node + +"use strict" + +function peakpoint(a) { + let h = 0; + let o = 0; + for (let p of a) { + h += p; + o = Math.max(o, h); + } + return o; +} + +if (peakpoint([-5, 1, 5, -9, 2]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (peakpoint([10, 10, 10, -25]) == 30) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (peakpoint([3, -4, 2, 5, -6, 1]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (peakpoint([-1, -2, -3, -4]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (peakpoint([-10, 15, 5]) == 10) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-339/roger-bell-west/kotlin/ch-1.kt b/challenge-339/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..fcc0931ee6 --- /dev/null +++ b/challenge-339/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,53 @@ +fun maxdiff(a: List<Int>): Int { + var md = 0 + for (w in 0 .. a.size - 2) { + for (x in w + 1 .. a.size - 1) { + for (y in w + 1 .. a.size - 2) { + if (y != x) { + for (z in y + 1 .. a.size - 1) { + if (z != x) { + val v = Math.abs(a[w] * a[x] - a[y] * a[z]) + md = listOf(md, v).maxOrNull()!! + } + } + } + } + } + } + return md +} + +fun main() { + + if (maxdiff(listOf(5, 9, 3, 4, 6)) == 42) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdiff(listOf(1, -2, 3, -4)) == 10) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdiff(listOf(-3, -1, -2, -4)) == 10) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdiff(listOf(10, 2, 0, 5, 1)) == 50) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdiff(listOf(7, 8, 9, 10, 10)) == 44) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-339/roger-bell-west/kotlin/ch-2.kt b/challenge-339/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..60963e5b66 --- /dev/null +++ b/challenge-339/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,44 @@ +fun peakpoint(a: List<Int>): Int { + var h = 0 + var o = 0 + for (p in a) { + h += p + o = listOf(o, h).maxOrNull()!! + } + return o +} + +fun main() { + + if (peakpoint(listOf(-5, 1, 5, -9, 2)) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (peakpoint(listOf(10, 10, 10, -25)) == 30) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (peakpoint(listOf(3, -4, 2, 5, -6, 1)) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (peakpoint(listOf(-1, -2, -3, -4)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (peakpoint(listOf(-10, 15, 5)) == 10) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-339/roger-bell-west/lua/ch-1.lua b/challenge-339/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..681ebd8b7b --- /dev/null +++ b/challenge-339/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,56 @@ +#! /usr/bin/lua + +function maxdiff(a) + local md = 0 + for w = 1, #a - 1 do + for x = w + 1, #a do + for y = w + 1, #a - 1 do + if y ~= x then + for z = y + 1, #a do + if z ~= x then + v = math.abs(a[w] * a[x] - a[y] * a[z]) + md = math.max(md, v) + end + end + end + end + end + end + return md +end + +if maxdiff({5, 9, 3, 4, 6}) == 42 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxdiff({1, -2, 3, -4}) == 10 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxdiff({-3, -1, -2, -4}) == 10 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxdiff({10, 2, 0, 5, 1}) == 50 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxdiff({7, 8, 9, 10, 10}) == 44 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-339/roger-bell-west/lua/ch-2.lua b/challenge-339/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..fe1f83e11c --- /dev/null +++ b/challenge-339/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,47 @@ +#! /usr/bin/lua + +function peakpoint(a) + local h = 0 + local o = 0 + for _, p in ipairs(a) do + h = h + p + o = math.max(o, h) + end + return o +end + +if peakpoint({-5, 1, 5, -9, 2}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if peakpoint({10, 10, 10, -25}) == 30 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if peakpoint({3, -4, 2, 5, -6, 1}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if peakpoint({-1, -2, -3, -4}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if peakpoint({-10, 15, 5}) == 10 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-339/roger-bell-west/perl/ch-1.pl b/challenge-339/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..51645f6cd9 --- /dev/null +++ b/challenge-339/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 => 5; + +is(maxdiff([5, 9, 3, 4, 6]), 42, 'example 1'); +is(maxdiff([1, -2, 3, -4]), 10, 'example 2'); +is(maxdiff([-3, -1, -2, -4]), 10, 'example 3'); +is(maxdiff([10, 2, 0, 5, 1]), 50, 'example 4'); +is(maxdiff([7, 8, 9, 10, 10]), 44, 'example 5'); + +use List::Util qw(max); +sub maxdiff($a) { + my $md = 0; + foreach my $w (0 .. scalar @{$a} - 2) { + foreach my $x ($w + 1 .. scalar @{$a} - 1) { + foreach my $y ($w + 1 .. scalar @{$a} - 2) { + if ($y != $x) { + foreach my $z ($y + 1 .. scalar @{$a} - 1) { + if ($z != $x) { + my $v = abs($a->[$w] * $a->[$x] - $a->[$y] * $a->[$z]); + $md = max($md, $v); + } + } + } + } + } + } + $md; +} diff --git a/challenge-339/roger-bell-west/perl/ch-2.pl b/challenge-339/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..fd4f3c7f51 --- /dev/null +++ b/challenge-339/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(peakpoint([-5, 1, 5, -9, 2]), 1, 'example 1'); +is(peakpoint([10, 10, 10, -25]), 30, 'example 2'); +is(peakpoint([3, -4, 2, 5, -6, 1]), 6, 'example 3'); +is(peakpoint([-1, -2, -3, -4]), 0, 'example 4'); +is(peakpoint([-10, 15, 5]), 10, 'example 5'); + +use List::Util qw(max); + +sub peakpoint($a) { + my $h = 0; + my $o = 0; + foreach my $p (@{$a}) { + $h += $p; + $o = max($o, $h); + } + $o; +} diff --git a/challenge-339/roger-bell-west/postscript/ch-1.ps b/challenge-339/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..c5e860c3ad --- /dev/null +++ b/challenge-339/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,70 @@ +%!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 + +/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.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 + +/maxdiff { + 0 dict begin + /a exch def + 0 + 0 1 a length 2 sub { + /w exch def + w 1 add 1 a length 1 sub { + /x exch def + w 1 add 1 a length 2 sub { + /y exch def + x y ne { + y 1 add 1 a length 1 sub { + /z exch def + z x ne { + a w get a x get mul a y get a z get mul sub abs + max + } if + } for + } if + } for + } for + } for + end +} bind def + +(maxdiff) test.start +[5 9 3 4 6] maxdiff 42 eq test +[1 -2 3 -4] maxdiff 10 eq test +[-3 -1 -2 -4] maxdiff 10 eq test +[10 2 0 5 1] maxdiff 50 eq test +[7 8 9 10 10] maxdiff 44 eq test +test.end diff --git a/challenge-339/roger-bell-west/postscript/ch-2.ps b/challenge-339/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..8d066e067f --- /dev/null +++ b/challenge-339/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,56 @@ +%!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 + + +% end included library code + +/peakpoint { + 0 dict begin + 0 exch + /h 0 def + { + h add /h exch def + h max + } forall + end +} bind def + +(peakpoint) test.start +[-5 1 5 -9 2] peakpoint 1 eq test +[10 10 10 -25] peakpoint 30 eq test +[3 -4 2 5 -6 1] peakpoint 6 eq test +[-1 -2 -3 -4] peakpoint 0 eq test +[-10 15 5] peakpoint 10 eq test +test.end diff --git a/challenge-339/roger-bell-west/python/ch-1.py b/challenge-339/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..558e268a00 --- /dev/null +++ b/challenge-339/roger-bell-west/python/ch-1.py @@ -0,0 +1,34 @@ +#! /usr/bin/python3 + +def maxdiff(a): + md = 0 + for w in range(len(a) - 1): + for x in range(w + 1, len(a)): + for y in range(w + 1, len(a) - 1): + if y != x: + for z in range(y + 1, len(a)): + if z != x: + v = abs(a[w] * a[x] - a[y] * a[z]) + md = max(md, v) + return md + +import unittest + +class TestMaxdiff(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maxdiff([5, 9, 3, 4, 6]), 42, 'example 1') + + def test_ex2(self): + self.assertEqual(maxdiff([1, -2, 3, -4]), 10, 'example 2') + + def test_ex3(self): + self.assertEqual(maxdiff([-3, -1, -2, -4]), 10, 'example 3') + + def test_ex4(self): + self.assertEqual(maxdiff([10, 2, 0, 5, 1]), 50, 'example 4') + + def test_ex5(self): + self.assertEqual(maxdiff([7, 8, 9, 10, 10]), 44, 'example 5') + +unittest.main() diff --git a/challenge-339/roger-bell-west/python/ch-2.py b/challenge-339/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..4ac047e917 --- /dev/null +++ b/challenge-339/roger-bell-west/python/ch-2.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +def peakpoint(a): + h = 0 + o = 0 + for p in a: + h += p + o = max(o, h) + return o + +import unittest + +class TestPeakpoint(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(peakpoint([-5, 1, 5, -9, 2]), 1, 'example 1') + + def test_ex2(self): + self.assertEqual(peakpoint([10, 10, 10, -25]), 30, 'example 2') + + def test_ex3(self): + self.assertEqual(peakpoint([3, -4, 2, 5, -6, 1]), 6, 'example 3') + + def test_ex4(self): + self.assertEqual(peakpoint([-1, -2, -3, -4]), 0, 'example 4') + + def test_ex5(self): + self.assertEqual(peakpoint([-10, 15, 5]), 10, 'example 5') + +unittest.main() diff --git a/challenge-339/roger-bell-west/raku/ch-1.p6 b/challenge-339/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..a3e7819d75 --- /dev/null +++ b/challenge-339/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,30 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(maxdiff([5, 9, 3, 4, 6]), 42, 'example 1'); +is(maxdiff([1, -2, 3, -4]), 10, 'example 2'); +is(maxdiff([-3, -1, -2, -4]), 10, 'example 3'); +is(maxdiff([10, 2, 0, 5, 1]), 50, 'example 4'); +is(maxdiff([7, 8, 9, 10, 10]), 44, 'example 5'); + +sub maxdiff(@a) { + my $md = 0; + for 0 .. @a.elems - 2 -> $w { + for $w + 1 .. @a.elems - 1 -> $x { + for $w + 1 .. @a.elems - 2 -> $y { + if ($y != $x) { + for $y + 1 .. @a.elems - 1 -> $z { + if ($z != $x) { + my $v = abs(@a[$w] * @a[$x] - @a[$y] * @a[$z]); + $md = max($md, $v); + } + } + } + } + } + } + $md; +} diff --git a/challenge-339/roger-bell-west/raku/ch-2.p6 b/challenge-339/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..39d2d7f5e8 --- /dev/null +++ b/challenge-339/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(peakpoint([-5, 1, 5, -9, 2]), 1, 'example 1'); +is(peakpoint([10, 10, 10, -25]), 30, 'example 2'); +is(peakpoint([3, -4, 2, 5, -6, 1]), 6, 'example 3'); +is(peakpoint([-1, -2, -3, -4]), 0, 'example 4'); +is(peakpoint([-10, 15, 5]), 10, 'example 5'); + +sub peakpoint(@a) { + my $h = 0; + my $o = 0; + for @a -> $p { + $h += $p; + $o = max($o, $h); + } + $o; +} diff --git a/challenge-339/roger-bell-west/ruby/ch-1.rb b/challenge-339/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..218453841d --- /dev/null +++ b/challenge-339/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,46 @@ +#! /usr/bin/ruby + +def maxdiff(a) + md = 0 + 0.upto(a.length - 2) do |w| + (w + 1).upto(a.length - 1) do |x| + (w + 1).upto(a.length - 2) do |y| + if y != x + (y + 1).upto(a.length - 1) do |z| + if z != x + v = (a[w] * a[x] - a[y] * a[z]).abs + md = [md, v].max + end + end + end + end + end + end + md +end + +require 'test/unit' + +class TestMaxdiff < Test::Unit::TestCase + + def test_ex1 + assert_equal(42, maxdiff([5, 9, 3, 4, 6])) + end + + def test_ex2 + assert_equal(10, maxdiff([1, -2, 3, -4])) + end + + def test_ex3 + assert_equal(10, maxdiff([-3, -1, -2, -4])) + end + + def test_ex4 + assert_equal(50, maxdiff([10, 2, 0, 5, 1])) + end + + def test_ex5 + assert_equal(44, maxdiff([7, 8, 9, 10, 10])) + end + +end diff --git a/challenge-339/roger-bell-west/ruby/ch-2.rb b/challenge-339/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..c989106caf --- /dev/null +++ b/challenge-339/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,37 @@ +#! /usr/bin/ruby + +def peakpoint(a) + h = 0 + o = 0 + a.each do |p| + h += p + o = [o, h].max + end + o +end + +require 'test/unit' + +class TestPeakpoint < Test::Unit::TestCase + + def test_ex1 + assert_equal(1, peakpoint([-5, 1, 5, -9, 2])) + end + + def test_ex2 + assert_equal(30, peakpoint([10, 10, 10, -25])) + end + + def test_ex3 + assert_equal(6, peakpoint([3, -4, 2, 5, -6, 1])) + end + + def test_ex4 + assert_equal(0, peakpoint([-1, -2, -3, -4])) + end + + def test_ex5 + assert_equal(10, peakpoint([-10, 15, 5])) + end + +end diff --git a/challenge-339/roger-bell-west/rust/ch-1.rs b/challenge-339/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..295014543a --- /dev/null +++ b/challenge-339/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,46 @@ +#! /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!(maxdiff(vec![5, 9, 3, 4, 6]), 42); +} + +#[test] +fn test_ex2() { + assert_eq!(maxdiff(vec![1, -2, 3, -4]), 10); +} + +#[test] +fn test_ex3() { + assert_eq!(maxdiff(vec![-3, -1, -2, -4]), 10); +} + +#[test] +fn test_ex4() { + assert_eq!(maxdiff(vec![10, 2, 0, 5, 1]), 50); +} + +#[test] +fn test_ex5() { + assert_eq!(maxdiff(vec![7, 8, 9, 10, 10]), 44); +} + +fn maxdiff(a: Vec<i32>) -> i32 { + let mut md = 0; + for w in 0..a.len() - 1 { + for x in w + 1..a.len() { + for y in w + 1..a.len() - 1 { + if y != x { + for z in y + 1..a.len() { + if z != x { + let v = (a[w] * a[x] - a[y] * a[z]).abs(); + md = *[md, v].iter().max().unwrap(); + } + } + } + } + } + } + md +} diff --git a/challenge-339/roger-bell-west/rust/ch-2.rs b/challenge-339/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..5d6b5a7469 --- /dev/null +++ b/challenge-339/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,37 @@ +#! /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!(peakpoint(vec![-5, 1, 5, -9, 2]), 1); +} + +#[test] +fn test_ex2() { + assert_eq!(peakpoint(vec![10, 10, 10, -25]), 30); +} + +#[test] +fn test_ex3() { + assert_eq!(peakpoint(vec![3, -4, 2, 5, -6, 1]), 6); +} + +#[test] +fn test_ex4() { + assert_eq!(peakpoint(vec![-1, -2, -3, -4]), 0); +} + +#[test] +fn test_ex5() { + assert_eq!(peakpoint(vec![-10, 15, 5]), 10); +} + +fn peakpoint(a: Vec<i32>) -> i32 { + let mut h = 0; + let mut o = 0; + for p in a { + h += p; + o = *[o, h].iter().max().unwrap(); + } + o +} diff --git a/challenge-339/roger-bell-west/scala/ch-1.scala b/challenge-339/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..0d5b251c4e --- /dev/null +++ b/challenge-339/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,54 @@ + +object Maxdiff { +def maxdiff(a: List[Int]): Int = { + var md = 0 + for (w <- 0 until a.size - 1) { + for (x <- w + 1 until a.size) { + for (y <- w + 1 until a.size - 1) { + if (y != x) { + for (z <- y + 1 until a.size) { + if (z != x) { + val v = (a(w) * a(x) - a(y) * a(z)).abs + md = List(md, v).max + } + } + } + } + } + } + md +} + def main(args: Array[String]) { + if (maxdiff(List(5, 9, 3, 4, 6)) == 42) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdiff(List(1, -2, 3, -4)) == 10) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdiff(List(-3, -1, -2, -4)) == 10) { + print("Pass") + } else { |
