Knapsack problem/Continuous: Difference between revisions
m
→{{header|Perl 6}}: used gather statement
m (→{{header|Perl 6}}: used gather statement) |
|||
Line 643:
=={{header|Perl 6}}==
This Solutions sorts the item by WEIGHT/VALUE
<lang perl6>class KnapsackItem{
has ($.name, $.weight is rw,$.price is rw, $.ppw);
method new (Str $n, $w, $p){
Line 660 ⟶ 659:
};
my
say "Item Portion Value";▼
for gather for
<beef 3.8 36
pork 5.4 43
Line 674:
==> map {KnapsackItem.new($^a, $^b, $^c)}
==> sort {.ppw}
{ #For-Loop inside gather
my $last_one = .cut_maybe($max_w);
$max_w -= .weight;
last if $last_one;
}
{say ~$_}
▲say "Item Portion Value";
</lang>
'''Output:'''
<pre>
%perl6 knapsack_continous.p6
Item Portion Value
salami 3.00 95.00
Line 695 ⟶ 691:
greaves 2.40 45.00
welt 3.50 63.38
</pre>
=={{header|PicoLisp}}==
<lang PicoLisp>(scl 2)
|