aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-08-03 06:34:55 +0100
committerGitHub <noreply@github.com>2023-08-03 06:34:55 +0100
commita9cda3002313deae83b789a03da8d885cd7deece (patch)
tree6386400d165dea2587dca2155a64c58c7e7325ec
parentd617f08c5be0e712a1de617c2ea6ef1d4d7ff64b (diff)
parentf69cc1a20d12d7470604b0b02587bef4e4a5ae11 (diff)
downloadperlweeklychallenge-club-a9cda3002313deae83b789a03da8d885cd7deece.tar.gz
perlweeklychallenge-club-a9cda3002313deae83b789a03da8d885cd7deece.tar.bz2
perlweeklychallenge-club-a9cda3002313deae83b789a03da8d885cd7deece.zip
Merge pull request #8496 from Firedrake/rogerbw-challenge-228
RogerBW solutions for challenge no. 228
-rwxr-xr-xchallenge-228/roger-bell-west/javascript/ch-1.js40
-rwxr-xr-xchallenge-228/roger-bell-west/javascript/ch-2.js27
-rw-r--r--challenge-228/roger-bell-west/kotlin/ch-1.kt30
-rw-r--r--challenge-228/roger-bell-west/kotlin/ch-2.kt32
-rwxr-xr-xchallenge-228/roger-bell-west/lua/ch-1.lua42
-rwxr-xr-xchallenge-228/roger-bell-west/lua/ch-2.lua34
-rwxr-xr-xchallenge-228/roger-bell-west/perl/ch-1.pl19
-rwxr-xr-xchallenge-228/roger-bell-west/perl/ch-2.pl28
-rw-r--r--challenge-228/roger-bell-west/postscript/ch-1.ps65
-rw-r--r--challenge-228/roger-bell-west/postscript/ch-2.ps99
-rwxr-xr-xchallenge-228/roger-bell-west/python/ch-1.py24
-rwxr-xr-xchallenge-228/roger-bell-west/python/ch-2.py27
-rwxr-xr-xchallenge-228/roger-bell-west/raku/ch-1.p614
-rwxr-xr-xchallenge-228/roger-bell-west/raku/ch-2.p624
-rwxr-xr-xchallenge-228/roger-bell-west/ruby/ch-1.rb27
-rwxr-xr-xchallenge-228/roger-bell-west/ruby/ch-2.rb31
-rwxr-xr-xchallenge-228/roger-bell-west/rust/ch-1.rs21
-rwxr-xr-xchallenge-228/roger-bell-west/rust/ch-2.rs31
-rw-r--r--challenge-228/roger-bell-west/tests.yaml33
19 files changed, 648 insertions, 0 deletions
diff --git a/challenge-228/roger-bell-west/javascript/ch-1.js b/challenge-228/roger-bell-west/javascript/ch-1.js
new file mode 100755
index 0000000000..411f73f85b
--- /dev/null
+++ b/challenge-228/roger-bell-west/javascript/ch-1.js
@@ -0,0 +1,40 @@
+#! /usr/bin/node
+
+"use strict"
+
+function uniquesum(a) {
+ let c = new Map();
+ for (let n of a) {
+ if (c.has(n)) {
+ c.set(n, c.get(n) + 1);
+ } else {
+ c.set(n, 1);
+ }
+ }
+ let t = 0;
+ for (const [k, v] of c) {
+ if (v == 1) {
+ t += k;
+ }
+ }
+ return t;
+}
+
+if (uniquesum([2, 1, 3, 2]) == 4) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (uniquesum([1, 1, 1, 1]) == 0) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (uniquesum([2, 1, 3, 4]) == 10) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-228/roger-bell-west/javascript/ch-2.js b/challenge-228/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..d60216aaac
--- /dev/null
+++ b/challenge-228/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,27 @@
+#! /usr/bin/node
+
+"use strict"
+
+function emptyarray(a0) {
+ let t = 0
+ let a = a0;
+ while (a.length > 0) {
+ const i = a.indexOf(Math.min(...a));
+ t += i + 1;
+ a.splice(i, 1);
+ }
+ return t;
+}
+
+if (emptyarray([3, 4, 2]) == 5) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (emptyarray([1, 2, 3]) == 3) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-228/roger-bell-west/kotlin/ch-1.kt b/challenge-228/roger-bell-west/kotlin/ch-1.kt
new file mode 100644
index 0000000000..668ada5155
--- /dev/null
+++ b/challenge-228/roger-bell-west/kotlin/ch-1.kt
@@ -0,0 +1,30 @@
+fun uniquesum(a: List<Int>): Int {
+ var c = mutableMapOf<Int, Int>().withDefault({0})
+ for (n in a) {
+ c.set(n, c.getValue(n) + 1)
+ }
+ return c.filter {(_, v) -> v == 1}.map {(k, _) -> k}.sum()
+}
+
+fun main() {
+
+ if (uniquesum(listOf(2, 1, 3, 2)) == 4) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (uniquesum(listOf(1, 1, 1, 1)) == 0) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (uniquesum(listOf(2, 1, 3, 4)) == 10) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+}
diff --git a/challenge-228/roger-bell-west/kotlin/ch-2.kt b/challenge-228/roger-bell-west/kotlin/ch-2.kt
new file mode 100644
index 0000000000..58a02bfe24
--- /dev/null
+++ b/challenge-228/roger-bell-west/kotlin/ch-2.kt
@@ -0,0 +1,32 @@
+fun emptyarray(a0: List<Int>): Int {
+ var t = 0
+ var a = a0.toMutableList()
+ while (a.size > 0) {
+ val mn = a.minOrNull()!!
+ for ((i, v) in a.withIndex()) {
+ if (v == mn) {
+ t += i + 1
+ a.removeAt(i)
+ break
+ }
+ }
+ }
+ return t
+}
+
+fun main() {
+
+ if (emptyarray(listOf(3, 4, 2)) == 5) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (emptyarray(listOf(1, 2, 3)) == 3) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+}
diff --git a/challenge-228/roger-bell-west/lua/ch-1.lua b/challenge-228/roger-bell-west/lua/ch-1.lua
new file mode 100755
index 0000000000..11812738e8
--- /dev/null
+++ b/challenge-228/roger-bell-west/lua/ch-1.lua
@@ -0,0 +1,42 @@
+#! /usr/bin/lua
+
+function uniquesum(a)
+ local c = {}
+ for _, v in ipairs(a) do
+ if c[v] == nil then
+ c[v] = 1
+ else
+ c[v] = c[v] + 1
+ end
+ end
+ local t = 0
+ for k, v in pairs(c) do
+ if v == 1 then
+ t = t + k
+ end
+ end
+ return t
+end
+
+
+if uniquesum({2, 1, 3, 2}) == 4 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if uniquesum({1, 1, 1, 1}) == 0 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if uniquesum({2, 1, 3, 4}) == 10 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-228/roger-bell-west/lua/ch-2.lua b/challenge-228/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..5b32f2d690
--- /dev/null
+++ b/challenge-228/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,34 @@
+#! /usr/bin/lua
+
+function emptyarray(a0)
+ local t = 0
+ local a = a0
+ while #a > 0 do
+ local mn = math.min(table.unpack(a))
+ for i, v in ipairs(a) do
+ if v == mn then
+ table.remove(a, i)
+ t = t + i
+ break
+ end
+ end
+ end
+ return t
+end
+
+
+
+if emptyarray({3, 4, 2}) == 5 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if emptyarray({1, 2, 3}) == 3 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-228/roger-bell-west/perl/ch-1.pl b/challenge-228/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..649424f6c6
--- /dev/null
+++ b/challenge-228/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use Test::More tests => 3;
+
+is(uniquesum([2, 1, 3, 2]), 4, 'example 1');
+is(uniquesum([1, 1, 1, 1]), 0, 'example 2');
+is(uniquesum([2, 1, 3, 4]), 10, 'example 3');
+
+use List::Util qw(sum0);
+
+sub uniquesum($a) {
+ my %c;
+ map {$c{$_}++} @{$a};
+ return sum0(grep {$c{$_} == 1} keys %c);
+}
diff --git a/challenge-228/roger-bell-west/perl/ch-2.pl b/challenge-228/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..3db9eebaf4
--- /dev/null
+++ b/challenge-228/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 => 2;
+
+is(emptyarray([3, 4, 2]), 5, 'example 1');
+is(emptyarray([1, 2, 3]), 3, 'example 2');
+
+use List::Util qw(min);
+
+sub emptyarray($a0) {
+ my $t = 0;
+ my @a = @{$a0};
+ while (scalar @a > 0) {
+ my $mn = min(@a);
+ foreach my $i (0..$#a) {
+ if ($a[$i] == $mn) {
+ $t += $i + 1;
+ splice(@a, $i, 1);
+ last;
+ }
+ }
+ }
+ return $t;
+}
diff --git a/challenge-228/roger-bell-west/postscript/ch-1.ps b/challenge-228/roger-bell-west/postscript/ch-1.ps
new file mode 100644
index 0000000000..b71c8b712b
--- /dev/null
+++ b/challenge-228/roger-bell-west/postscript/ch-1.ps
@@ -0,0 +1,65 @@
+%!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.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 {
+ /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
+
+/uniquesum {
+ 2 dict begin
+ /c 0 dict def
+ {
+ /v exch def
+ c v known {
+ c v c v get 1 add put
+ } {
+ c v 1 put
+ } ifelse
+ } forall
+ 0
+ c {
+ 1 eq {
+ add
+ } {
+ pop
+ } ifelse
+ } forall
+ end
+} bind def
+
+(uniquesum) test.start
+[2 1 3 2] uniquesum 4 eq test
+[1 1 1 1] uniquesum 0 eq test
+[2 1 3 4] uniquesum 10 eq test
+test.end
diff --git a/challenge-228/roger-bell-west/postscript/ch-2.ps b/challenge-228/roger-bell-west/postscript/ch-2.ps
new file mode 100644
index 0000000000..f35c55da64
--- /dev/null
+++ b/challenge-228/roger-bell-west/postscript/ch-2.ps
@@ -0,0 +1,99 @@
+%!PS
+
+% begin included library code
+% see https://codeberg.org/Firedrake/postscript-libraries/
+/listmin {
+ { min } 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.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
+
+/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
+
+
+% end included library code
+
+/emptyarray {
+ 0 dict begin
+ /a exch def
+ /t 0 def
+ {
+ a length 0 eq {
+ exit
+ } if
+ /mn a listmin def
+ a enumerate.array {
+ aload pop
+ mn eq {
+ /i exch def
+ /t t i add 1 add def
+ [
+ a aload length /l exch def
+ l i 1 add neg roll
+ pop
+ l 1 sub i roll
+ ] /a exch def
+ exit
+ } {
+ pop
+ } ifelse
+ } forall
+ } loop
+ t
+ end
+} bind def
+
+(emptyarray) test.start
+[3 4 2] emptyarray 5 eq test
+[1 2 3] emptyarray 3 eq test
+test.end
diff --git a/challenge-228/roger-bell-west/python/ch-1.py b/challenge-228/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..1e43d3ed80
--- /dev/null
+++ b/challenge-228/roger-bell-west/python/ch-1.py
@@ -0,0 +1,24 @@
+#! /usr/bin/python3
+
+from collections import defaultdict
+
+def uniquesum(a):
+ c = defaultdict(lambda: 0)
+ for n in a:
+ c[n] += 1
+ return sum([k for k, v in c.items() if v == 1])
+
+import unittest
+
+class TestUniquesum(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(uniquesum([2, 1, 3, 2]), 4, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(uniquesum([1, 1, 1, 1]), 0, 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(uniquesum([2, 1, 3, 4]), 10, 'example 3')
+
+unittest.main()
diff --git a/challenge-228/roger-bell-west/python/ch-2.py b/challenge-228/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..63e29db0c7
--- /dev/null
+++ b/challenge-228/roger-bell-west/python/ch-2.py
@@ -0,0 +1,27 @@
+#! /usr/bin/python3
+
+from collections import deque
+
+def emptyarray(a0):
+ t = 0
+ a = deque(a0)
+ while len(a) > 0:
+ mn = min(a)
+ for i, v in enumerate(a):
+ if v == mn:
+ t += i + 1
+ del a[i]
+ break
+ return t
+
+import unittest
+
+class TestEmptyarray(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(emptyarray([3, 4, 2]), 5, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(emptyarray([1, 2, 3]), 3, 'example 2')
+
+unittest.main()
diff --git a/challenge-228/roger-bell-west/raku/ch-1.p6 b/challenge-228/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..ca4848ca27
--- /dev/null
+++ b/challenge-228/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,14 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 3;
+
+is(uniquesum([2, 1, 3, 2]), 4, 'example 1');
+is(uniquesum([1, 1, 1, 1]), 0, 'example 2');
+is(uniquesum([2, 1, 3, 4]), 10, 'example 3');
+
+sub uniquesum(@a) {
+ my $c = bag(@a);
+ return $c.keys.grep({$c{$_} == 1}).sum();
+}
diff --git a/challenge-228/roger-bell-west/raku/ch-2.p6 b/challenge-228/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..e24c6c5cad
--- /dev/null
+++ b/challenge-228/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,24 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 2;
+
+is(emptyarray([3, 4, 2]), 5, 'example 1');
+is(emptyarray([1, 2, 3]), 3, 'example 2');
+
+sub emptyarray(@a0) {
+ my $t = 0;
+ my @a = @a0;
+ while @a.elems > 0 {
+ my $mn = @a.min;
+ for 0..@a.end -> $i {
+ if (@a[$i] == $mn) {
+ $t += $i + 1;
+ @a.splice($i, 1);
+ last;
+ }
+ }
+ }
+ return $t;
+}
diff --git a/challenge-228/roger-bell-west/ruby/ch-1.rb b/challenge-228/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..a2d0dec79a
--- /dev/null
+++ b/challenge-228/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,27 @@
+#! /usr/bin/ruby
+
+def uniquesum(a)
+ c = Hash.new(0)
+ a.each do |n|
+ c[n] += 1
+ end
+ return c.keys.find_all {|i| c[i] == 1}.inject(0) {|a, b| a + b}
+end
+
+require 'test/unit'
+
+class TestUniquesum < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(4, uniquesum([2, 1, 3, 2]))
+ end
+
+ def test_ex2
+ assert_equal(0, uniquesum([1, 1, 1, 1]))
+ end
+
+ def test_ex3
+ assert_equal(10, uniquesum([2, 1, 3, 4]))
+ end
+
+end
diff --git a/challenge-228/roger-bell-west/ruby/ch-2.rb b/challenge-228/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..1135204f66
--- /dev/null
+++ b/challenge-228/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,31 @@
+#! /usr/bin/ruby
+
+def emptyarray(a0)
+ t = 0
+ a = a0
+ while a.length > 0 do
+ mn = a.min
+ 0.upto(a.length - 1) do |i|
+ if a[i] == mn then
+ t += i + 1
+ a.delete_at(i)
+ break
+ end
+ end
+ end
+ return t
+end
+
+require 'test/unit'
+
+class TestEmptyarray < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(5, emptyarray([3, 4, 2]))
+ end
+
+ def test_ex2
+ assert_equal(3, emptyarray([1, 2, 3]))
+ end
+
+end
diff --git a/challenge-228/roger-bell-west/rust/ch-1.rs b/challenge-228/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..11a7a8c0d3
--- /dev/null
+++ b/challenge-228/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,21 @@
+use counter::Counter;
+
+#[test]
+fn test_ex1() {
+ assert_eq!(uniquesum(vec![2, 1, 3, 2]), 4);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(uniquesum(vec![1, 1, 1, 1]), 0);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(uniquesum(vec![2, 1, 3, 4]), 10);
+}
+
+fn uniquesum(a: Vec<u32>) -> u32 {
+ let c = a.into_iter().collect::<Counter<u32>>();
+ c.iter().filter(|(_k, v)| **v == 1usize).map(|(k, _v)| k).sum()
+}
diff --git a/challenge-228/roger-bell-west/rust/ch-2.rs b/challenge-228/roger-bell-west/rust/ch-2.rs
new file mode 100755
index 0000000000..9573413ddd
--- /dev/null
+++ b/challenge-228/roger-bell-west/rust/ch-2.rs
@@ -0,0 +1,31 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit
+
+use std::collections::VecDeque;
+
+#[test]
+fn test_ex1() {
+ assert_eq!(emptyarray(vec![3, 4, 2]), 5);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(emptyarray(vec![1, 2, 3]), 3);
+}
+
+fn emptyarray(a0: Vec<u32>) -> u32 {
+ let mut t = 0;
+ let mut a = VecDeque::from(a0);
+ while a.len() > 0 {
+ let mn = a.iter().min().unwrap();
+ let i = a
+ .iter()
+ .enumerate()
+ .filter(|(_i, v)| *v == mn)
+ .map(|(i, _v)| i)
+ .collect::<Vec<_>>()[0];
+ t += 1 + (i as u32);
+ a.remove(i);
+ }
+ t
+}
diff --git a/challenge-228/roger-bell-west/tests.yaml b/challenge-228/roger-bell-west/tests.yaml
new file mode 100644
index 0000000000..ab49a341ee
--- /dev/null
+++ b/challenge-228/roger-bell-west/tests.yaml
@@ -0,0 +1,33 @@
+---
+ch-1:
+ - function: uniquesum
+ arguments:
+ - 2
+ - 1
+ - 3
+ - 2
+ result: 4
+ - arguments:
+ - 1
+ - 1
+ - 1
+ - 1
+ result: 0
+ - arguments:
+ - 2
+ - 1
+ - 3
+ - 4
+ result: 10
+ch-2:
+ - function: emptyarray
+ arguments:
+ - 3
+ - 4
+ - 2
+ result: 5
+ - arguments:
+ - 1
+ - 2
+ - 3
+ result: 3