diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-10 18:37:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-10 18:37:42 +0000 |
| commit | 43c5cd48a6369f58f1e40b310cf0a56b63c449aa (patch) | |
| tree | 487fcf7efc22e90b58e2c1e219175b1d9976996d /challenge-199 | |
| parent | 79c9fc30ab7cad1bd61c3b82a60b0a4d7b174ae7 (diff) | |
| parent | 4b3ae31959af409b55ae29e72b35371d81da6604 (diff) | |
| download | perlweeklychallenge-club-43c5cd48a6369f58f1e40b310cf0a56b63c449aa.tar.gz perlweeklychallenge-club-43c5cd48a6369f58f1e40b310cf0a56b63c449aa.tar.bz2 perlweeklychallenge-club-43c5cd48a6369f58f1e40b310cf0a56b63c449aa.zip | |
Merge pull request #7393 from Firedrake/rogerbw-challenge-199
Solutions for challenge #199
Diffstat (limited to 'challenge-199')
19 files changed, 669 insertions, 0 deletions
diff --git a/challenge-199/roger-bell-west/javascript/ch-1.js b/challenge-199/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..93218d5032 --- /dev/null +++ b/challenge-199/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,38 @@ +#! /usr/bin/node + +"use strict" + +function goodpairs(l) { + let c = 0; + let k = new Map(); + for (let i of l) { + if (k.has(i)) { + k.set(i, k.get(i) + 1); + } else { + k.set(i, 1); + } + } + for (let v of k.values()) { + c += v * (v - 1); + } + return c / 2; +} + +if (goodpairs([1, 2, 3, 1, 1, 3]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (goodpairs([1, 2, 3]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (goodpairs([1, 1, 1, 1]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-199/roger-bell-west/javascript/ch-2.js b/challenge-199/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..be44f6d4c2 --- /dev/null +++ b/challenge-199/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,33 @@ +#! /usr/bin/node + +"use strict" + +function goodtriplets(a, x, y, z) { + let c = 0; + for (let i = 0; i <= a.length - 3; i++) { + for (let j = i + 1; j <= a.length - 2; j++) { + if (Math.abs(a[i] - a[j]) <= x) { + for (let k = j + 1; k <= a.length - 1; k++) { + if (Math.abs(a[j] - a[k]) <= y && + Math.abs(a[i] - a[k]) <= z) { + c++; + } + } + } + } + } + return c; +} + +if (goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (goodtriplets([1, 1, 2, 2, 3], 0, 0, 1) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-199/roger-bell-west/kotlin/ch-1.kt b/challenge-199/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..c59e10195c --- /dev/null +++ b/challenge-199/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,38 @@ +fun goodpairs(l: List<Int>): Int { + var c = 0 + var k = mutableMapOf<Int, Int>() + for (i in l) { + if (k.containsKey(i)) { + k[i] = k[i]!!+1 + } else { + k[i] = 1 + } + } + for (v in k.values) { + c += v * (v - 1) + } + return c / 2 +} + +fun main() { + + if (goodpairs(listOf(1, 2, 3, 1, 1, 3)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (goodpairs(listOf(1, 2, 3)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (goodpairs(listOf(1, 1, 1, 1)) == 6) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-199/roger-bell-west/kotlin/ch-2.kt b/challenge-199/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..65dc2cedf5 --- /dev/null +++ b/challenge-199/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,35 @@ +import kotlin.math.* + +fun goodtriplets(a: List<Int>, x: Int, y: Int, z: Int): Int { + var c = 0 + for (i in 0..a.size-3) { + for (j in i+1..a.size-2) { + if (abs(a[i] - a[j]) <= x) { + for (k in j+1..a.size-1) { + if (abs(a[j] - a[k]) <= y && + abs(a[i] - a[k]) <= z) { + c += 1 + } + } + } + } + } + return c +} + +fun main() { + + if (goodtriplets(listOf(3, 0, 1, 1, 9, 7), 7, 2, 3) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (goodtriplets(listOf(1, 1, 2, 2, 3), 0, 0, 1) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-199/roger-bell-west/lua/ch-1.lua b/challenge-199/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..3931fe1154 --- /dev/null +++ b/challenge-199/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,39 @@ +#! /usr/bin/lua + +function goodpairs(l) + local c = 0 + local k = {} + for dummy, i in ipairs(l) do + if k[i] == nil then + k[i] = 1 + else + k[i] = k[i] + 1 + end + end + for dummy, v in ipairs(k) do + c = c + v * (v - 1) + end + return c / 2 +end + +if goodpairs({1, 2, 3, 1, 1, 3}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if goodpairs({1, 2, 3}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if goodpairs({1, 1, 1, 1}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-199/roger-bell-west/lua/ch-2.lua b/challenge-199/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..63d4cad318 --- /dev/null +++ b/challenge-199/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,33 @@ +#! /usr/bin/lua + +function goodtriplets(a, x, y, z) + local c = 0 + for i = 1, #a - 2 do + for j = i + 1, #a - 1 do + if math.abs(a[i] - a[j]) <= x then + for k = j + 1, #a do + if math.abs(a[j] - a[k]) <= y and + math.abs(a[i] - a[k]) <= z then + c = c + 1 + end + end + end + end + end + return c +end + +if goodtriplets({3, 0, 1, 1, 9, 7}, 7, 2, 3) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if goodtriplets({1, 1, 2, 2, 3}, 0, 0, 1) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-199/roger-bell-west/perl/ch-1.pl b/challenge-199/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..6c8febdd0f --- /dev/null +++ b/challenge-199/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(goodpairs([1, 2, 3, 1, 1, 3]), 4, 'example 1'); +is(goodpairs([1, 2, 3]), 0, 'example 2'); +is(goodpairs([1, 1, 1, 1]), 6, 'example 3'); + +sub goodpairs($l) { + my $c = 0; + my %k; + map {$k{$_}++} @{$l}; + foreach my $v (values %k) { + $c += $v * ($v-1); + } + return $c / 2; +} diff --git a/challenge-199/roger-bell-west/perl/ch-2.pl b/challenge-199/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..ac81a4f099 --- /dev/null +++ b/challenge-199/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,27 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3), 4, 'example 1'); +is(goodtriplets([1, 1, 2, 2, 3], 0, 0, 1), 0, 'example 2'); + +sub goodtriplets($a, $x, $y, $z) { + my $c = 0; + foreach my $i (0..$#{$a} - 2) { + foreach my $j ($i+1..$#{$a} - 1) { + if (abs($a->[$i] - $a->[$j]) <= $x) { + foreach my $k ($j+1..$#{$a}) { + if (abs($a->[$j] - $a->[$k]) <= $y && + abs($a->[$i] - $a->[$k]) <= $z) { + $c++; + } + } + } + } + } + return $c; +} diff --git a/challenge-199/roger-bell-west/postscript/ch-1.ps b/challenge-199/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..15731eb3b4 --- /dev/null +++ b/challenge-199/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,70 @@ +%!PS + +% begin included library code +% see https://github.com/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 + +/values { % dict -> array of dict values + [ exch + { + exch pop + } forall + ] +} 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 + +/goodpairs { + 1 dict begin + /k 0 dict def + { + dup k exch known { + dup k exch get + } { + 0 + } ifelse + k 3 1 roll 1 add put + } forall + 0 + k values { + dup 1 sub mul add + } forall + 2 idiv + end +} bind def + +(goodpairs) test.start +[1 2 3 1 1 3] goodpairs 4 eq test +[1 2 3] goodpairs 0 eq test +[1 1 1 1] goodpairs 6 eq test +test.end diff --git a/challenge-199/roger-bell-west/postscript/ch-2.ps b/challenge-199/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..dcc459d9bc --- /dev/null +++ b/challenge-199/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,68 @@ +%!PS + +% begin included library code +% see https://github.com/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 + +/goodtriplets { + 7 dict begin + /z exch def + /y exch def + /x exch def + /a exch def + 0 + 0 1 a length 3 sub { + /i exch def + i 1 add 1 a length 2 sub { + /j exch def + a i get a j get sub abs x le { + j 1 add 1 a length 1 sub { + /k exch def + a j get a k get sub abs y le + a i get a k get sub abs z le and { + 1 add + } if + } for + } if + } for + } for + end + +} bind def + +(goodtriplets) test.start +[3 0 1 1 9 7] 7 2 3 goodtriplets 4 eq test +[1 1 2 2 3] 0 0 1 goodtriplets 0 eq test +test.end diff --git a/challenge-199/roger-bell-west/python/ch-1.py b/challenge-199/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..1f35b4fc87 --- /dev/null +++ b/challenge-199/roger-bell-west/python/ch-1.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +import unittest + +from collections import defaultdict + +def goodpairs(l): + c = 0 + k = defaultdict(lambda: 0) + for i in l: + k[i] += 1 + for v in k.values(): + c += v * (v - 1) + return c / 2 + +class TestGoodpairs(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(goodpairs([1, 2, 3, 1, 1, 3]), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(goodpairs([1, 2, 3]), 0, 'example 2') + + def test_ex3(self): + self.assertEqual(goodpairs([1, 1, 1, 1]), 6, 'example 3') + +unittest.main() diff --git a/challenge-199/roger-bell-west/python/ch-2.py b/challenge-199/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..b96dac98dc --- /dev/null +++ b/challenge-199/roger-bell-west/python/ch-2.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 + +import unittest + +def goodtriplets(a, x, y, z): + c = 0 + for i in range(len(a) - 2): + for j in range(i + 1, len(a) - 1): + if abs(a[i] - a[j]) <= x: + for k in range(j + 1, len(a)): + if abs(a[j] - a[k]) <= y and abs(a[i] - a[k]) <= z: + c += 1 + return c + +class TestGoodtriplets(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(goodtriplets([1, 1, 2, 2, 3], 0, 0, 1), 0, 'example 2') + +unittest.main() diff --git a/challenge-199/roger-bell-west/raku/ch-1.p6 b/challenge-199/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..9bcf412b53 --- /dev/null +++ b/challenge-199/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(goodpairs([1, 2, 3, 1, 1, 3]), 4, 'example 1'); +is(goodpairs([1, 2, 3]), 0, 'example 2'); +is(goodpairs([1, 1, 1, 1]), 6, 'example 3'); + +sub goodpairs(@l) { + my $c = 0; + my %k; + map {%k{$_}++}, @l; + for %k.values() -> $v { + $c += $v * ($v-1); + } + return $c / 2; +} diff --git a/challenge-199/roger-bell-west/raku/ch-2.p6 b/challenge-199/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..47fb60b20e --- /dev/null +++ b/challenge-199/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3), 4, 'example 1'); +is(goodtriplets([1, 1, 2, 2, 3], 0, 0, 1), 0, 'example 2'); + +sub goodtriplets(@a, $x, $y, $z) { + my $c = 0; + for (0..@a.end - 2) -> $i { + for ($i+1..@a.end - 1) -> $j { + if (abs(@a[$i] - @a[$j]) <= $x) { + for ($j+1..@a.end) -> $k { + if (abs(@a[$j] - @a[$k]) <= $y && + abs(@a[$i] - @a[$k]) <= $z) { + $c++; + } + } + } + } + } + return $c; +} diff --git a/challenge-199/roger-bell-west/ruby/ch-1.rb b/challenge-199/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..53cc7318bd --- /dev/null +++ b/challenge-199/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def goodpairs(l) + c = 0 + k = Hash.new(0) + l.each do |i| + k[i] += 1 + end + k.values.each do |v| + c += v * (v - 1) + end + return c / 2 +end + +class TestGoodpairs < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, goodpairs([1, 2, 3, 1, 1, 3])) + end + + def test_ex2 + assert_equal(0, goodpairs([1, 2, 3])) + end + + def test_ex3 + assert_equal(6, goodpairs([1, 1, 1, 1])) + end + +end diff --git a/challenge-199/roger-bell-west/ruby/ch-2.rb b/challenge-199/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..f016291568 --- /dev/null +++ b/challenge-199/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,32 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def goodtriplets(a, x, y, z) + c = 0 + 0.upto(a.length - 3) do |i| + (i + 1).upto(a.length - 2) do |j| + if (a[i] - a[j]).abs <= x then + (j + 1).upto(a.length - 1) do |k| + if (a[j] - a[k]).abs <= y && + (a[i] - a[k]).abs <= z then + c += 1 + end + end + end + end + end + return c +end + +class TestGoodtriplets < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3)) + end + + def test_ex2 + assert_equal(0, goodtriplets([1, 1, 2, 2, 3], 0, 0, 1)) + end + +end diff --git a/challenge-199/roger-bell-west/rust/ch-1.rs b/challenge-199/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..8c7f28ce1f --- /dev/null +++ b/challenge-199/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,32 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashMap; + +#[test] +fn test_ex1() { + assert_eq!(goodpairs(vec![1, 2, 3, 1, 1, 3]), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(goodpairs(vec![1, 2, 3]), 0); +} + +#[test] +fn test_ex3() { + assert_eq!(goodpairs(vec![1, 1, 1, 1]), 6); +} + +fn goodpairs(l: Vec<usize>) -> usize { + let mut c = 0; + let mut k: HashMap<usize, usize> = HashMap::new(); + for i in l { + let en = k.entry(i).or_insert(0); + *en += 1; + } + for v in k.values() { + c += v * (v - 1); + } + c / 2 +} diff --git a/challenge-199/roger-bell-west/rust/ch-2.rs b/challenge-199/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..83c624d79a --- /dev/null +++ b/challenge-199/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,28 @@ +#! /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!(goodtriplets(vec![3, 0, 1, 1, 9, 7], 7, 2, 3), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(goodtriplets(vec![1, 1, 2, 2, 3], 0, 0, 1), 0); +} + +fn goodtriplets(a: Vec<isize>, x: isize, y: isize, z: isize) -> usize { + let mut c = 0; + for i in 0..=a.len() - 3 { + for j in (i + 1)..=a.len() - 2 { + if (a[i] - a[j]).abs() <= x { + for k in (j + 1)..=a.len() - 1 { + if (a[j] - a[k]).abs() <= y && (a[i] - a[k]).abs() <= z { + c += 1; + } + } + } + } + } + c +} diff --git a/challenge-199/roger-bell-west/tests.yaml b/challenge-199/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..6ca095a9bc --- /dev/null +++ b/challenge-199/roger-bell-west/tests.yaml @@ -0,0 +1,50 @@ +--- +ch-1: + - function: goodpairs + arguments: + - 1 + - 2 + - 3 + - 1 + - 1 + - 3 + result: 4 + - function: goodpairs + arguments: + - 1 + - 2 + - 3 + result: 0 + - function: goodpairs + arguments: + - 1 + - 1 + - 1 + - 1 + result: 6 +ch-2: + - function: goodtriplets + arguments: + - - 3 + - 0 + - 1 + - 1 + - 9 + - 7 + - 7 + - 2 + - 3 + multiarg: true + result: 4 + - function: goodtriplets + arguments: + - - 1 + - 1 + - 2 + - 2 + - 3 + - 0 + - 0 + - 1 + multiarg: true + result: 0 |
