diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-09-13 16:48:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-13 16:48:02 +0100 |
| commit | 09f00ecfdd4b42e8a1c23304421c8bbe6b4e8a23 (patch) | |
| tree | 3577f2c6e74ab43cf8c46f84a7cd81adf26914ea | |
| parent | 0a9c0364a28e48d14b6b3b5ce4dfa1e53c80de06 (diff) | |
| parent | f615f336337319f9054f5e3186c48805dca52ed9 (diff) | |
| download | perlweeklychallenge-club-09f00ecfdd4b42e8a1c23304421c8bbe6b4e8a23.tar.gz perlweeklychallenge-club-09f00ecfdd4b42e8a1c23304421c8bbe6b4e8a23.tar.bz2 perlweeklychallenge-club-09f00ecfdd4b42e8a1c23304421c8bbe6b4e8a23.zip | |
Merge pull request #6732 from Firedrake/rogerbw-challenge-182
Solutions for challenge #182
18 files changed, 855 insertions, 0 deletions
diff --git a/challenge-182/roger-bell-west/javascript/ch-1.js b/challenge-182/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..b181eb81c2 --- /dev/null +++ b/challenge-182/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,36 @@ +#! /usr/bin/node + +"use strict" + +function maxindex(n) { + let mxv = 0; + let mxi = 0; + n.forEach((v, i) => { + if (i == 0 || v > mxv) { + mxv = v; + mxi = i; + } + }); + return mxi; +} + +if (maxindex([5, 2, 9, 1, 7, 6]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (maxindex([4, 2, 3, 1, 5, 0]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (maxindex([4, 2, 3, 1, 4, 0]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-182/roger-bell-west/javascript/ch-2.js b/challenge-182/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..0afcc2751f --- /dev/null +++ b/challenge-182/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,42 @@ +#! /usr/bin/node + +"use strict" + +function commonpath(p) { + let pa = []; + let pl = []; + for (let sp of p) { + let q = sp.split("/"); + pl.push(q.length); + pa.push(q); + } + let out = []; + for (let cl = 0; cl < Math.min(...pl); cl++) { + let ex = false; + let tx = pa[0][cl]; + for (let pe of pa) { + if (pe[cl] != tx) { + ex = true; + break; + } + } + if (ex) { + break; + } + out.push(tx); + } + return out.join("/"); +} + +if (commonpath([ + "/a/b/c/1/x.pl", + "/a/b/c/d/e/2/x.pl", + "/a/b/c/d/3/x.pl", + "/a/b/c/4/x.pl", + "/a/b/c/d/5/x.pl" +]) == "/a/b/c") { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-182/roger-bell-west/kotlin/ch-1.kt b/challenge-182/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..3dcb228086 --- /dev/null +++ b/challenge-182/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,32 @@ +fun maxindex(n: List<Int>): Int { + var mxv = 0; + var mxi = 0; + for ((i, v) in n.withIndex()) { + if (i == 0 || v > mxv) { + mxv = v + mxi = i + } + } + return mxi +} + +fun main() { + if (maxindex(listOf(5, 2, 9, 1, 7, 6)) == 2) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (maxindex(listOf(4, 2, 3, 1, 5, 0)) == 4) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (maxindex(listOf(4, 2, 3, 1, 4, 0)) == 0) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-182/roger-bell-west/kotlin/ch-2.kt b/challenge-182/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..914808fa33 --- /dev/null +++ b/challenge-182/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,40 @@ +fun commonpath(p: List<String>): String { + var pa = ArrayList<List<String>>() + var pl = ArrayList<Int>() + for (sp in p) { + val q = sp.split("/").toList() + pa.add(q) + pl.add(q.size) + } + var out = ArrayList<String>() + for (cl in 0..pl.minOrNull()!!-1) { + var ex = false + val tx = pa[0][cl] + for (pe in pa) { + if (pe[cl] != tx) { + ex = true + break + } + } + if (ex) { + break + } + out.add(tx) + } + return out.joinToString("/") +} + +fun main() { + if (commonpath(listOf( + "/a/b/c/1/x.pl", + "/a/b/c/d/e/2/x.pl", + "/a/b/c/d/3/x.pl", + "/a/b/c/4/x.pl", + "/a/b/c/d/5/x.pl" + )) == "/a/b/c") { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-182/roger-bell-west/lua/ch-1.lua b/challenge-182/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..5e1e0c4887 --- /dev/null +++ b/challenge-182/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,34 @@ +#! /usr/bin/lua + +function maxindex(n) + local mxv = 0 + local mxi = 0 + for i, v in ipairs(n) do + if i == 1 or v > mxv then + mxv = v + mxi = i + end + end + return mxi-1 +end + +if maxindex({5, 2, 9, 1, 7, 6}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxindex({4, 2, 3, 1, 5, 0}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxindex({4, 2, 3, 1, 4, 0}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-182/roger-bell-west/lua/ch-2.lua b/challenge-182/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..08c63996aa --- /dev/null +++ b/challenge-182/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,53 @@ +#! /usr/bin/lua + +-- bart at https://stackoverflow.com/questions/1426954/split-string-in-lua +function split(inputstr, sep) + sep=sep or '%s' + local t={} + for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do + table.insert(t,field) + if s=="" then + return t + end + end +end + + +function commonpath(p) + local pa = {} + local pl = {} + for i, sp in ipairs(p) do + local q = split(sp, "/") + table.insert(pl,#q) + table.insert(pa,q) + end + local out = {} + for cl = 1,math.min(table.unpack(pl)) do + local ex = false + local tx = pa[1][cl] + for i, pe in ipairs(pa) do + if pe[cl] ~= tx then + ex = true + break + end + end + if ex then + break + end + table.insert(out,tx) + end + return table.concat(out,"/") +end + +if commonpath({ + "/a/b/c/1/x.pl", + "/a/b/c/d/e/2/x.pl", + "/a/b/c/d/3/x.pl", + "/a/b/c/4/x.pl", + "/a/b/c/d/5/x.pl" + }) == "/a/b/c" then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-182/roger-bell-west/perl/ch-1.pl b/challenge-182/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..6a2d333f49 --- /dev/null +++ b/challenge-182/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,23 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(maxindex([5, 2, 9, 1, 7, 6]),2,'example 1'); +is(maxindex([4, 2, 3, 1, 5, 0]),4,'example 2'); +is(maxindex([4, 2, 3, 1, 4, 0]),0,'example 3'); + +sub maxindex($n) { + my $mxv = 0; + my $mxi = 0; + foreach my $i (0..$#{$n}) { + if ($i == 0 || $n->[$i] > $mxv) { + $mxv = $n->[$i]; + $mxi = $i; + } + } + return $mxi; +} diff --git a/challenge-182/roger-bell-west/perl/ch-2.pl b/challenge-182/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..8da4c02792 --- /dev/null +++ b/challenge-182/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,43 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 1; + +use List::Util qw(min); + +is(commonpath([ + "/a/b/c/1/x.pl", + "/a/b/c/d/e/2/x.pl", + "/a/b/c/d/3/x.pl", + "/a/b/c/4/x.pl", + "/a/b/c/d/5/x.pl" + ]), "/a/b/c", 'example 1'); + +sub commonpath($p) { + my @pa; + my @pl; + foreach my $sp (@{$p}) { + my @q = split '/',$sp; + push @pl,scalar @q; + push @pa,\@q; + } + my @out; + foreach my $cl (0..min(@pl)-1) { + my $ex = 0; + my $tx = $pa[0][$cl]; + foreach my $pe (@pa) { + if ($pe->[$cl] ne $tx) { + $ex = 1; + last; + } + } + if ($ex) { + last; + } + push @out,$tx; + } + return join('/',@out); +} diff --git a/challenge-182/roger-bell-west/postscript/ch-1.ps b/challenge-182/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..d8835bb397 --- /dev/null +++ b/challenge-182/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,70 @@ +%!PS + +% begin included library code +% see https://github.com/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.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 + +/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 + + +% end included library code + +/maxindex { + 4 dict begin + /mxv 0 def + /mxi 0 def + enumerate.array { + dup 0 get /i exch def + 1 get /v exch def + i 0 eq v mxv gt or { + /mxv v def + /mxi i def + } if + } forall + mxi + end +} bind def + +(maxindex) test.start +[5 2 9 1 7 6] maxindex 2 eq test +[4 2 3 1 5 0] maxindex 4 eq test +[4 2 3 1 4 0] maxindex 0 eq test +test.end diff --git a/challenge-182/roger-bell-west/postscript/ch-2.ps b/challenge-182/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..323b3d9cda --- /dev/null +++ b/challenge-182/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,210 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} 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 + +/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 + +/listmin { + { min } reduce +} 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 + +/apush { apush.right } bind def + +/strconcat % (a) (b) -> (ab) +{ exch dup length + 2 index length add string + dup dup 4 2 roll copy length + 4 -1 roll putinterval +} bind def + +/strsplit % (ajbjc) (j) -> [ (a) (b) (c) ] +{ + 1 dict begin + /sep exch def + [ exch + { + dup length 0 eq { + pop + exit + } { + sep search { + exch pop + dup length 0 eq { + pop + } { + exch + } ifelse + } { + () + } ifelse + } ifelse + } loop + ] + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/strjoin % [(a) (b) (c)] (j) -> (ajbjc) +{ + 3 dict begin + /j exch def + dup 0 get /out exch def + /first true def + { + first { + pop + /first false def + } { + out j strconcat + exch strconcat + /out exch def + } ifelse + } forall + out + end +} bind def + + +% end included library code + +/commonpath { + 6 dict begin + /pa 0 array def + /pl 0 array def + { + (/) strsplit /q exch def + /pl pl q length apush def + /pa pa q apush def + } forall + /out [ () ] def + 0 1 pl listmin 1 sub { + /cl exch def + /ex false def + /tx pa 0 get cl get def + pa { + cl get tx deepeq not { + /ex true def + exit + } if + } forall + ex { + exit + } if + /out out tx apush def + } for + out (/) strjoin + end +} bind def + +(commonpath) test.start +[ + (/a/b/c/1/x.pl) + (/a/b/c/d/e/2/x.pl) + (/a/b/c/d/3/x.pl) + (/a/b/c/4/x.pl) + (/a/b/c/d/5/x.pl) +] commonpath (/a/b/c) deepeq test +test.end diff --git a/challenge-182/roger-bell-west/python/ch-1.py b/challenge-182/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..cd935671c9 --- /dev/null +++ b/challenge-182/roger-bell-west/python/ch-1.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +import unittest + +def maxindex(n): + mxv = 0 + mxi = 0 + for (i, v) in enumerate(n): + if i == 0 or v > mxv: + mxv = v + mxi = i + return mxi + +class TestMaxindex(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maxindex([5, 2, 9, 1, 7, 6]), + 2, 'example 1') + + def test_ex2(self): + self.assertEqual(maxindex([4, 2, 3, 1, 5, 0]), + 4, 'example 2') + + def test_ex3(self): + self.assertEqual(maxindex([4, 2, 3, 1, 4, 0]), + 0, 'example 3') + +unittest.main() diff --git a/challenge-182/roger-bell-west/python/ch-2.py b/challenge-182/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..c06b97aedb --- /dev/null +++ b/challenge-182/roger-bell-west/python/ch-2.py @@ -0,0 +1,39 @@ +#! /usr/bin/python3 + +from collections import defaultdict + +import unittest + +def commonpath(p): + pa = [] + pl = [] + for sp in p: + q = sp.split("/") + pl.append(len(q)) + pa.append(q) + out = [] + for cl in range(min(pl)): + ex = False + tx = pa[0][cl] + for pe in pa: + if pe[cl] != tx: + ex = True + break + if ex: + break + out.append(tx) + return "/".join(out) + +class TestCommonpath(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(commonpath([ + "/a/b/c/1/x.pl", + "/a/b/c/d/e/2/x.pl", + "/a/b/c/d/3/x.pl", + "/a/b/c/4/x.pl", + "/a/b/c/d/5/x.pl" + ]), + "/a/b/c", 'example 1') + +unittest.main() diff --git a/challenge-182/roger-bell-west/raku/ch-1.p6 b/challenge-182/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..10b2112122 --- /dev/null +++ b/challenge-182/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(maxindex([5, 2, 9, 1, 7, 6]),2,'example 1'); +is(maxindex([4, 2, 3, 1, 5, 0]),4,'example 2'); +is(maxindex([4, 2, 3, 1, 4, 0]),0,'example 3'); + +sub maxindex(@n) { + my $mxv = 0; + my $mxi = 0; + for (0..@n.end) -> $i { + if ($i == 0 || @n[$i] > $mxv) { + $mxv = @n[$i]; + $mxi = $i; + } + } + return $mxi; +} diff --git a/challenge-182/roger-bell-west/raku/ch-2.p6 b/challenge-182/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..97f93f8492 --- /dev/null +++ b/challenge-182/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,39 @@ +#! /usr/bin/perl6 + +use Test; + +plan 1; + +is(commonpath([ + "/a/b/c/1/x.pl", + "/a/b/c/d/e/2/x.pl", + "/a/b/c/d/3/x.pl", + "/a/b/c/4/x.pl", + "/a/b/c/d/5/x.pl" + ]), "/a/b/c", 'example 1'); + +sub commonpath(@p) { + my @pa; + my @pl; + for @p -> $sp { + my @q = split("/",$sp); + @pl.push(@q.elems); + @pa.push(@q); + } + my @out; + for (0..min(@pl)-1) -> $cl { + my $ex = False; + my $tx = @pa[0][$cl]; + for (@pa) -> @pe { + if (@pe[$cl] ne $tx) { + $ex = True; + last; + } + } + if ($ex) { + last; + } + @out.push($tx); + } + return @out.join("/"); +} diff --git a/challenge-182/roger-bell-west/ruby/ch-1.rb b/challenge-182/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..5863f966ea --- /dev/null +++ b/challenge-182/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def maxindex(n) + mxv = 0 + mxi = 0 + n.each_with_index do |v, i| + if i == 0 || v > mxv then + mxv = v + mxi = i + end + end + return mxi +end + +class TestMaxindex < Test::Unit::TestCase + + def test_ex1 + assert_equal(2, maxindex([5, 2, 9, 1, 7, 6])) + end + + def test_ex2 + assert_equal(4, maxindex([4, 2, 3, 1, 5, 0])) + end + + def test_ex3 + assert_equal(0, maxindex([4, 2, 3, 1, 4, 0])) + end + +end diff --git a/challenge-182/roger-bell-west/ruby/ch-2.rb b/challenge-182/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..43ffba95e5 --- /dev/null +++ b/challenge-182/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,43 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def commonpath(p) + pa = [] + pl = [] + p.each do |sp| + q = sp.split("/") + pl.push(q.length) + pa.push(q) + end + out = [] + 0.upto(pl.min-1).each do |cl| + ex = false + tx = pa[0][cl] + pa.each do |pe| + if pe[cl] != tx then + ex = true + break + end + end + if ex then + break + end + out.push(tx) + end + return out.join("/") +end + +class TestCommonpath < Test::Unit::TestCase + + def test_ex1 + assert_equal("/a/b/c", commonpath([ + "/a/b/c/1/x.pl", + "/a/b/c/d/e/2/x.pl", + "/a/b/c/d/3/x.pl", + "/a/b/c/4/x.pl", + "/a/b/c/d/5/x.pl" + ])) + end + +end diff --git a/challenge-182/roger-bell-west/rust/ch-1.rs b/challenge-182/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..3820a782ca --- /dev/null +++ b/challenge-182/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,29 @@ +#! /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!(maxindex(vec![5, 2, 9, 1, 7, 6]), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(maxindex(vec![4, 2, 3, 1, 5, 0]), 4); +} + +#[test] +fn test_ex3() { + assert_eq!(maxindex(vec![4, 2, 3, 1, 4, 0]), 0); +} + +fn maxindex(n: Vec<isize>) -> usize { + let mut mxv = 0; + let mut mxi = 0; + for (i, v) in n.iter().enumerate() { + if i == 0 || *v > mxv { + mxv = *v; + mxi = i; + } + } + mxi +} diff --git a/challenge-182/roger-bell-west/rust/ch-2.rs b/challenge-182/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..06b6abe5eb --- /dev/null +++ b/challenge-182/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,42 @@ +#! /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!( + commonpath(vec![ + "/a/b/c/1/x.pl", + "/a/b/c/d/e/2/x.pl", + "/a/b/c/d/3/x.pl", + "/a/b/c/4/x.pl", + "/a/b/c/d/5/x.pl" + ]), + "/a/b/c" + ); +} + +fn commonpath(p: Vec<&str>) -> String { + let mut pa: Vec<Vec<String>> = Vec::new(); + let mut pl: Vec<usize> = Vec::new(); + for sp in p { + let q = sp.split('/').map(|x| x.to_string()).collect::<Vec<String>>(); + pl.push(q.len()); + pa.push(q); + } + let mut out: Vec<String> = Vec::new(); + for cl in 0..*pl.iter().min().unwrap() { + let mut ex = false; + let tx = &pa[0][cl]; + for pe in &pa { + if &pe[cl] != tx { + ex = true; + break; + } + } + if ex { + break; + } + out.push(tx.to_string()); + } + out.join("/") +} |
