diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-10-17 15:50:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-17 15:50:21 +0100 |
| commit | c0437d38d472e445dc37924b9220b6c3da34fff5 (patch) | |
| tree | 7314948a34d730b3a58716b39d87f9a93011e8fc | |
| parent | 70539816fb240ac725e4689763b9851c6fce6788 (diff) | |
| parent | 1d23f91d86e7ebb5291c17fc99ec0fc2ca48b64a (diff) | |
| download | perlweeklychallenge-club-c0437d38d472e445dc37924b9220b6c3da34fff5.tar.gz perlweeklychallenge-club-c0437d38d472e445dc37924b9220b6c3da34fff5.tar.bz2 perlweeklychallenge-club-c0437d38d472e445dc37924b9220b6c3da34fff5.zip | |
Merge pull request #6923 from Firedrake/rogerbw-challenge-187
Solutions for challenge #187
| -rwxr-xr-x | challenge-187/roger-bell-west/javascript/ch-1.js | 54 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/javascript/ch-2.js | 87 | ||||
| -rw-r--r-- | challenge-187/roger-bell-west/kotlin/ch-1.kt | 49 | ||||
| -rw-r--r-- | challenge-187/roger-bell-west/kotlin/ch-2.kt | 51 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/lua/ch-2.lua | 97 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/perl/ch-1.pl | 45 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/perl/ch-2.pl | 43 | ||||
| -rw-r--r-- | challenge-187/roger-bell-west/postscript/ch-1.ps | 155 | ||||
| -rw-r--r-- | challenge-187/roger-bell-west/postscript/ch-2.ps | 222 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/python/ch-1.py | 43 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/python/ch-2.py | 40 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/raku/ch-1.p6 | 40 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/raku/ch-2.p6 | 38 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/ruby/ch-1.rb | 44 | ||||
| -rwxr-xr-x | challenge-187/roger-bell-west/ruby/ch-2.rb | 44 | ||||
| -rw-r--r-- | challenge-187/roger-bell-west/rust/ch-1.rs | 40 | ||||
| -rw-r--r-- | challenge-187/roger-bell-west/rust/ch-2.rs | 41 |
17 files changed, 1133 insertions, 0 deletions
diff --git a/challenge-187/roger-bell-west/javascript/ch-1.js b/challenge-187/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..e00346b98f --- /dev/null +++ b/challenge-187/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,54 @@ +#! /usr/bin/node + +"use strict" + +function s2date(ds) { + let dm = ds.split("-"); + if (dm.length == 2) { + return Date.UTC(2022, parseInt(dm[1])-1, parseInt(dm[0]), 0, 0, 0, 0)/86400000; + } +} + +function daystogether(a, b) { + let starts = [s2date(a[0]), s2date(b[0])]; + starts.sort(function(a,b) { + return a-b; + }); + let ends = [s2date(a[1]), s2date(b[1])]; + ends.sort(function(a,b) { + return a-b; + }); + if (ends[0] >= starts[1]) { + return ends[0]-starts[1]+1; + } else { + return 0; + } +} + +if (daystogether(["12-01", "20-01"], ["15-01", "18-01"]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (daystogether(["02-03", "12-03"], ["13-03", "14-03"]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (daystogether(["02-03", "12-03"], ["11-03", "15-03"]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (daystogether(["30-03", "05-04"], ["28-03", "02-04"]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-187/roger-bell-west/javascript/ch-2.js b/challenge-187/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..67529e6246 --- /dev/null +++ b/challenge-187/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,87 @@ +#! /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 magicaltriplets(a) { + let out = []; + let mv = 0; + for (let ai = 0; ai < a.length-2; ai++) { + for (let bi = ai+1; bi < a.length-1; bi++) { + for (let ci = bi+1; ci < a.length; ci++) { + if (a[ai] + a[bi] > a[ci] && + a[bi] + a[ci] > a[ai] && + a[ai] + a[ci] > a[bi]) { + let v = a[ai] + a[bi] + a[ci]; + if (v > mv) { + mv = v; + out = [a[ai], a[bi], a[ci]]; + } + } + } + } + } + out.sort(function(a,b) { + return b-a; + }); + return out; +} + +if (deepEqual(magicaltriplets([1, 2, 3, 2]), + [3, 2, 2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(magicaltriplets([1, 3, 2]), + [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(magicaltriplets([1, 1, 2, 3]), + [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(magicaltriplets([2, 4, 3]), + [4, 3, 2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} + +process.stdout.write("\n"); diff --git a/challenge-187/roger-bell-west/kotlin/ch-1.kt b/challenge-187/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..299c4db50c --- /dev/null +++ b/challenge-187/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,49 @@ +import java.time.LocalDate +import java.time.format.DateTimeFormatter +import java.time.temporal.ChronoUnit + +fun s2date(ds: String): LocalDate { + val formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy") + return LocalDate.parse(ds + "-2022", formatter) +} + +fun daystogether(a: List<String>, b: List<String>): Int { + var starts = listOf(s2date(a[0]), s2date(b[0])).sorted() + var ends = listOf(s2date(a[1]), s2date(b[1])).sorted() + if (ends[0] >= starts[1]) { + return ChronoUnit.DAYS.between(starts[1], ends[0]).toInt()+1 + } else { + return 0 + } +} + +fun main() { + if (daystogether(listOf("12-01", "20-01"), listOf("15-01", "18-01")) == + 4) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (daystogether(listOf("02-03", "12-03"), listOf("13-03", "14-03")) == + 0) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (daystogether(listOf("02-03", "12-03"), listOf("11-03", "15-03")) == + 2) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (daystogether(listOf("30-03", "05-04"), listOf("28-03", "02-04")) == + 4) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-187/roger-bell-west/kotlin/ch-2.kt b/challenge-187/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..93fc98e9d7 --- /dev/null +++ b/challenge-187/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,51 @@ +fun magicaltriplets(a: List<Int>): List<Int> { + var out = listOf<Int>() + var mv = 0 + for (ai in 0..a.size-3) { + for (bi in ai+1..a.size-2) { + for (ci in bi+1..a.size-1) { + if (a[ai] + a[bi] > a[ci] && + a[bi] + a[ci] > a[ai] && + a[ai] + a[ci] > a[bi]) { + var v = a[ai] + a[bi] + a[ci] + if (v > mv) { + mv = v + out = listOf(a[ai], a[bi], a[ci]) + } + } + } + } + } + return out.sorted().reversed().toList() +} + +fun main() { + if (magicaltriplets(listOf(1, 2, 3, 2)) == + listOf(3, 2, 2)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (magicaltriplets(listOf(1, 3, 2)) == + listOf<Int>()) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (magicaltriplets(listOf(1, 1, 2, 3)) == + listOf<Int>()) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (magicaltriplets(listOf(2, 4, 3)) == + listOf(4, 3, 2)) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-187/roger-bell-west/lua/ch-2.lua b/challenge-187/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..8aac5559d1 --- /dev/null +++ b/challenge-187/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,97 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +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 + + -- Check each key-value pair + -- We have to do this both ways in case we miss some. + -- TODO: Could probably be smarter and not check those we've + -- already checked though! + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + + return true +end +-- by hookenz at +-- https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console +function dump(o) + if type(o) == 'table' then + local s = '{ ' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. '['..k..'] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end +end + +function magicaltriplets(a) + local out = {} + local mv = 0 + for ai = 1,#a-2 do + for bi = ai+1,#a-1 do + for ci = bi+1,#a do + if a[ai] + a[bi] > a[ci] and + a[bi] + a[ci] > a[ai] and + a[ai] + a[ci] > a[bi] then + local v = a[ai] + a[bi] + a[ci] + if v > mv then + mv = v + out = {a[ai], a[bi], a[ci]} + end + end + end + end + end + table.sort(out, function (i,j) return j < i end) + return out +end + +if (recursive_compare(magicaltriplets({1, 2, 3, 2}), + {3, 2, 2})) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if (recursive_compare(magicaltriplets({1, 3, 2}), + {})) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if (recursive_compare(magicaltriplets({1, 1, 2, 3}), + {})) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if (recursive_compare(magicaltriplets({2, 4, 3}), + {4, 3, 2})) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-187/roger-bell-west/perl/ch-1.pl b/challenge-187/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..52ec4111b9 --- /dev/null +++ b/challenge-187/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,45 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use DateTime; +use DateTime::Format::Strptime; + +use Test::More tests => 4; + +is_deeply(daystogether(["12-01", "20-01"], ["15-01", "18-01"]), + 4, + 'example 1'); + +is_deeply(daystogether(["02-03", "12-03"], ["13-03", "14-03"]), + 0, + 'example 2'); + +is_deeply(daystogether(["02-03", "12-03"], ["11-03", "15-03"]), + 2, + 'example 3'); + +is_deeply(daystogether(["30-03", "05-04"], ["28-03", "02-04"]), + 4, + 'example 4'); + +sub daystogether($a,$b) { + my @starts = sort (s2date($a->[0]), s2date($b->[0])); + my @ends = sort (s2date($a->[1]), s2date($b->[1])); + if ($ends[0] > $starts[1]) { + return $ends[0]->delta_days($starts[1])->in_units('days')+1; + } else { + return 0; + } +} + +sub s2date($ds) { + my $strp = DateTime::Format::Strptime->new( + pattern => '%d-%m-%Y', + strict => 1, + time_zone => 'GMT', + ); + return $strp->parse_datetime("$ds-2022"); +} diff --git a/challenge-187/roger-bell-west/perl/ch-2.pl b/challenge-187/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..f0f55a7735 --- /dev/null +++ b/challenge-187/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 => 4; + +use Algorithm::Combinatorics qw(combinations); + +is_deeply(magicaltriplets([1, 2, 3, 2]), + [3, 2, 2], + 'example 1'); + +is_deeply(magicaltriplets([1, 3, 2]), + [], + 'example 2'); + +is_deeply(magicaltriplets([1, 1, 2, 3]), + [], + 'example 3'); + +is_deeply(magicaltriplets([2, 4, 3]), + [4, 3, 2], + 'example 4'); + +sub magicaltriplets($a) { + my $out = []; + my $mv = 0; + my $iter = combinations($a, 3); + while (my $b = $iter->next) { + if ($b->[0] + $b->[1] > $b->[2] && + $b->[1] + $b->[2] > $b->[0] && + $b->[0] + $b->[2] > $b->[1]) { + my $v = $b->[0] + $b->[1] + $b->[2]; + if ($v > $mv) { + $mv = $v; + $out = [$b->[0], $b->[1], $b->[2]]; + } + } + } + return [reverse sort @{$out}]; +} diff --git a/challenge-187/roger-bell-west/postscript/ch-1.ps b/challenge-187/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..3344baa22e --- /dev/null +++ b/challenge-187/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,155 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/quicksort { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + 0 arr length 1 sub quicksort.main + arr + end +} 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 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} 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.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.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 + +/ymd2jd { + 4 dict begin + aload pop + /d exch def + /m exch def + /y exch def + /mn m 14 sub 12 idiv def + y 4800 add mn add 1461 mul 4 idiv + mn 12 mul neg 2 sub m add 367 mul 12 idiv add + y 4900 add mn add 100 idiv 3 mul 4 idiv sub + d add + 32075 sub + end +} bind def + +/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 + + +% end included library code + +/daystogether { + 2 dict begin + /starts 2 array def + /ends 2 array def + dup 0 get s2jd starts exch 1 exch put + 1 get s2jd ends exch 1 exch put + dup 0 get s2jd starts exch 0 exch put + 1 get s2jd ends exch 0 exch put + starts quicksort + ends quicksort + ends 0 get starts 1 get ge { + ends 0 get starts 1 get sub 1 add + } { + 0 + } ifelse + end +} bind def + +/s2jd { + [ exch + 2022 exch + dup + 3 2 getinterval cvi exch + 0 2 getinterval cvi + ] + ymd2jd +} bind def + +(daystogether) test.start +[ (12-01) (20-01) ] [ (15-01) (18-01) ] daystogether 4 eq test +[ (02-03) (12-03) ] [ (13-03) (14-03) ] daystogether 0 eq test +[ (02-03) (12-03) ] [ (11-03) (15-03) ] daystogether 2 eq test +[ (30-03) (05-04) ] [ (28-03) (02-04) ] daystogether 4 eq test +test.end diff --git a/challenge-187/roger-bell-west/postscript/ch-2.ps b/challenge-187/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..b4f239de20 --- /dev/null +++ b/challenge-187/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,222 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/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 + +/quicksort { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} 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 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} 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.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.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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/reverse { + 3 dict begin + /str exch def + str length 0 gt { + /temp str 0 get def + /i 0 def + str length 2 idiv { + /temp str i get def + str i str str length i sub 1 sub get put + str str length i sub 1 sub temp put + /i i 1 add def + } repeat + } if + str + end +} bind def + +/magicaltriplets { + 6 dict begin + /a exch def + /out 0 array def + /mv 0 def + 0 + 1 + a length 3 sub { + /ai exch def + ai 1 add + 1 + a length 2 sub { + /bi exch def + bi 1 add + 1 + a length 1 sub { + /ci exch def + a ai get a bi get add a ci get gt + a bi get a ci get add a ai get gt and + a ai get a ci get add a bi get gt and { + /v a ai get a bi get add a ci get add def + v mv gt { + /mv v def + /out [ a ai get a bi get a ci get ] def + } if + } if + } for + } for + } for + out quicksort reverse + end +} bind def + +(magicaltriplets) test.start +[ 1 2 3 2 ] magicaltriplets [ 3 2 2 ] deepeq test +[ 1 3 2 ] magicaltriplets [ ] deepeq test +[ 1 1 2 3 ] magicaltriplets [ ] deepeq test +[ 2 4 3 ] magicaltriplets [ 4 3 2 ] deepeq test +test.end diff --git a/challenge-187/roger-bell-west/python/ch-1.py b/challenge-187/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..b7be1cd5c4 --- /dev/null +++ b/challenge-187/roger-bell-west/python/ch-1.py @@ -0,0 +1,43 @@ +#! /usr/bin/python3 + +import unittest + +from datetime import date, timedelta +import re + +def s2date(ds): + dm = re.findall(r"\d+", ds) + if len(dm) == 2: + return date(2022, int(dm[1]), int(dm[0])) + +def daystogether(a, b): + starts = sorted([s2date(a[0]), s2date(b[0])]) + ends = sorted([s2date(a[1]), s2date(b[1])]) + if ends[0] >= starts[1]: + return (ends[0]-starts[1]).days+1 + else: + return 0 + +class TestDaystogether(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(daystogether(["12-01", "20-01"], ["15-01", "18-01"]), + 4, + 'example 1') + + def test_ex2(self): + self.assertEqual(daystogether(["02-03", "12-03"], ["13-03", "14-03"]), + 0, + 'example 2') + + def test_ex3(self): + self.assertEqual(daystogether(["02-03", "12-03"], ["11-03", "15-03"]), + 2, + 'example 3') + + def test_ex4(self): + self.assertEqual(daystogether(["30-03", "05-04"], ["28-03", "02-04"]), + 4, + 'example 4') + +unittest.main() diff --git a/challenge-187/roger-bell-west/python/ch-2.py b/challenge-187/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..0686ce96cd --- /dev/null +++ b/challenge-187/roger-bell-west/python/ch-2.py @@ -0,0 +1,40 @@ +#! /usr/bin/python3 + +import unittest + +from itertools import combinations + +def magicaltriplets(a): + out = [] + mv = 0 + for b in combinations(a, 3): + if b[0] + b[1] > b[2] and b[1] + b[2] > b[0] and b[0] + b[2] > b[1]: + v = sum(b) + if v > mv: + mv = v + out = b + return list(reversed(sorted(out))) + +class TestMagicaltriplets(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(magicaltriplets([1, 2, 3, 2]), + [3, 2, 2], + 'example 1') + + def test_ex2(self): + self.assertEqual(magicaltriplets([1, 3, 2]), + [], + 'example 2') + + def test_ex3(self): + self.assertEqual(magicaltriplets([1, 1, 2, 3]), + [], + 'example 3') + + def test_ex4(self): + self.assertEqual(magicaltriplets([2, 4, 3]), + [4, 3, 2], + 'example 4') + +unittest.main() diff --git a/challenge-187/roger-bell-west/raku/ch-1.p6 b/challenge-187/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..f90728fc46 --- /dev/null +++ b/challenge-187/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,40 @@ +#! /usr/bin/perl6 + +use Test; + +plan 4; + +is-deeply(daystogether(["12-01", "20-01"], ["15-01", "18-01"]), + 4, + 'example 1'); + +is-deeply(daystogether(["02-03", "12-03"], ["13-03", "14-03"]), + 0, + 'example 2'); + +is-deeply(daystogether(["02-03", "12-03"], ["11-03", "15-03"]), + 2, + 'example 3'); + +is-deeply(daystogether(["30-03", "05-04"], ["28-03", "02-04"]), + 4, + 'example 4'); + +sub daystogether(@a,@b) { + my @starts = [s2date(@a[0]), s2date(@b[0])].sort; + my @ends = [s2date(@a[1]), s2date(@b[1])].sort; + if (@ends[0] > @starts[1]) { + return @ends[0]-@starts[1]+1; + } else { + return 0; + } +} + +sub s2date($ds) { + if ($ds ~~ /^(\d+)\-(\d+)$/) { + return Date.new(year => 2022, + month => $1, + day => $0); + } + die "$ds is not a recognisable date\n"; +} diff --git a/challenge-187/roger-bell-west/raku/ch-2.p6 b/challenge-187/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..805ed2f684 --- /dev/null +++ b/challenge-187/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,38 @@ +#! /usr/bin/perl6 + +use Test; + +plan 4; + +is-deeply(magicaltriplets([1, 2, 3, 2]), + [3, 2, 2], + 'example 1'); + +is-deeply(magicaltriplets([1, 3, 2]), + [], + 'example 2'); + +is-deeply(magicaltriplets([1, 1, 2, 3]), + [], + 'example 3'); + +is-deeply(magicaltriplets([2, 4, 3]), + [4, 3, 2], + 'example 4'); + +sub magicaltriplets(@a) { + my @out; + my $mv = 0; + for @a.combinations(3) -> @b { + if (@b[0] + @b[1] > @b[2] && + @b[1] + @b[2] > @b[0] && + @b[0] + @b[2] > @b[1]) { + my $v = @b.sum; + if ($v > $mv) { + $mv = $v; + @out = @b; + } + } + } + return Array(@out.sort.reverse); +} diff --git a/challenge-187/roger-bell-west/ruby/ch-1.rb b/challenge-187/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..fa83e9dad2 --- /dev/null +++ b/challenge-187/roger-bell-west/ruby/ch-1.rb |
