aboutsummaryrefslogtreecommitdiff
path: root/challenge-211
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-211')
-rw-r--r--challenge-211/mark-anderson/raku/ch-2.raku16
1 files changed, 9 insertions, 7 deletions
diff --git a/challenge-211/mark-anderson/raku/ch-2.raku b/challenge-211/mark-anderson/raku/ch-2.raku
index 255ada41f4..8af4568b57 100644
--- a/challenge-211/mark-anderson/raku/ch-2.raku
+++ b/challenge-211/mark-anderson/raku/ch-2.raku
@@ -1,6 +1,11 @@
#!/usr/bin/env raku
use Test;
+# This is a do-over after reading other's solutions.
+# Jorg Sommrey and W. Luis Mochan explained that if one subset's average
+# equals the average of the whole list then the other subset will have
+# the same average.
+
ok split-same-avg(1,2,3,4,5,6,7,8); # [1 8] [2 3 4 5 6 7]
nok split-same-avg(1,3);
ok split-same-avg(3,3,5,5,5,2,2,1); # [2 3 3 5] [1 2 5 5]
@@ -8,14 +13,11 @@ nok split-same-avg(5,5,5,2,2,1);
sub split-same-avg(*@nums)
{
- for (^@nums).combinations(1..@nums.elems div 2) -> @a is copy
- {
- my @b = (^@nums (-) @a).keys;
+ my $avg = @nums.sum / @nums.elems;
- @a = @nums[@a];
- @b = @nums[@b];
-
- return True if @a.sum / @a.elems == @b.sum / @b.elems
+ for (^@nums).combinations(1..@nums.elems div 2) -> @a
+ {
+ return True if @nums[@a].sum / @nums[@a].elems == $avg
}
return False