File tree 4 files changed +13
-14
lines changed
exercises/practice/knapsack
4 files changed +13
-14
lines changed Original file line number Diff line number Diff line change 1
1
The items are represented by [ records] ( https://lfe.gitbooks.io/reference-guide/content/16.html ) , defined in ` item.lfe ` .
2
- Use ` item-weight ` to get the weight and ` item-value ` to get the value.
2
+ Use ` item-weight ` to get the weight and ` item-value ` to get the value.
3
3
4
4
```
5
5
;; Create an item with weight=5, value=50
@@ -10,4 +10,4 @@ Use `item-weight` to get the weight and `item-value` to get the value.
10
10
11
11
;; Get the value. Returns 50.
12
12
(item-value item)
13
- ```
13
+ ```
Original file line number Diff line number Diff line change 10
10
(weight-value (item-weight item))
11
11
(value-value (item-value item))
12
12
(left-over-capacity (- capacity weight-value)))
13
- (if
13
+ (if
14
14
(< left-over-capacity 0 )
15
15
value-without
16
- (let
16
+ (let
17
17
((value-with
18
18
(+
19
19
(lists:nth (+ left-over-capacity 1 ) last-values)
22
22
23
23
(defun next-values
24
24
([item last-values -1 acc] acc)
25
- ([item last-values capacity acc]
26
- (next-values
27
- item
28
- last-values
29
- (- capacity 1 )
25
+ ([item last-values capacity acc]
26
+ (next-values
27
+ item
28
+ last-values
29
+ (- capacity 1 )
30
30
(cons (find-max item last-values capacity) acc))))
31
31
32
32
(defun do-maximum-values
33
33
(['() last-values capacity] (lists:last last-values))
34
34
([(cons item remaining) last-values capacity]
35
35
(do -maximum-values
36
- remaining
36
+ remaining
37
37
(next-values item last-values capacity '())
38
38
capacity)))
39
39
40
40
(defun maximum-value
41
41
(('() _capacity) 0 )
42
42
((items capacity)
43
- (do -maximum-values
43
+ (do -maximum-values
44
44
items
45
- (lists:duplicate
45
+ (lists:duplicate
46
46
(+ capacity 1 )
47
47
0 )
48
48
capacity)))
Original file line number Diff line number Diff line change 1
1
; This include file defines the item record used in the exercise.
2
2
; There shouldn't be any need to modify this file.
3
3
4
- (defrecord item weight value)
4
+ (defrecord item weight value)
Original file line number Diff line number Diff line change 81
81
(make-item weight 118 value 229 )
82
82
(make-item weight 120 value 240 ))
83
83
750 )))
84
-
You can’t perform that action at this time.
0 commit comments