diff options
23 files changed, 1043 insertions, 0 deletions
diff --git a/challenge-285/roger-bell-west/crystal/ch-1.cr b/challenge-285/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..b9fd7825d4 --- /dev/null +++ b/challenge-285/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,17 @@ +#! /usr/bin/crystal + +def noconnection(a) + os = Set.new(a.map {|x| x[0]}) + is = Set.new(a.map {|x| x[1]}) + (is - os).to_a[0] +end + +require "spec" +describe "noconnection" do + it "test_ex1" do + noconnection([["B", "C"], ["D", "B"], ["C", "A"]]).should eq "A" + end + it "test_ex2" do + noconnection([["A", "Z"]]).should eq "Z" + end +end diff --git a/challenge-285/roger-bell-west/crystal/ch-2.cr b/challenge-285/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..8443eda967 --- /dev/null +++ b/challenge-285/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,62 @@ +#! /usr/bin/crystal + +# from https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists#Crystal + +def cartesian_product(a, b) + return a.flat_map { |i| b.map { |j| [i, j] } } +end + +def cartesian_product(l) + if l.size <= 1 + return l + elsif l.size == 2 + return cartesian_product(l[0], l[1]) + end + return l[0].flat_map { |i| + cartesian_product(l[1..]).map { |j| + [i, j].flatten + } + } +end + +def makingchange(a) + coins = [1, 5, 10, 25, 50] + max = coins.map { |x| (a / x).to_i } + pat = Array(Array(Int32)).new + 0.upto(coins.size - 1) do |i| + if max[i] > 0 + al = Array.new(max[i] + 1, 0) + al.fill(0..) { |i| i } + pat.push(al) + else + break + end + end + ct = 0 + cartesian_product(pat).each do |combo| + t = 0 + combo.each_with_index do |c, i| + t += c * coins[i] + if t > a + break + end + end + if t == a + ct += 1 + end + end + ct +end + +require "spec" +describe "makingchange" do + it "test_ex1" do + makingchange(9).should eq 2 + end + it "test_ex2" do + makingchange(15).should eq 6 + end + it "test_ex3" do + makingchange(100).should eq 292 + end +end diff --git a/challenge-285/roger-bell-west/javascript/ch-1.js b/challenge-285/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..503eafd812 --- /dev/null +++ b/challenge-285/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,27 @@ +#! /usr/bin/node + +"use strict" + +function noconnection(a) { + let os = new Set; + let is = new Set; + for (let x of a) { + os.add(x[0]); + is.add(x[1]); + } + const difference = [...is].filter(i => !os.has(i)); + return difference[0]; +} + +if (noconnection([['B', 'C'], ['D', 'B'], ['C', 'A']]) == 'A') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (noconnection([['A', 'Z']]) == 'Z') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-285/roger-bell-west/javascript/ch-2.js b/challenge-285/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..c1941b4101 --- /dev/null +++ b/challenge-285/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,50 @@ +#! /usr/bin/node + +"use strict" + +// by rsp at https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript +const cartesian = + (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat()))); + +function makingchange(a) { + const coins = [1, 5, 10, 25, 50]; + const max = coins.map(x => Math.floor(a / x)); + let pat = []; + for (let i = 0; i < coins.length; i++) { + if (max[i] > 0) { + pat.push(Array(max[i] + 1).fill().map((element, index) => index)); + } else { + break; + } + } + let ct = 0; + for (let combo of cartesian(...pat)) { + let t = 0; + combo.forEach((c, i) => { + t += c * coins[i]; + }); + if (t == a) { + ct++; + } + } + return ct; +} + +if (makingchange(9) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (makingchange(15) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (makingchange(100) == 292) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-285/roger-bell-west/kotlin/ch-1.kt b/challenge-285/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..940817c897 --- /dev/null +++ b/challenge-285/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,22 @@ +fun noconnection(a: List<List<Char>>): Char { + val os = a.map{it[0]}.toSet() + val iis = a.map{it[1]}.toSet() + return iis.subtract(os).toList()[0] +} + +fun main() { + + if (noconnection(listOf(listOf('B', 'C'), listOf('D', 'B'), listOf('C', 'A'))) == 'A') { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (noconnection(listOf(listOf('A', 'Z'))) == 'Z') { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-285/roger-bell-west/kotlin/ch-2.kt b/challenge-285/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..61cedf8895 --- /dev/null +++ b/challenge-285/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,79 @@ +fun nAryCartesianProduct(lists: List<List<Int>>): List<List<Int>> { + var c = ArrayList<Int>() + var cm = ArrayList<Int>() + for (i in lists) { + cm.add(i.size - 1) + c.add(0) + } + var out = ArrayList<List<Int>>() + var ex = false + while (!ex) { + var o = ArrayList<Int>() + for (i in 0..c.size - 1) { + o.add(lists[i][c[i]]) + } + out.add(o.toList()) + var ss = c.size-1 + while (true) { + c[ss] += 1 + if (c[ss] > cm[ss]) { + if (ss == 0) { + ex = true + break + } + c[ss] = 0 + ss-- + } else { + break + } + } + } + return out.toList() +} + +fun makingchange(a: Int): Int { + val coins = listOf(1, 5, 10, 25, 50) + val max = coins.map{a / it} + var pat = ArrayList<List<Int>>() + for (i in 0..coins.size - 1) { + if (max[i] > 0) { + pat.add((0..max[i]).toList()) + } else { + break + } + } + var ct = 0 + for (combo in nAryCartesianProduct(pat)) { + var t = 0 + combo.forEachIndexed{i, c -> + t += c * coins[i] + } + if (t == a) { + ct += 1 + } + } + return ct +} + +fun main() { + + if (makingchange(9) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (makingchange(15) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (makingchange(100) == 292) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-285/roger-bell-west/lua/ch-1.lua b/challenge-285/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..9634b42f4d --- /dev/null +++ b/challenge-285/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,34 @@ +#! /usr/bin/lua + +function noconnection(a) + local os = {}; + local iss = {}; + for _, x in ipairs(a) do + os[x[1]] = true + iss[x[2]] = true + end + local out + for x, _ in pairs(iss) do + if os[x] ~= nil then + iss[x] = nil + else + out = x + end + end + return out +end + +if noconnection({{"B", "C"}, {"D", "B"}, {"C", "A"}}) == "A" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if noconnection({{"A", "Z"}}) == "Z" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-285/roger-bell-west/lua/ch-2.lua b/challenge-285/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..3d51e8fb78 --- /dev/null +++ b/challenge-285/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,73 @@ +#! /usr/bin/lua + +-- from https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists#Lua +-- support: +function T(t) return setmetatable(t, {__index=table}) end +table.clone = function(t) local s=T{} for k,v in ipairs(t) do s[k]=v end return s end +table.reduce = function(t,f,acc) for i=1,#t do acc=f(t[i],acc) end return acc end + +-- implementation: +local function cartesian(sets) + local temp, prod = T{}, T{} + local function descend(depth) + for _,v in ipairs(sets[depth]) do + temp[depth] = v + if (depth==#sets) then prod[#prod+1]=temp:clone() else descend(depth+1) end + end + end + descend(1) + return prod +end + +function makingchange(a) + local coins = {1, 5, 10, 25, 50} + local pat = {} + for _, x in ipairs(coins) do + local max = math.floor(a / x) + if max > 0 then + local t = {} + for i = 0, max do + table.insert(t, i) + end + table.insert(pat, t) + else + break + end + end + local ct = 0 + for _, combo in ipairs(cartesian(pat)) do + local t = 0 + for i, c in ipairs(combo) do + t = t + c * coins[i] + if t > a then + break + end + end + if t == a then + ct = ct + 1 + end + end + return ct +end + +if makingchange(9) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if makingchange(15) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if makingchange(100) == 292 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-285/roger-bell-west/perl/ch-1.pl b/challenge-285/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..2449dff843 --- /dev/null +++ b/challenge-285/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 => 2; + +is(noconnection([['B', 'C'], ['D', 'B'], ['C', 'A']]), 'A', 'example 1'); +is(noconnection([['A', 'Z']]), 'Z', 'example 2'); + +sub noconnection($a) { + my %os = map {$_->[0] => 1} @{$a}; + my %is = map {$_->[1] => 1} @{$a}; + foreach my $k (keys %os) { + delete $is{$k}; + } + return (keys %is)[0]; +} diff --git a/challenge-285/roger-bell-west/perl/ch-2.pl b/challenge-285/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..81ea6313d0 --- /dev/null +++ b/challenge-285/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,68 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(makingchange(9), 2, 'example 1'); +is(makingchange(15), 6, 'example 2'); +is(makingchange(100), 292, 'example 3'); + +sub cartesianproduct($lists) { + my $sl = scalar @{$lists}; + my @c = (0) x $sl; + my @cm = map {scalar @{$lists->[$_]} - 1} (0 .. $sl - 1); + my @out; + my $ex = 0; + while (!$ex) { + my @o; + foreach my $i (0 .. $sl - 1) { + push @o, $lists->[$i][$c[$i]]; + } + push @out, \@o; + my $ss = $sl - 1; + while (1) { + $c[$ss]++; + if ($c[$ss] > $cm[$ss]) { + if ($ss == 0) { + $ex = 1; + last; + } + $c[$ss] = 0; + $ss--; + } else { + last; + } + } + } + return \@out; +} + +sub makingchange($a) { + my @coins = (1, 5, 10, 25, 50); + my @mx = map {int($a / $_)} @coins; + my @pat; + foreach my $i (0 .. $#coins) { + if ($mx[$i] > 0) { + push @pat, [0 .. $mx[$i]]; + } else { + last; + } + } + my $ct; + foreach my $combo (@{cartesianproduct(\@pat)}) { + my $t = 0; + while (my ($i, $c) = each @{$combo}) { + $t += $c * $coins[$i]; + if ($t > $a) { + last; + } + } + if ($t == $a) { + $ct++; + } + } + return $ct; +} diff --git a/challenge-285/roger-bell-west/postscript/ch-1.ps b/challenge-285/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..f548e6b3ca --- /dev/null +++ b/challenge-285/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,93 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/set.difference { + 4 dict begin + /s 0 dict def + /b exch def + /a exch def + a keys { + /k exch def + b k known not { + s k true put + } if + } forall + s + end +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} 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 + +/toset { % array -> dict of (value, true) + << exch + { + true + } forall + >> +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/noconnection { + 0 dict begin + /a exch def + /os a { 0 get } map toset def + /is a { 1 get } map toset def + is os set.difference keys 0 get + end +} bind def + +(noconnection) test.start +[[(B) (C)] [(D) (B)] [(C) (A)]] noconnection (A) eq test +[[(A) (Z)]] noconnection (Z) eq test +test.end diff --git a/challenge-285/roger-bell-west/postscript/ch-2.ps b/challenge-285/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..84bbab63bc --- /dev/null +++ b/challenge-285/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,137 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} bind def + +/cartesianproduct { % [ [ a b ] [ c d ] ] -> [ [ a c ] [ a d ] [ b c ] [ b d ] ] + 5 dict begin + /pat exch def + /c [ pat length { 0 } repeat ] def + /cm [ pat { length 1 sub } forall ] def + /ex false def + [ + { + ex { + exit + } if + [ + 0 1 c length 1 sub { + /i exch def + pat i get c i get get + } for + ] + /ss c length 1 sub def + { + c ss c ss get 1 add put + c ss get cm ss get gt { + ss 0 eq { + /ex true def + exit + } if + c ss 0 put + /ss ss 1 sub def + } { + exit + } ifelse + } loop + } loop + end + ] +} 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 + +/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 + +/makingchange { + 0 dict begin + /a exch def + /coins [ 1 5 10 25 50 ] def + /mx coins { a exch idiv } map def + /pat [ + 0 1 coins length 1 sub { + /i exch def + mx i get 0 gt { + [ 0 1 mx i get {} for ] + } { + exit + } ifelse + } for + ] def + 0 + pat cartesianproduct { + /combo exch def + /t 0 def + combo enumerate.array { + aload pop + /c exch def + /i exch def + /t c coins i get mul t add def + t a gt { + exit + } if + } forall + t a eq { + 1 add + } if + } forall + end +} bind def + +(makingchange) test.start +9 makingchange 2 eq test +15 makingchange 6 eq test +% 100 makingchange 292 eq test +test.end + diff --git a/challenge-285/roger-bell-west/python/ch-1.py b/challenge-285/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..7ed30223c4 --- /dev/null +++ b/challenge-285/roger-bell-west/python/ch-1.py @@ -0,0 +1,18 @@ +#! /usr/bin/python3 + +def noconnection(a): + os = set(x[0] for x in a) + iis = set(x[1] for x in a) + return list(iis.difference(os))[0] + +import unittest + +class TestNoconnection(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(noconnection([["B", "C"], ["D", "B"], ["C", "A"]]), "A", 'example 1') + + def test_ex2(self): + self.assertEqual(noconnection([["A", "Z"]]), "Z", 'example 2') + +unittest.main() diff --git a/challenge-285/roger-bell-west/python/ch-2.py b/challenge-285/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..108d73ad3d --- /dev/null +++ b/challenge-285/roger-bell-west/python/ch-2.py @@ -0,0 +1,39 @@ +#! /usr/bin/python3 + +from itertools import product +from math import floor + +def makingchange(a): + coins = [1, 5, 10, 25, 50] + mx = [floor(a / x) for x in coins] + pat = [] + for i in range(len(coins)): + if mx[i] > 0: + pat.append([x for x in range(mx[i] + 1)]) + else: + break + ct = 0 + for combo in product(*pat): + t = 0 + for i, c in enumerate(combo): + t += c * coins[i] + if t > a: + break + if t == a: + ct += 1 + return ct + +import unittest + +class TestMakingchange(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(makingchange(9), 2, 'example 1') + + def test_ex2(self): + self.assertEqual(makingchange(15), 6, 'example 2') + + def test_ex3(self): + self.assertEqual(makingchange(100), 292, 'example 3') + +unittest.main() diff --git a/challenge-285/roger-bell-west/raku/ch-1.p6 b/challenge-285/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..c52059f930 --- /dev/null +++ b/challenge-285/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,14 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(noconnection([['B', 'C'], ['D', 'B'], ['C', 'A']]), 'A', 'example 1'); +is(noconnection([['A', 'Z'], ]), 'Z', 'example 2'); + +sub noconnection(@a) { + my %os = Set.new(@a.map({$_[0]})); + my %is = Set.new(@a.map({$_[1]})); + (%is (-) %os)[0] +} diff --git a/challenge-285/roger-bell-west/raku/ch-2.p6 b/challenge-285/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..2949fd5e07 --- /dev/null +++ b/challenge-285/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,37 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(makingchange(9), 2, 'example 1'); +is(makingchange(15), 6, 'example 2'); +is(makingchange(100), 292, 'example 3'); + +sub makingchange($a) { + my @coins = [1, 5, 10, 25, 50]; + my @mx = @coins.map({floor($a / $_)}); + my @pat; + for 0 .. @coins.end -> $i { + if @mx[$i] > 0 { + @pat.push([0 .. @mx[$i]]); + } else { + last; + } + } + my $ct; + for [X] @pat -> @combo { + my $t = 0; + for @combo.kv -> $i, $c { + $t += $c * @coins[$i]; + if ($t > $a) { + last; + } + } + if ($t == $a) { + $ct++; + } + } + $ct +} + diff --git a/challenge-285/roger-bell-west/ruby/ch-1.rb b/challenge-285/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..44d24cef80 --- /dev/null +++ b/challenge-285/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,23 @@ +#! /usr/bin/ruby + +require 'set' + +def noconnection(a) + os = Set.new(a.map {|x| x[0]}) + is = Set.new(a.map {|x| x[1]}) + (is - os).to_a[0] +end + +require 'test/unit' + +class TestNoconnection < Test::Unit::TestCase + + def test_ex1 + assert_equal('A', noconnection([['B', 'C'], ['D', 'B'], ['C', 'A']])) + end + + def test_ex2 + assert_equal('Z', noconnection([['A', 'Z']])) + end + +end diff --git a/challenge-285/roger-bell-west/ruby/ch-2.rb b/challenge-285/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..98e531e8a7 --- /dev/null +++ b/challenge-285/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,48 @@ +#! /usr/bin/ruby + +def makingchange(a) + coins = [1, 5, 10, 25, 50] + max = coins.map { |x| (a / x).to_i } + pat = [] + 0.upto(coins.size - 1) do |i| + if max[i] > 0 + al = Array.new(max[i] + 1, 0) + al.fill(0..) { |i| i } + pat.push(al) + else + break + end + end + ct = 0 + pat[0].product(*pat[1..-1]).each do |combo| + t = 0 + combo.each_with_index do |c, i| + t += c * coins[i] + if t > a + break + end + end + if t == a + ct += 1 + end + end + ct +end + +require 'test/unit' + +class TestMakingchange < Test::Unit::TestCase + +# def test_ex1 +# assert_equal(2, makingchange(9)) +# end +# +# def test_ex2 +# assert_equal(6, makingchange(15)) +# end + + def test_ex3 + assert_equal(292, makingchange(100)) + end + +end diff --git a/challenge-285/roger-bell-west/rust/ch-1.rs b/challenge-285/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..0bc05878f6 --- /dev/null +++ b/challenge-285/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,20 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!(noconnection(vec![vec!['B', 'C'], vec!['D', 'B'], vec!['C', 'A']]), 'A'); +} + +#[test] +fn test_ex2() { + assert_eq!(noconnection(vec![vec!['A', 'Z']]), 'Z'); +} + +fn noconnection(a: Vec<Vec<char>>) -> char { + let os = a.iter().map(|x| x[0]).collect::<HashSet<_>>(); + let is = a.iter().map(|x| x[1]).collect::<HashSet<_>>(); + *is.difference(&os).collect::<Vec<_>>()[0] +} diff --git a/challenge-285/roger-bell-west/rust/ch-2.rs b/challenge-285/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..19ecb8d084 --- /dev/null +++ b/challenge-285/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,44 @@ +use itertools::Itertools; + +#[test] +fn test_ex1() { + assert_eq!(makingchange(9), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(makingchange(15), 6); +} + +#[test] +fn test_ex3() { + assert_eq!(makingchange(100), 292); +} + +fn makingchange(a: u32) -> usize { + let coins = vec![1, 5, 10, 25, 50]; + let max = coins.clone().into_iter().map(|x| a / x).collect::<Vec<_>>(); + let mut pat = Vec::new(); + for i in 0 .. coins.len() { + if max[i] > 0 { + pat.push((0 ..= max[i]).collect::<Vec<_>>()); + } else { + break; + } + } + let mut ct = 0; + for combo in pat.iter().multi_cartesian_product() { + let mut t = 0; + for (i, c) in combo.iter().enumerate() { + t += *c * coins[i]; + if t > a { + break; + } + } + if t == a { + ct += 1; + } + } + ct +} + diff --git a/challenge-285/roger-bell-west/scala/ch-1.scala b/challenge-285/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..dae05b9e0d --- /dev/null +++ b/challenge-285/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,23 @@ + +object Noconnection { + def noconnection(a: List[List[Char]]): Char = { + val os = a.map( n => n(0) ).toSet + val is = a.map( n => n(1) ).toSet + is.diff(os).toList(0) + } + def main(args: Array[String]) { + if (noconnection(List(List('B', 'C'), List('D', 'B'), List('C', 'A'))) == 'A') { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (noconnection(List(List('A', 'Z'))) == 'Z') { + print("Pass") + } else { + print("Fail") + } + println("") + + } +}< |
