From b69c6dab84dbceb45cdb28c6f09370ec6bbf1498 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Fri, 7 Apr 2023 04:40:45 +0000 Subject: ch-2.raku do-over --- challenge-211/mark-anderson/raku/ch-2.raku | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'challenge-211') 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 -- cgit