Jump to content

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{
class KnapsackItem{
has ($.name, $.weight is rw,$.price is rw, $.ppw);
method new (Str $n, $w, $p){
Line 660 ⟶ 659:
};
 
my ($max_w, @in_knapsack) = 15;
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);
push @in_knapsack,take $_;
$max_w -= .weight;
last if $last_one;
}
{say ~$_}
 
#Output:
say "Item Portion Value";
for @in_knapsack {say .Str}
printf "TOTAL: %1.2f %1.2f\n",([+]@in_knapsack».weight),[+]@in_knapsack».price
</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
TOTAL: 15.00 349.38
</pre>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(scl 2)
38

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.