Count the coins/0-1: Difference between revisions

m
→‎{{header|Raku}}: DRY, reduce to essential complexity
m (→‎{{header|Raku}}: DRY, reduce to essential complexity)
Line 73:
 
=={{header|Raku}}==
This is pretty much duplicating other tasks, in process if not wording. First part is combinations filtered on a certain property. Second part (extra credit) is permutations of those combinations. Even though I am adding a solution, my vote would be for deletion as it doesn't really add anything to the other tasks; [[Combinations]], [[Permutations]], [[Subset sum problem]] and to a large extent [[4-rings or 4-squares puzzle]].
 
This is pretty much duplicating other tasks, in process if not wording. Even though I am adding a solution, my vote would be for deletion as it doesn't really add anything to the other tasks; [[Combinations]], [[Permutations]], [[Subset sum problem]] and to a large extent [[4-rings or 4-squares puzzle]].
<lang perl6>sub which (*@items, :$sum-to) { ^@items .combinations.grep: { @items[$_].sum == $sum-to } }
 
<lang perl6>for <1 2 3 4 5>, 6
,<1 1 2 3 3 4 5>, 6
,<1 2 3 4 5 5 5 5 15 15 10 10 10 10 25 100>, 40
Line 84:
put "\n\nHow many combinations of [{ @items.join: ', ' }] sum to $sum?";
 
given my ^@items .&whichcombinations.grep: :{ @items[$_].sum-to( == $sum )} {
.&display;
put "\nOrder unimportant:\nCount: { +$_ }\nIndices" ~ ( +$_ > 10 ?? ' (10 random examples):' !! ':' );
putdisplay .pick(10)race.sort».joinmap(', '){ Slip(.join:permutations) "\n"} ), '';
 
my $p = .race.map: { Slip(.permutations) }
put "\nOrder important:\nCount: { +$p }\nIndices" ~ ( +$p > 10 ?? ' (10 random examples):' !! ':' );
put $p.pick(10).sort».join(', ').join: "\n";
}
}
 
sub display ($list, $un = 'un') {
put "\nOrder unimportant{$un}important:\nCount: { +$_list }\nIndices" ~ ( +$_list > 10 ?? ' (10 random examples):' !! ':' );
put $plist.pick(10).sort».join(', ').join: "\n";
}</lang>
{{out}}
10,327

edits