diff options
| author | Roger Bell_West <roger@firedrake.org> | 2023-11-22 10:12:58 +0000 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2023-11-22 10:12:58 +0000 |
| commit | cff6b7e06388a30f03100c03b8b2d88e0bb92cab (patch) | |
| tree | e20d90743c0ad4bc2faab9bd16b92ef2af8371ae | |
| parent | c42b89630cf7082c105df06276d9de6ea6ccd4d7 (diff) | |
| download | perlweeklychallenge-club-cff6b7e06388a30f03100c03b8b2d88e0bb92cab.tar.gz perlweeklychallenge-club-cff6b7e06388a30f03100c03b8b2d88e0bb92cab.tar.bz2 perlweeklychallenge-club-cff6b7e06388a30f03100c03b8b2d88e0bb92cab.zip | |
RogerBW solutions for challenge no. 244
21 files changed, 1173 insertions, 0 deletions
diff --git a/challenge-244/roger-bell-west/javascript/ch-1.js b/challenge-244/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..4304464493 --- /dev/null +++ b/challenge-244/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,65 @@ +#! /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 countsmaller(nums) { + let b = [...nums]; + b.sort(function(a,b) { + return a-b; + }); + let sm = new Map; + let l = 0; + b.forEach((e, i) => { + if (i == 0 || e != l) { + sm.set(e, i); + l = e; + } + }); + return nums.map(n => sm.get(n)); +} + +if (deepEqual(countsmaller([8, 1, 2, 2, 3]), [4, 0, 1, 1, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(countsmaller([6, 5, 4, 8]), [2, 1, 0, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(countsmaller([2, 2, 2]), [0, 0, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-244/roger-bell-west/javascript/ch-2.js b/challenge-244/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..f0599b7a7e --- /dev/null +++ b/challenge-244/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,52 @@ +#! /usr/bin/node + +"use strict" + +function combinations(arr, k) { + let c = []; + for (let i = 0; i < k; i++) { + c.push(i); + } + c.push(arr.length); + c.push(0); + let out = []; + while (true) { + let inner = []; + for (let i = k-1; i >= 0; i--) { + inner.push(arr[c[i]]); + } + out.push(inner); + let j = 0; + while (c[j] + 1 == c[j + 1]) { + c[j] = j; + j += 1; + } + if (j >= k) { + break; + } + c[j] += 1; + } + return out; +} + +function grouphero(nums0) { + let nums = [...nums0]; + nums.sort(function(a,b) { + return b-a; + }); + let sum = 0; + for (let l = 1; l <= nums.length; l++) { + for (let c of combinations(nums, l)) { + const h = c[c.length - 1]; + sum += h * h * c[0]; + } + } + return sum; +} + +if (grouphero([2, 1, 4]) == 141) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-244/roger-bell-west/kotlin/ch-1.kt b/challenge-244/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..e496c94cd3 --- /dev/null +++ b/challenge-244/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,35 @@ +fun countsmaller(nums: List<Int>): List<Int> { + val b = nums.sorted() + var sm = mutableMapOf<Int, Int>() + var l = 0 + b.forEachIndexed {i, e -> if (i == 0 || e != l) { + sm[e] = i + l = e + } + } + return nums.map{n -> sm.getValue(n)} +} + + +fun main() { + + if (countsmaller(listOf(8, 1, 2, 2, 3)) == listOf(4, 0, 1, 1, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (countsmaller(listOf(6, 5, 4, 8)) == listOf(2, 1, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (countsmaller(listOf(2, 2, 2)) == listOf(0, 0, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-244/roger-bell-west/kotlin/ch-2.kt b/challenge-244/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..b9e246e62f --- /dev/null +++ b/challenge-244/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,49 @@ +fun combinations(arr: List<Int>, k: Int): List<List<Int>> { + var c = ArrayList<Int>() + for (i in 0 .. k-1) { + c.add(i) + } + c.add(arr.size) + c.add(0) + var out = ArrayList<List<Int>>() + while (true) { + var inner = ArrayList<Int>() + for (i in k-1 downTo 0) { + inner.add(arr[c[i]]) + } + out.add(inner.toList()) + var j = 0 + while (c[j] + 1 == c[j + 1]) { + c[j] = j + j += 1 + } + if (j >= k) { + break + } + c[j] += 1 + } + return out.toList() +} + +fun grouphero(nums0: List<Int>): Int { + val nums = nums0.sorted().reversed() + var sum = 0 + for (l in 1 .. nums.size) { + for (c in combinations(nums, l)) { + val h = c.last() + sum += h * h * c[0] + } + } + return sum +} + +fun main() { + + if (grouphero(listOf(2, 1, 4)) == 141) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-244/roger-bell-west/lua/ch-1.lua b/challenge-244/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..cc59650a31 --- /dev/null +++ b/challenge-244/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,98 @@ +#! /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 combinations(arr, k) + local c = {} + for i = 1, k do + table.insert(c, i) + end + table.insert(c, #arr + 1) + table.insert(c, 0) + local out = {} + while true do + local inner = {} + for i = k, 1, -1 do + table.insert(inner, arr[c[i]]) + end + table.insert(out, inner) + local j = 1 + while c[j] + 1 == c[j + 1] do + c[j] = j + j = j + 1 + end + if j > k then + break + end + c[j] = c[j] + 1 + end + return ipairs(out) +end + +function countsmaller(nums) + local b = nums + table.sort(b, function (i,j) return j < i end) + local sm = {} + local l = 0 + for i1, e in ipairs(b) do + local i = i1 - 1 + if i == 0 or e ~= l then + sm[e] = i + l = e + end + end + local out = {} + for _i, n in ipairs(nums) do + table.insert(out, sm[n]) + end + return out +end + +if recursive_compare(countsmaller({8, 1, 2, 2, 3}), {4, 0, 1, 1, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(countsmaller({6, 5, 4, 8}), {2, 1, 0, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(countsmaller({2, 2, 2}), {0, 0, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-244/roger-bell-west/lua/ch-2.lua b/challenge-244/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..98d12c02a6 --- /dev/null +++ b/challenge-244/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,48 @@ +#! /usr/bin/lua + +function combinations(arr, k) + local c = {} + for i = 1, k do + table.insert(c, i) + end + table.insert(c, #arr + 1) + table.insert(c, 0) + local out = {} + while true do + local inner = {} + for i = k, 1, -1 do + table.insert(inner, arr[c[i]]) + end + table.insert(out, inner) + local j = 1 + while c[j] + 1 == c[j + 1] do + c[j] = j + j = j + 1 + end + if j > k then + break + end + c[j] = c[j] + 1 + end + return ipairs(out) +end + +function grouphero(nums0) + local nums = nums0 + table.sort(nums, function (i,j) return j < i end) + local sum = 0 + for l = 1, #nums do + for _, c in combinations(nums, l) do + sum = sum + c[#c] * c[#c] * c[1] + end + end + return sum +end + +if grouphero({2, 1, 4}) == 141 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-244/roger-bell-west/perl/ch-1.pl b/challenge-244/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..29279711bc --- /dev/null +++ b/challenge-244/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(countsmaller([8, 1, 2, 2, 3]), [4, 0, 1, 1, 3], 'example 1'); +is_deeply(countsmaller([6, 5, 4, 8]), [2, 1, 0, 3], 'example 2'); +is_deeply(countsmaller([2, 2, 2]), [0, 0, 0], 'example 3'); + +sub countsmaller($nums) { + my @b = sort {$a <=> $b} @{$nums}; + my %sm; + my $l = 0; + foreach my $i (0 .. $#b) { + my $e = $b[$i]; + if ($i == 0 || $e != $l) { + $sm{$e} = $i; + $l = $e; + } + } + return [map {$sm{$_}} @{$nums}]; +} diff --git a/challenge-244/roger-bell-west/perl/ch-2.pl b/challenge-244/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..114052dfd3 --- /dev/null +++ b/challenge-244/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 1; + +is(grouphero([2, 1, 4]), 141, 'example 1'); + +use Algorithm::Combinatorics qw(combinations); + +sub grouphero($nums0) { + my @nums = sort {$a <=> $b} @{$nums0}; + my $sum = 0; + foreach my $l (1 .. scalar @nums) { + foreach my $c (combinations(\@nums, $l)) { + $sum += $c->[-1] * $c->[-1] * $c->[0]; + } + } + return $sum; +} diff --git a/challenge-244/roger-bell-west/postscript/ch-1.ps b/challenge-244/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..92bef555ad --- /dev/null +++ b/challenge-244/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,267 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } 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 + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} 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 + +/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 + +/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.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 + +/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 + +/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 + +/deepcopy { + 2 dict begin + /a exch def + a type /dicttype eq { + << + a keys { + /k exch def + k + a k get deepcopy + } forall + >> + } { + a type /arraytype eq { + [ + a { + deepcopy + } forall + ] + } { + a type /stringtype eq { + a dup length string cvs + } { + a + } ifelse + } ifelse + } ifelse + end +} bind def + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + + +% end included library code + +/countsmaller { + 0 dict begin + /nums exch def + /b nums deepcopy quicksort def + /sm 0 dict def + /l 0 def + b enumerate.array { + aload pop + /e exch def + /i exch def + i 0 eq e l ne or { + sm e i put + /l e def + } if + } forall + nums { sm exch get } map + end +} bind def + +(countsmaller) test.start +[8 1 2 2 3] countsmaller [4 0 1 1 3] deepeq test +[6 5 4 8] countsmaller [2 1 0 3] deepeq test +[2 2 2] countsmaller [0 0 0] deepeq test +test.end diff --git a/challenge-244/roger-bell-west/postscript/ch-2.ps b/challenge-244/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..5cbe083074 --- /dev/null +++ b/challenge-244/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,188 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/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 + +/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 + +/combinations { + 4 dict begin + /k exch def + /arr exch def + /c [ + 0 1 k 1 sub { } for + arr length + 0 + ] def + [ + { + [ + k 1 sub -1 0 { + c exch get arr exch get + } for + ] + /j 0 def + { + c j get 1 add c j 1 add get ne { + exit + } if + c j j put + /j j 1 add def + } loop + j k ge { + exit + } if + c j c j get 1 add put + } loop + ] + end +} bind def + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + 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 { + { quicksort.cmp } quicksort.with_comparator +} 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.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/grouphero { + 0 dict begin + quicksort reverse /nums exch def + 0 + 1 1 nums length { + nums exch combinations { + /c exch def + /h c c length 1 sub get def + h h mul c 0 get mul add + } forall + } for + end +} bind def + +(grouphero) test.start +[2 1 4] grouphero 141 eq test +test.end diff --git a/challenge-244/roger-bell-west/python/ch-1.py b/challenge-244/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..75ebaf7901 --- /dev/null +++ b/challenge-244/roger-bell-west/python/ch-1.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +def countsmaller(nums): + b = nums.copy() + b.sort() + sm = dict() + l = 0 + for i, e in enumerate(b): + if i == 0 or e != l: + sm[e] = i + l = e + return [sm[n] for n in nums] + +import unittest + +class TestCountsmaller(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(countsmaller([8, 1, 2, 2, 3]), [4, 0, 1, 1, 3], 'example 1') + + def test_ex2(self): + self.assertEqual(countsmaller([6, 5, 4, 8]), [2, 1, 0, 3], 'example 2') + + def test_ex3(self): + self.assertEqual(countsmaller([2, 2, 2]), [0, 0, 0], 'example 3') + +unittest.main() diff --git a/challenge-244/roger-bell-west/python/ch-2.py b/challenge-244/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..7878ee513c --- /dev/null +++ b/challenge-244/roger-bell-west/python/ch-2.py @@ -0,0 +1,21 @@ +#! /usr/bin/python3 + +from itertools import combinations + +def grouphero(nums0): + nums = nums0.copy() + nums.sort() + sum = 0 + for l in range(1, len(nums) + 1): + for c in combinations(nums, l): + sum += c[-1] * c[-1] * c[0] + return sum + +import unittest + +class TestGrouphero(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(grouphero([2, 1, 4]), 141, 'example 1') + +unittest.main() diff --git a/challenge-244/roger-bell-west/raku/ch-1.p6 b/challenge-244/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..53b60c9570 --- /dev/null +++ b/challenge-244/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,23 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(countsmaller([8, 1, 2, 2, 3]), [4, 0, 1, 1, 3], 'example 1'); +is-deeply(countsmaller([6, 5, 4, 8]), [2, 1, 0, 3], 'example 2'); +is-deeply(countsmaller([2, 2, 2]), [0, 0, 0], 'example 3'); + +sub countsmaller(@nums) { + my @b = @nums.sort({$^a <=> $^b}); + my %sm; + my $l = 0; + for 0 .. @b.end -> $i { + my $e = @b[$i]; + if ($i == 0 || $e != $l) { + %sm{$e} = $i; + $l = $e; + } + } + return Array(@nums.map({%sm{$_}})); +} diff --git a/challenge-244/roger-bell-west/raku/ch-2.p6 b/challenge-244/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..c656777604 --- /dev/null +++ b/challenge-244/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 1; + +is(grouphero([2, 1, 4]), 141, 'example 1'); + +sub grouphero(@nums0) { + my @nums = @nums0.sort({$^a <=> $^b}); + my $sum = 0; + for (1 .. @nums.elems) -> $l { + for combinations(@nums, $l) -> @c { + $sum += @c[*-1] * @c[*-1] * @c[0]; + } + } + return $sum; +} diff --git a/challenge-244/roger-bell-west/ruby/ch-1.rb b/challenge-244/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..d8564d2402 --- /dev/null +++ b/challenge-244/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,32 @@ +#! /usr/bin/ruby + +def countsmaller(nums) + b = nums.sort() + sm = Hash.new + l = 0 + b.each_with_index do |e, i| + if i == 0 || e != l then + sm[e] = i + l = e + end + end + return nums.map {|n| sm[n]} +end + +require 'test/unit' + +class TestCountsmaller < Test::Unit::TestCase + + def test_ex1 + assert_equal([4, 0, 1, 1, 3], countsmaller([8, 1, 2, 2, 3])) + end + + def test_ex2 + assert_equal([2, 1, 0, 3], countsmaller([6, 5, 4, 8])) + end + + def test_ex3 + assert_equal([0, 0, 0], countsmaller([2, 2, 2])) + end + +end diff --git a/challenge-244/roger-bell-west/ruby/ch-2.rb b/challenge-244/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..0b6d1328f1 --- /dev/null +++ b/challenge-244/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,22 @@ +#! /usr/bin/ruby + +def grouphero(nums0) + nums = nums0.sort() + sum = 0 + 1.upto(nums.length) do |l| + nums.combination(l) do |c| + sum += c[-1] * c[-1] * c[0]< |
