Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,36 @@
|
|||
(lib 'struct)
|
||||
(lib 'sql) ;; for table
|
||||
|
||||
(define T (make-table (struct meal (name poids price))))
|
||||
|
||||
(define meals
|
||||
'((🐂-beef 3.8 36)
|
||||
(🍖-pork 5.4 43)
|
||||
(🍗-ham 3.6 90)
|
||||
(🐪-greaves 2.4 45)
|
||||
(flitch 4.0 30)
|
||||
(brawn 2.5 56)
|
||||
(welt 3.7 67)
|
||||
(🐃--salami 3.0 95)
|
||||
(🐖-sausage 5.9 98)))
|
||||
|
||||
(list->table meals T)
|
||||
;; sort table according to best price/poids ratio
|
||||
(define (price/poids a b )
|
||||
(- (// (* (meal-price b) (meal-poids a)) (meal-price a) (meal-poids b)) 1))
|
||||
(table-sort T price/poids)
|
||||
|
||||
(define-syntax-rule (name i) (table-xref T i 0))
|
||||
(define-syntax-rule (poids i) (table-xref T i 1))
|
||||
|
||||
;; shop : add items in basket, in order, until W exhausted
|
||||
(define (shop W )
|
||||
(for/list ((i (table-count T)))
|
||||
#:break (<= W 0)
|
||||
(begin0
|
||||
(cons (name i) (if (<= (poids i) W) 'all W))
|
||||
(set! W (- W (poids i))))))
|
||||
|
||||
;; output
|
||||
(shop 15)
|
||||
→ ((🐃--salami . all) (🍗-ham . all) (brawn . all) (🐪-greaves . all) (welt . 3.5))
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
[
|
||||
[ "beef", 3.8, 36 ], [ "pork", 5.4, 43 ], [ "ham", 3.6, 90 ],
|
||||
[ "greaves", 2.4, 45 ], [ "flitch", 4.0, 30 ], [ "brawn", 2.5, 56 ],
|
||||
[ "welt", 3.7, 67 ], [ "salami", 3.0, 95 ], [ "sausage", 5.9, 98 ]
|
||||
] const: Items
|
||||
|
||||
: rob
|
||||
| item value |
|
||||
0.0 ->value
|
||||
15.0 #[ dup second swap third / ] Items sortBy forEach: item [
|
||||
dup 0.0 == ifTrue: [ return ]
|
||||
dup item second >= ifTrue: [
|
||||
"Taking" . item first . " :" . item second dup .cr -
|
||||
item third value + ->value continue
|
||||
]
|
||||
"And part of" . item first . " :" . dup .cr
|
||||
item third * item second / value + "Total value :" . .cr break
|
||||
] ;
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
var items =
|
||||
[
|
||||
[:beef, 3.8, 36],
|
||||
[:pork, 5.4, 43],
|
||||
[:ham, 3.6, 90],
|
||||
[:greaves, 2.4, 45],
|
||||
[:flitch, 4.0, 30],
|
||||
[:brawn, 2.5, 56],
|
||||
[:welt, 3.7, 67],
|
||||
[:salami, 3.0, 95],
|
||||
[:sausage, 5.9, 98],
|
||||
].sort {|a,b| b[2]/b[1] <=> a[2]/a[1] }
|
||||
|
||||
var (limit, value) = (15, 0);
|
||||
print "Item Fraction Weight Value\n";
|
||||
|
||||
items.each { |item|
|
||||
var ratio = (item[1] > limit ? limit/item[1] : 1);
|
||||
value += item[2]*ratio;
|
||||
limit -= item[1];
|
||||
if (ratio == 1) {
|
||||
printf("%-8s %4s %7.2f %6.2f\n", item[0], 'all', item[1], item[2]);
|
||||
}
|
||||
else {
|
||||
printf("%-8s %-4.2f %7.2f %6.2f\n", item[0], ratio, item[1]*ratio, item[2]*ratio);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
say "#{'-'*28}\ntotal value: #{'%.14g' % value }"
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# continuous_knapsack(W) expects the input to be
|
||||
# an array of objects {"name": _, "weight": _, "value": _}
|
||||
# where "value" is the value of the given weight of the object.
|
||||
|
||||
def continuous_knapsack(W):
|
||||
map( .price = (if .weight > 0 then (.value/.weight) else 0 end) )
|
||||
| sort_by( .price )
|
||||
| reverse
|
||||
| reduce .[] as $item
|
||||
# state: [array, capacity]
|
||||
([[], W];
|
||||
.[1] as $c
|
||||
| if $c <= 0 then .
|
||||
else ( [$item.weight, $c] | min) as $min
|
||||
| [.[0] + [ $item | (.weight = $min) | .value = (.price * $min)],
|
||||
($c - $min) ]
|
||||
end)
|
||||
| .[1] as $remainder
|
||||
| .[0]
|
||||
| (.[] | {name, weight}),
|
||||
"Total value: \( map(.value) | add)",
|
||||
"Total weight: \(W - $remainder)" ;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
def items: [
|
||||
{ "name": "beef", "weight": 3.8, "value": 36},
|
||||
{ "name": "pork", "weight": 5.4, "value": 43},
|
||||
{ "name": "ham", "weight": 3.6, "value": 90},
|
||||
{ "name": "greaves", "weight": 2.4, "value": 45},
|
||||
{ "name": "flitch", "weight": 4.0, "value": 30},
|
||||
{ "name": "brawn", "weight": 2.5, "value": 56},
|
||||
{ "name": "welt", "weight": 3.7, "value": 67},
|
||||
{ "name": "salami", "weight": 3.0, "value": 95},
|
||||
{ "name": "sausage", "weight": 5.9, "value": 98} ];
|
||||
|
||||
items | continuous_knapsack(15)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
$ jq -r -c -n -f knapsack_continuous.jq
|
||||
{"name":"salami","weight":3}
|
||||
{"name":"ham","weight":3.6}
|
||||
{"name":"brawn","weight":2.5}
|
||||
{"name":"greaves","weight":2.4}
|
||||
{"name":"welt","weight":3.5000000000000004}
|
||||
Total value: 349.3783783783784
|
||||
Total weight: 15
|
||||
Loading…
Add table
Add a link
Reference in a new issue