From fd4e72cd030558e66803068c3dd8222cf7e571f6 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 2 Jan 2024 16:35:26 +0000 Subject: RogerBW solutions for challenge no. 250 --- challenge-250/roger-bell-west/javascript/ch-1.js | 32 +++++++++++ challenge-250/roger-bell-west/javascript/ch-2.js | 28 ++++++++++ challenge-250/roger-bell-west/kotlin/ch-1.kt | 31 +++++++++++ challenge-250/roger-bell-west/kotlin/ch-2.kt | 27 +++++++++ challenge-250/roger-bell-west/lua/ch-1.lua | 33 +++++++++++ challenge-250/roger-bell-west/lua/ch-2.lua | 28 ++++++++++ challenge-250/roger-bell-west/perl/ch-1.pl | 20 +++++++ challenge-250/roger-bell-west/perl/ch-2.pl | 24 ++++++++ challenge-250/roger-bell-west/postscript/ch-1.ps | 69 +++++++++++++++++++++++ challenge-250/roger-bell-west/postscript/ch-2.ps | 71 ++++++++++++++++++++++++ challenge-250/roger-bell-west/python/ch-1.py | 22 ++++++++ challenge-250/roger-bell-west/python/ch-2.py | 24 ++++++++ challenge-250/roger-bell-west/raku/ch-1.p6 | 18 ++++++ challenge-250/roger-bell-west/raku/ch-2.p6 | 20 +++++++ challenge-250/roger-bell-west/ruby/ch-1.rb | 28 ++++++++++ challenge-250/roger-bell-west/ruby/ch-2.rb | 27 +++++++++ challenge-250/roger-bell-west/rust/ch-1.rs | 27 +++++++++ challenge-250/roger-bell-west/rust/ch-2.rs | 24 ++++++++ challenge-250/roger-bell-west/scala/ch-1.scala | 32 +++++++++++ challenge-250/roger-bell-west/scala/ch-2.scala | 33 +++++++++++ challenge-250/roger-bell-west/tests.yaml | 41 ++++++++++++++ 21 files changed, 659 insertions(+) create mode 100755 challenge-250/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-250/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-250/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-250/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-250/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-250/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-250/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-250/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-250/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-250/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-250/roger-bell-west/python/ch-1.py create mode 100755 challenge-250/roger-bell-west/python/ch-2.py create mode 100755 challenge-250/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-250/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-250/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-250/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-250/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-250/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-250/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-250/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-250/roger-bell-west/tests.yaml diff --git a/challenge-250/roger-bell-west/javascript/ch-1.js b/challenge-250/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..7d2657a29c --- /dev/null +++ b/challenge-250/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,32 @@ +#! /usr/bin/node + +"use strict" + +function smallestindex(a) { + let r = -1; + a.forEach((n, i) => { + if (r == -1 && n % 10 == i) { + r = i; + } + }) + return r; +} + +if (smallestindex([0, 1, 2]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (smallestindex([4, 3, 2, 1]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (smallestindex([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == -1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-250/roger-bell-west/javascript/ch-2.js b/challenge-250/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..7228225e74 --- /dev/null +++ b/challenge-250/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,28 @@ +#! /usr/bin/node + +"use strict" + +function alphanumericstringvalue(a) { + let l = []; + for (let n of a) { + let p = 1 * n; + if (Number.isNaN(p)) { + p = n.length; + } + l.push(p); + } + return Math.max(...l); +} + +if (alphanumericstringvalue(['perl', "2", "000", 'python', 'raku']) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (alphanumericstringvalue(["001", "1", "000", "0001"]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-250/roger-bell-west/kotlin/ch-1.kt b/challenge-250/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..67da365163 --- /dev/null +++ b/challenge-250/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,31 @@ +fun smallestindex(a: List): Int { + a.forEachIndexed {i, n -> + if (n % 10 == i) { + return i + } + } + return -1 +} + +fun main() { + + if (smallestindex(listOf(0, 1, 2)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallestindex(listOf(4, 3, 2, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallestindex(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)) == -1) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-250/roger-bell-west/kotlin/ch-2.kt b/challenge-250/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..aa7d5368a7 --- /dev/null +++ b/challenge-250/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,27 @@ +import kotlin.math.max + +fun alphanumericstringvalue(a: List): Int { + var l = ArrayList() + for (n in a) { + val p: Int? = try { n.toInt() } catch (e: NumberFormatException) { n.length } + l.add(p!!) + } + return l.maxOrNull()!! +} + +fun main() { + + if (alphanumericstringvalue(listOf("perl", "2", "000", "python", "raku")) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (alphanumericstringvalue(listOf("001", "1", "000", "0001")) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-250/roger-bell-west/lua/ch-1.lua b/challenge-250/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..adb48b9a25 --- /dev/null +++ b/challenge-250/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,33 @@ +#! /usr/bin/lua + +function smallestindex(a) + for i, n in ipairs(a) do + local j = i - 1; + if n % 10 == j then + return j + end + end + return -1 +end + +if smallestindex({0, 1, 2}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if smallestindex({4, 3, 2, 1}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if smallestindex({1, 2, 3, 4, 5, 6, 7, 8, 9, 0}) == -1 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-250/roger-bell-west/lua/ch-2.lua b/challenge-250/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..bb2ad72473 --- /dev/null +++ b/challenge-250/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,28 @@ +#! /usr/bin/lua + +function alphanumericstringvalue(a) + local l = {} + for _i, n in ipairs(a) do + local p = tonumber(n) + if p == nil then + p = #n + end + table.insert(l, p) + end + return math.max(table.unpack(l)) +end + +if alphanumericstringvalue({"perl", "2", "000", "python", "raku"}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if alphanumericstringvalue({"001", "1", "000", "0001"}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-250/roger-bell-west/perl/ch-1.pl b/challenge-250/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..c20cc81a36 --- /dev/null +++ b/challenge-250/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,20 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(smallestindex([0, 1, 2]), 0, 'example 1'); +is(smallestindex([4, 3, 2, 1]), 2, 'example 2'); +is(smallestindex([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), -1, 'example 3'); + +sub smallestindex($a) { + foreach my $i (0 .. $#{$a}) { + if ($a->[$i] % 10 == $i) { + return $i; + } + } + return -1; +} diff --git a/challenge-250/roger-bell-west/perl/ch-2.pl b/challenge-250/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..1ea57e8dea --- /dev/null +++ b/challenge-250/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(alphanumericstringvalue(['perl', 2, 000, 'python', 'raku']), 6, 'example 1'); +is(alphanumericstringvalue([001, 1, 000, 0001]), 1, 'example 2'); + +use List::Util qw(max); + +sub alphanumericstringvalue($a) { + my @l; + foreach my $n (@{$a}) { + if ($n =~ /^[0-9]+$/) { + push @l, 0 + $n; + } else { + push @l, length($n); + } + } + return max(@l); +} diff --git a/challenge-250/roger-bell-west/postscript/ch-1.ps b/challenge-250/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..e5f3194b8f --- /dev/null +++ b/challenge-250/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,69 @@ +%!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 + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} 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 + +/smallestindex { + 0 dict begin + /a exch def + -1 + a enumerate.array { + aload pop + /i exch def + 10 mod i eq { + pop i + exit + } if + } forall + end +} bind def + +(smallestindex) test.start +[0 1 2] smallestindex 0 eq test +[4 3 2 1] smallestindex 2 eq test +[1 2 3 4 5 6 7 8 9 0] smallestindex -1 eq test +test.end diff --git a/challenge-250/roger-bell-west/postscript/ch-2.ps b/challenge-250/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..6500d56830 --- /dev/null +++ b/challenge-250/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,71 @@ +%!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 + +/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 + +/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 + + +% end included library code + +/alphanumericstringvalue { + [ exch + { + { + cvi + } stopped { + length + } if + } forall + ] + listmax +} bind def + +(alphanumericstringvalue) test.start +[(perl) (2) (000) (python) (raku)] alphanumericstringvalue 6 eq test +[(001) (1) (000) (0001)] alphanumericstringvalue 1 eq test +test.end diff --git a/challenge-250/roger-bell-west/python/ch-1.py b/challenge-250/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..b93676aeb8 --- /dev/null +++ b/challenge-250/roger-bell-west/python/ch-1.py @@ -0,0 +1,22 @@ +#! /usr/bin/python3 + +def smallestindex(a): + for i, n in enumerate(a): + if n % 10 == i: + return i + return -1 + +import unittest + +class TestSmallestindex(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(smallestindex([0, 1, 2]), 0, 'example 1') + + def test_ex2(self): + self.assertEqual(smallestindex([4, 3, 2, 1]), 2, 'example 2') + + def test_ex3(self): + self.assertEqual(smallestindex([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), -1, 'example 3') + +unittest.main() diff --git a/challenge-250/roger-bell-west/python/ch-2.py b/challenge-250/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..144109ba57 --- /dev/null +++ b/challenge-250/roger-bell-west/python/ch-2.py @@ -0,0 +1,24 @@ +#! /usr/bin/python3 + +def alphanumericstringvalue(a): + l = [] + for n in a: + p = 0 + try: + p = int(n) + except: + p = len(n) + l.append(p) + return max(l) + +import unittest + +class TestAlphanumericstringvalue(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(alphanumericstringvalue(["perl", "2", "000", "python", "raku"]), 6, 'example 1') + + def test_ex2(self): + self.assertEqual(alphanumericstringvalue(["001", "1", "000", "0001"]), 1, 'example 2') + +unittest.main() diff --git a/challenge-250/roger-bell-west/raku/ch-1.p6 b/challenge-250/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..cb4fb5821b --- /dev/null +++ b/challenge-250/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(smallestindex([0, 1, 2]), 0, 'example 1'); +is(smallestindex([4, 3, 2, 1]), 2, 'example 2'); +is(smallestindex([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), -1, 'example 3'); + +sub smallestindex(@a) { + for 0 .. @a.end -> $i { + if (@a[$i] % 10 == $i) { + return $i; + } + } + return -1; +} diff --git a/challenge-250/roger-bell-west/raku/ch-2.p6 b/challenge-250/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..a20a4a45aa --- /dev/null +++ b/challenge-250/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(alphanumericstringvalue(['perl', "2", "000", 'python', 'raku']), 6, 'example 1'); +is(alphanumericstringvalue(["001", "1", "000", "0001"]), 1, 'example 2'); + +sub alphanumericstringvalue(@a) { + my @l; + for @a -> $n { + if ($n ~~ /^\d+$/) { + push @l, 0 + $n; + } else { + push @l, $n.chars; + } + } + return @l.max; +} diff --git a/challenge-250/roger-bell-west/ruby/ch-1.rb b/challenge-250/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..551489b802 --- /dev/null +++ b/challenge-250/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,28 @@ +#! /usr/bin/ruby + +def smallestindex(a) + a.each_with_index do |n, i| + if n % 10 == i then + return i + end + end + return -1 +end + +require 'test/unit' + +class TestSmallestindex < Test::Unit::TestCase + + def test_ex1 + assert_equal(0, smallestindex([0, 1, 2])) + end + + def test_ex2 + assert_equal(2, smallestindex([4, 3, 2, 1])) + end + + def test_ex3 + assert_equal(-1, smallestindex([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])) + end + +end diff --git a/challenge-250/roger-bell-west/ruby/ch-2.rb b/challenge-250/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..bcc0f3529a --- /dev/null +++ b/challenge-250/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,27 @@ +#! /usr/bin/ruby + +def alphanumericstringvalue(a) + l = [] + a.each do |n| + begin + l.push(Integer(n)) + rescue ArgumentError + l.push(n.length) + end + end + return l.max() +end + +require 'test/unit' + +class TestAlphanumericstringvalue < Test::Unit::TestCase + + def test_ex1 + assert_equal(6, alphanumericstringvalue(['perl', 2, 000, 'python', 'raku'])) + end + + def test_ex2 + assert_equal(1, alphanumericstringvalue([001, 1, 000, 0001])) + end + +end diff --git a/challenge-250/roger-bell-west/rust/ch-1.rs b/challenge-250/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..4e6312c67c --- /dev/null +++ b/challenge-250/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,27 @@ +#! /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!(smallestindex(vec![0, 1, 2]), 0); +} + +#[test] +fn test_ex2() { + assert_eq!(smallestindex(vec![4, 3, 2, 1]), 2); +} + +#[test] +fn test_ex3() { + assert_eq!(smallestindex(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), -1); +} + +fn smallestindex(a: Vec) -> i32 { + for (ii, n) in a.iter().enumerate() { + let i = ii as i32; + if n % 10 == i { + return i; + } + } + -1 +} diff --git a/challenge-250/roger-bell-west/rust/ch-2.rs b/challenge-250/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..58f8c58d88 --- /dev/null +++ b/challenge-250/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,24 @@ +#! /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!(alphanumericstringvalue(vec!["perl", "2", "000", "python", "raku"]), 6); +} + +#[test] +fn test_ex2() { + assert_eq!(alphanumericstringvalue(vec!["001", "1", "000", "0001"]), 1); +} + +fn alphanumericstringvalue(a: Vec<&str>) -> u32 { + let mut l: Vec = Vec::new(); + for n in a { + let mut p = n.len() as u32; + if let Ok(k) = n.parse::() { + p = k; + } + l.push(p); + } + return *l.iter().max().unwrap(); +} diff --git a/challenge-250/roger-bell-west/scala/ch-1.scala b/challenge-250/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..ce2b3fdbc1 --- /dev/null +++ b/challenge-250/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,32 @@ + +object Smallestindex { + def smallestindex(a: List[Int]): Int = { + for ((n, i) <- a.zipWithIndex) { + if (n % 10 == i) { + return i + } + } + return -1 + } + def main(args: Array[String]) { + if (smallestindex(List(0, 1, 2)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallestindex(List(4, 3, 2, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallestindex(List(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)) == -1) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-250/roger-bell-west/scala/ch-2.scala b/challenge-250/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..27702c9e38 --- /dev/null +++ b/challenge-250/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,33 @@ +import scala.collection.mutable.ListBuffer + +object Alphanumericstringvalue { + + def alphanumericstringvalue(a: List[String]): Int = { + var l = new ListBuffer[Int] + for (n <- a) { + val p = try { + n.toInt + } catch { + case e: NumberFormatException => + n.length + } + l += p + } + return l.max + } + def main(args: Array[String]) { + if (alphanumericstringvalue(List("perl", "2", "000", "python", "raku")) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (alphanumericstringvalue(List("001", "1", "000", "0001")) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-250/roger-bell-west/tests.yaml b/challenge-250/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..0f575386d3 --- /dev/null +++ b/challenge-250/roger-bell-west/tests.yaml @@ -0,0 +1,41 @@ +--- +ch-1: + - function: smallestindex + arguments: + - 0 + - 1 + - 2 + result: 0 + - arguments: + - 4 + - 3 + - 2 + - 1 + result: 2 + - arguments: + - 1 + - 2 + - 3 + - 4 + - 5 + - 6 + - 7 + - 8 + - 9 + - 0 + result: -1 +ch-2: + - function: alphanumericstringvalue + arguments: + - perl + - "2" + - "000" + - python + - raku + result: 6 + - arguments: + - "001" + - "1" + - "000" + - "0001" + result: 1 -- cgit