diff options
19 files changed, 1023 insertions, 0 deletions
diff --git a/challenge-219/roger-bell-west/javascript/ch-1.js b/challenge-219/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..de21513378 --- /dev/null +++ b/challenge-219/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,51 @@ +#! /usr/bin/node + +"use strict" + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function sortedsquares(lst) { + let q = lst.map(x => x * x); + q.sort(function(a,b) { + return a-b; + }); + return q; +} + +if (deepEqual(sortedsquares([-2, -1, 0, 3, 4]), [0, 1, 4, 9, 16])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(sortedsquares([5, -4, -1, 3, 6]), [1, 9, 16, 25, 36])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-219/roger-bell-west/javascript/ch-2.js b/challenge-219/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..7aab6ecda1 --- /dev/null +++ b/challenge-219/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,46 @@ +#! /usr/bin/node + +"use strict" + +function travelexpenditure(costs, days0) { + let days = days0; + days.sort(function(a,b) { + return a-b; + }); + const validities = [1, 7, 30]; + let stack = []; + stack.push([0, days]); + let cheapest = days.length * costs[0]; + while (stack.length > 0) { + const c = stack.shift(); + if (c[1].length == 0) { + if (c[0] < cheapest) { + cheapest = c[0]; + } + } else { + if (c[0] >= cheapest) { + continue; + } + const start = c[1][0]; + for (let i = 0; i <= 2; i++) { + const ed = start + validities[i] - 1; + let dtd = c[1].filter(x => x > ed); + stack.push([c[0] + costs[i], dtd]); + } + } + } + return cheapest; +} + +if (travelexpenditure([2, 7, 25], [1, 5, 6, 7, 9, 15]) == 11) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (travelexpenditure([2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31]) == 20) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-219/roger-bell-west/kotlin/ch-1.kt b/challenge-219/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..4182f087a9 --- /dev/null +++ b/challenge-219/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,20 @@ +fun sortedsquares(lst: List<Int>): List<Int> { + return lst.map {it * it}.sorted() +} + +fun main() { + + if (sortedsquares(listOf(-2, -1, 0, 3, 4)) == listOf(0, 1, 4, 9, 16)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (sortedsquares(listOf(5, -4, -1, 3, 6)) == listOf(1, 9, 16, 25, 36)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-219/roger-bell-west/kotlin/ch-2.kt b/challenge-219/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..f8a485b136 --- /dev/null +++ b/challenge-219/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,43 @@ +fun travelexpenditure(costs: List<Int>, days0: List<Int>): Int { + val days = ArrayList(days0).sorted() + val validities = listOf(1, 7, 30) + var stack = ArrayDeque<Pair<Int, List<Int>>>() + stack.add(Pair(0, days)) + var cheapest = days.size * costs[0] + while (stack.size > 0) { + val c = stack.removeFirst() + if (c.second.size == 0) { + if (c.first < cheapest) { + cheapest = c.first + } + } else { + if (c.first >= cheapest) { + continue + } + val start = c.second[0] + for (i in 0..2) { + val ed = start + validities[i] - 1 + var dtd = c.second.filter{it > ed} + stack.add(Pair(c.first + costs[i], dtd)) + } + } + } + return cheapest +} + +fun main() { + + if (travelexpenditure(listOf(2, 7, 25), listOf(1, 5, 6, 7, 9, 15)) == 11) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (travelexpenditure(listOf(2, 7, 25), listOf(1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31)) == 20) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-219/roger-bell-west/lua/ch-1.lua b/challenge-219/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..241c855e73 --- /dev/null +++ b/challenge-219/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,54 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for _, k in ipairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +function sortedsquares(lst) + local q = {} + for _, j in ipairs(lst) do + table.insert(q, j * j) + end + table.sort(q) + return q +end + +if recursive_compare(sortedsquares({-2, -1, 0, 3, 4}), {0, 1, 4, 9, 16}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(sortedsquares({5, -4, -1, 3, 6}), {1, 9, 16, 25, 36}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-219/roger-bell-west/lua/ch-2.lua b/challenge-219/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..303ebc51db --- /dev/null +++ b/challenge-219/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,64 @@ +#! /usr/bin/lua + +function propersize(t) + local l=0 + for k,v in pairs(t) do + l = l + 1 + end + return l +end + +function keys(t) + local a = {} + for k, v in pairs(t) do + table.insert(a, k) + end + return a +end + +function travelexpenditure(costs, days0) + local days = days0 + table.sort(days) + local validities = {1, 7, 30} + local stack = {} + table.insert(stack, {0, days}) + local cheapest = #days * costs[1] + while #stack > 0 do + local c = table.remove(stack, 1) + if #(c[2]) == 0 then + if c[1] < cheapest then + cheapest = c[1] + end + else + if c[1] < cheapest then + local start = c[2][1] + for i = 1, 3 do + local ed = start + validities[i] - 1 + local dtd = {} + for _, j in ipairs(c[2]) do + if j > ed then + table.insert(dtd , j) + end + end + table.insert(stack, {c[1] + costs[i], dtd}) + end + end + end + end + return cheapest +end + +if travelexpenditure({2, 7, 25}, {1, 5, 6, 7, 9, 15}) == 11 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if travelexpenditure({2, 7, 25}, {1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31}) == 20 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-219/roger-bell-west/perl/ch-1.pl b/challenge-219/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..9a71315baa --- /dev/null +++ b/challenge-219/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,14 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(sortedsquares([-2, -1, 0, 3, 4]), [0, 1, 4, 9, 16], 'example 1'); +is_deeply(sortedsquares([5, -4, -1, 3, 6]), [1, 9, 16, 25, 36], 'example 2'); + +sub sortedsquares($lst) { + return [sort {$a <=> $b} map{$_ * $_} @{$lst}]; +} diff --git a/challenge-219/roger-bell-west/perl/ch-2.pl b/challenge-219/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..8d97e17bf9 --- /dev/null +++ b/challenge-219/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,38 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(travelexpenditure([2, 7, 25], [1, 5, 6, 7, 9, 15]), 11, 'example 1'); +is(travelexpenditure([2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31]), 20, 'example 2'); + +sub travelexpenditure($costs, $days0) { + my @days = sort {$a <=> $b} @{$days0}; + my @validities = (1, 7, 30); + my @stack; + push @stack, {costdone => 0, daystodo => \@days}; + my $cheapest = (scalar @days) * $costs->[0]; + while (scalar @stack > 0) { + my $c = shift @stack; + if (scalar @{$c->{daystodo}} == 0) { + if ($c->{costdone} < $cheapest) { + $cheapest = $c->{costdone}; + } + } else { + if ($c->{costdone} >= $cheapest) { + next; + } + my $start = $c->{daystodo}[0]; + foreach my $i (0..2) { + my $ed = $start + $validities[$i] - 1; + my @dtd = grep {$_ > $ed} @{$c->{daystodo}}; + push @stack,{costdone => $c->{costdone} + $costs->[$i], + daystodo => \@dtd}; + } + } + } + return $cheapest; +} diff --git a/challenge-219/roger-bell-west/postscript/ch-1.ps b/challenge-219/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..3852d988e1 --- /dev/null +++ b/challenge-219/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,203 @@ +%!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 + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} 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 + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + 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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + + +% end included library code + +/sortedsquares { + { dup mul } map quicksort +} bind def + +(sortedsquares) test.start +[-2 -1 0 3 4] sortedsquares [0 1 4 9 16] deepeq test +[5 -4 -1 3 6] sortedsquares [1 9 16 25 36] deepeq test +test.end diff --git a/challenge-219/roger-bell-west/postscript/ch-2.ps b/challenge-219/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..ce9dcb6550 --- /dev/null +++ b/challenge-219/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,188 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + end +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + 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 + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/apop.left { % [a b c] -> [b c] a + dup 0 get exch + [ exch aload length -1 roll pop ] exch +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + 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 + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + + +% end included library code + +/travelexpenditure { + 9 dict begin + quicksort /days exch def + /costs exch def + /validities [ 1 7 30 ] def + /stack [ [ 0 days ] ] def + /cheapest days length costs 0 get mul def + { + stack length 0 eq { + exit + } if + stack apop.left /c exch def /stack exch def + c 1 get length 0 eq { + c 0 get cheapest lt { + /cheapest c 0 get def + } if + } { + c 0 get cheapest lt { + /start c 1 get 0 get def + 0 1 2 { + /i exch def + /ed start validities i get add 1 sub def + /stack stack [ + c 0 get costs i get add + c 1 get { ed gt } filter + ] apush.right def + } for + } if + } ifelse + } loop + cheapest + end +} bind def + +(travelexpenditure) test.start +[2 7 25] [1 5 6 7 9 15] travelexpenditure 11 eq test +[2 7 25] [1 2 3 5 7 10 11 12 14 20 30 31] travelexpenditure 20 eq test +test.end diff --git a/challenge-219/roger-bell-west/python/ch-1.py b/challenge-219/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..b6737234e7 --- /dev/null +++ b/challenge-219/roger-bell-west/python/ch-1.py @@ -0,0 +1,18 @@ +#! /usr/bin/python3 + +def sortedsquares(lst): + q = [i * i for i in lst] + q.sort() + return q + +import unittest + +class TestSortedsquares(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(sortedsquares([-2, -1, 0, 3, 4]), [0, 1, 4, 9, 16], 'example 1') + + def test_ex2(self): + self.assertEqual(sortedsquares([5, -4, -1, 3, 6]), [1, 9, 16, 25, 36], 'example 2') + +unittest.main() diff --git a/challenge-219/roger-bell-west/python/ch-2.py b/challenge-219/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..d747e7ad69 --- /dev/null +++ b/challenge-219/roger-bell-west/python/ch-2.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +from collections import deque + +def travelexpenditure(costs, days0): + days = sorted(days0) + validities = [1, 7, 30] + stack = deque() + stack.append([0, days]) + cheapest = len(days) * costs[0] + while len(stack) > 0: + c = stack.popleft() + if len(c[1]) == 0: + if c[0] < cheapest: + cheapest = c[0] + else: + if c[0] >= cheapest: + continue + start = c[1][0] + for i in range(3): + end = start + validities[i] - 1 + dtd = list(filter(lambda x: x > end, c[1])) + stack.append([c[0] + costs[i], dtd]) + return cheapest + +import unittest + +class TestTravelexpenditure(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(travelexpenditure([2, 7, 25], [1, 5, 6, 7, 9, 15]), 11, 'example 1') + + def test_ex2(self): + self.assertEqual(travelexpenditure([2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31]), 20, 'example 2') + +unittest.main() diff --git a/challenge-219/roger-bell-west/raku/ch-1.p6 b/challenge-219/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..e6f6c68c72 --- /dev/null +++ b/challenge-219/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,12 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(sortedsquares([-2, -1, 0, 3, 4]), [0, 1, 4, 9, 16], 'example 1'); +is-deeply(sortedsquares([5, -4, -1, 3, 6]), [1, 9, 16, 25, 36], 'example 2'); + +sub sortedsquares(@lst) { + return @lst.map({$_ * $_}).sort({$^a <=> $^b}).Array; +} diff --git a/challenge-219/roger-bell-west/raku/ch-2.p6 b/challenge-219/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..6a450d6c54 --- /dev/null +++ b/challenge-219/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,36 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(travelexpenditure([2, 7, 25], [1, 5, 6, 7, 9, 15]), 11, 'example 1'); +is(travelexpenditure([2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31]), 20, 'example 2'); + +sub travelexpenditure(@costs, @days0) { + my @days = @days0.sort({$^a <=> $^b}); + my @validities = [1, 7, 30]; + my @stack; + @stack.push({costdone => 0, daystodo => @days}); + my $cheapest = @days.elems * @costs[0]; + while (@stack.elems > 0) { + my %c = @stack.shift; + if (%c{'daystodo'}.elems == 0) { + if (%c{'costdone'} < $cheapest) { + $cheapest = %c{'costdone'}; + } + } else { + if (%c{'costdone'} >= $cheapest) { + next; + } + my $start = %c{'daystodo'}[0]; + for 0..2 -> $i { + my $ed = $start + @validities[$i] - 1; + my @dtd = %c{'daystodo'}.grep({$_ > $ed}); + @stack.push({costdone => %c{'costdone'} + @costs[$i], + daystodo => @dtd}); + } + } + } + return $cheapest; +} diff --git a/challenge-219/roger-bell-west/ruby/ch-1.rb b/challenge-219/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..fc5ba50a46 --- /dev/null +++ b/challenge-219/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,19 @@ +#! /usr/bin/ruby + +def sortedsquares(lst) + return lst.collect {|x| x * x}.sort +end + +require 'test/unit' + +class TestSortedsquares < Test::Unit::TestCase + + def test_ex1 + assert_equal([0, 1, 4, 9, 16], sortedsquares([-2, -1, 0, 3, 4])) + end + + def test_ex2 + assert_equal([1, 9, 16, 25, 36], sortedsquares([5, -4, -1, 3, 6])) + end + +end diff --git a/challenge-219/roger-bell-west/ruby/ch-2.rb b/challenge-219/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..e4d72b318a --- /dev/null +++ b/challenge-219/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,42 @@ +#! /usr/bin/ruby + +def travelexpenditure(costs, days0) + days = days0.sort + validities = [1, 7, 30] + stack = [] + stack.push([0, days]) + cheapest = days.length * costs[0] + while stack.length > 0 do + c = stack.shift + if c[1].length == 0 then + if c[0] < cheapest then + cheapest = c[0] + end + else + if c[0] >= cheapest then + next + end + start = c[1][0] + 0.upto(2) do |i| + ed = start + validities[i] - 1 + dtd = c[1].find_all {|x| x > ed} + stack.push([c[0] + costs[i], dtd]) + end + end + end + return cheapest +end + +require 'test/unit' + +class TestTravelexpenditure < Test::Unit::TestCase + + def test_ex1 + assert_equal(11, travelexpenditure([2, 7, 25], [1, 5, 6, 7, 9, 15])) + end + + def test_ex2 + assert_equal(20, travelexpenditure([2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31])) + end + +end diff --git a/challenge-219/roger-bell-west/rust/ch-1.rs b/challenge-219/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..5bfa669c67 --- /dev/null +++ b/challenge-219/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,18 @@ +#! /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!(sortedsquares(vec![-2, -1, 0, 3, 4]), vec![0, 1, 4, 9, 16]); +} + +#[test] +fn test_ex2() { + assert_eq!(sortedsquares(vec![5, -4, -1, 3, 6]), vec![1, 9, 16, 25, 36]); +} + +fn sortedsquares(lst: Vec<i32>) -> Vec<i32> { + let mut q = lst.iter().map(|i| i * i).collect::<Vec<i32>>(); + q.sort(); + q +} diff --git a/challenge-219/roger-bell-west/rust/ch-2.rs b/challenge-219/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..9a0ac721e6 --- /dev/null +++ b/challenge-219/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,61 @@ +#! /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!(travelexpenditure(vec![2, 7, 25], vec![1, 5, 6, 7, 9, 15]), 11); +} + +#[test] +fn test_ex2() { + assert_eq!( + travelexpenditure( + vec![2, 7, 25], + vec![1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31] + ), + 20 + ); +} + +struct Costy { + costdone: u32, + daystodo: Vec<u32>, +} + +fn travelexpenditure(costs: Vec<u32>, days0: Vec<u32>) -> u32 { + let mut days = days0; + days.sort(); + let validities = vec![1, 7, 30]; + let mut stack = VecDeque::new(); + stack.push_back(Costy { costdone: 0, daystodo: days.clone() }); + let mut cheapest = (days.len() as u32) * costs[0]; + while stack.len() > 0 { + let c = stack.pop_front().unwrap(); + if c.daystodo.len() == 0 { + if c.costdone < cheapest { + cheapest = c.costdone; + } + } else { + if c.costdone >= cheapest { + continue; + } + let start = c.daystodo[0]; + for i in 0..=2 { + let end = start + validities[i] - 1; + let dtd = c + .daystodo + .iter() + .filter(|x| **x > end) + .map(|i| *i) + .collect::<Vec<u32>>(); + stack.push_back(Costy { + costdone: c.costdone + costs[i], + daystodo: dtd, + }); |
