| tableFor solve count assert oracle answer expected problems weights values digits digit selectedWeight selectedValue savedWeights savedValues first second |
tableFor := [:modelWeights :modelValues :capacity |
 | modelTable |
 ((modelWeights isKindOf: Array) and: [modelValues isKindOf: Array])
  ifFalse: [Error signal: 'Use two Arrays'].
 modelWeights size = modelValues size
  ifFalse: [Error signal: 'Weights and values must have equal size'].
 (capacity isInteger and: [capacity >= 0])
  ifFalse: [Error signal: 'Capacity must be a nonnegative integer'].
 (modelWeights allSatisfy: [:w | w isInteger and: [w >= 0]])
  ifFalse: [Error signal: 'Weights must be nonnegative integers'].
 (modelValues allSatisfy: [:v | v isInteger and: [v >= 0]])
  ifFalse: [Error signal: 'Values must be nonnegative integers'].
 ((modelWeights size + 1) * (capacity + 1)) > 1000000
  ifTrue: [Error signal: 'Teaching limit: one million table entries'].
 modelTable := (1 to: modelWeights size + 1) collect: [:i |
  Array new: capacity + 1 withAll: 0].
 1 to: modelWeights size do: [:i |
  | previous modelCurrent weight value |
  previous := modelTable at: i. modelCurrent := modelTable at: i + 1.
  weight := modelWeights at: i. value := modelValues at: i.
  0 to: capacity do: [:modelRoom |
   | best |
   best := previous at: modelRoom + 1.
   weight <= modelRoom ifTrue: [
    best := best max: (value + (previous at: modelRoom - weight + 1))].
   modelCurrent at: modelRoom + 1 put: best]].
 modelTable].
solve := [:modelWeights :modelValues :capacity |
 | modelTable modelChosen modelRoom |
 modelTable := tableFor value: modelWeights value: modelValues value: capacity.
 modelChosen := OrderedCollection new. modelRoom := capacity.
 modelWeights size to: 1 by: -1 do: [:i |
  ((modelTable at: i + 1) at: modelRoom + 1) > ((modelTable at: i) at: modelRoom + 1)
   ifTrue: [modelChosen addFirst: i. modelRoom := modelRoom - (modelWeights at: i)]].
 {modelTable last last. modelChosen asArray. modelTable}].
count := 0. problems := 0.
assert := [:condition | condition ifFalse: [Error signal: 'Knapsack assertion failed at ', (count + 1) printString]. count := count + 1].
assert value: ((Compiler evaluate: '| weights values chosen weight value |
weights := #(2 3 4 5). values := #(6 10 12 15).
chosen := #(2 3).
weight := chosen inject: 0 into: [:sum :i | sum + (weights at: i)].
value := chosen inject: 0 into: [:sum :i | sum + (values at: i)].
{weight. value. weight <= 7}') = (#(7 22 true))).
assert value: ((Compiler evaluate: '| weights values order room chosen total |
weights := #(2 3 4 5). values := #(6 10 12 15).
order := (1 to: weights size) asArray sorted: [:a :b |
 | left right |
 left := (values at: a) * (weights at: b).
 right := (values at: b) * (weights at: a).
 left = right ifTrue: [a < b] ifFalse: [left > right]].
room := 7. chosen := OrderedCollection new. total := 0.
order do: [:i | (weights at: i) <= room ifTrue: [
 chosen add: i. room := room - (weights at: i).
 total := total + (values at: i)]].
{chosen asArray. total. room}') = (#(#(2 1) 16 2))).
assert value: ((Compiler evaluate: '| table |
table := (1 to: 5) collect: [:i | Array new: 8 withAll: 0].
(table at: 2) at: 3 put: 6.
{table size. table first size.
 (table first at: 3). ((table at: 2) at: 3).
 table first == (table at: 2)}') = (#(5 8 0 6 false))).
assert value: ((Compiler evaluate: '| tableFor |
tableFor := [:weights :values :capacity |
 | table |
 ((weights isKindOf: Array) and: [values isKindOf: Array])
  ifFalse: [Error signal: ''Use two Arrays''].
 weights size = values size
  ifFalse: [Error signal: ''Weights and values must have equal size''].
 (capacity isInteger and: [capacity >= 0])
  ifFalse: [Error signal: ''Capacity must be a nonnegative integer''].
 (weights allSatisfy: [:w | w isInteger and: [w >= 0]])
  ifFalse: [Error signal: ''Weights must be nonnegative integers''].
 (values allSatisfy: [:v | v isInteger and: [v >= 0]])
  ifFalse: [Error signal: ''Values must be nonnegative integers''].
 ((weights size + 1) * (capacity + 1)) > 1000000
  ifTrue: [Error signal: ''Teaching limit: one million table entries''].
 table := (1 to: weights size + 1) collect: [:i |
  Array new: capacity + 1 withAll: 0].
 1 to: weights size do: [:i |
  | previous current weight value |
  previous := table at: i. current := table at: i + 1.
  weight := weights at: i. value := values at: i.
  0 to: capacity do: [:room |
   | best |
   best := previous at: room + 1.
   weight <= room ifTrue: [
    best := best max: (value + (previous at: room - weight + 1))].
   current at: room + 1 put: best]].
 table].
(tableFor value: #(2 3 4 5) value: #(6 10 12 15) value: 7) last') = (#(0 0 6 10 12 16 18 22))).
assert value: ((Compiler evaluate: '| tableFor solve answer |
tableFor := [:weights :values :capacity |
 | table |
 ((weights isKindOf: Array) and: [values isKindOf: Array])
  ifFalse: [Error signal: ''Use two Arrays''].
 weights size = values size
  ifFalse: [Error signal: ''Weights and values must have equal size''].
 (capacity isInteger and: [capacity >= 0])
  ifFalse: [Error signal: ''Capacity must be a nonnegative integer''].
 (weights allSatisfy: [:w | w isInteger and: [w >= 0]])
  ifFalse: [Error signal: ''Weights must be nonnegative integers''].
 (values allSatisfy: [:v | v isInteger and: [v >= 0]])
  ifFalse: [Error signal: ''Values must be nonnegative integers''].
 ((weights size + 1) * (capacity + 1)) > 1000000
  ifTrue: [Error signal: ''Teaching limit: one million table entries''].
 table := (1 to: weights size + 1) collect: [:i |
  Array new: capacity + 1 withAll: 0].
 1 to: weights size do: [:i |
  | previous current weight value |
  previous := table at: i. current := table at: i + 1.
  weight := weights at: i. value := values at: i.
  0 to: capacity do: [:room |
   | best |
   best := previous at: room + 1.
   weight <= room ifTrue: [
    best := best max: (value + (previous at: room - weight + 1))].
   current at: room + 1 put: best]].
 table].
solve := [:weights :values :capacity |
 | table chosen room |
 table := tableFor value: weights value: values value: capacity.
 chosen := OrderedCollection new. room := capacity.
 weights size to: 1 by: -1 do: [:i |
  ((table at: i + 1) at: room + 1) > ((table at: i) at: room + 1)
   ifTrue: [chosen addFirst: i. room := room - (weights at: i)]].
 {table last last. chosen asArray. table}].
answer := solve value: #(2 3 4 5) value: #(6 10 12 15) value: 7.
{answer first. answer second}') = (#(22 #(2 3)))).
assert value: ((Compiler evaluate: '| tableFor solve answer |
tableFor := [:weights :values :capacity |
 | table |
 ((weights isKindOf: Array) and: [values isKindOf: Array])
  ifFalse: [Error signal: ''Use two Arrays''].
 weights size = values size
  ifFalse: [Error signal: ''Weights and values must have equal size''].
 (capacity isInteger and: [capacity >= 0])
  ifFalse: [Error signal: ''Capacity must be a nonnegative integer''].
 (weights allSatisfy: [:w | w isInteger and: [w >= 0]])
  ifFalse: [Error signal: ''Weights must be nonnegative integers''].
 (values allSatisfy: [:v | v isInteger and: [v >= 0]])
  ifFalse: [Error signal: ''Values must be nonnegative integers''].
 ((weights size + 1) * (capacity + 1)) > 1000000
  ifTrue: [Error signal: ''Teaching limit: one million table entries''].
 table := (1 to: weights size + 1) collect: [:i |
  Array new: capacity + 1 withAll: 0].
 1 to: weights size do: [:i |
  | previous current weight value |
  previous := table at: i. current := table at: i + 1.
  weight := weights at: i. value := values at: i.
  0 to: capacity do: [:room |
   | best |
   best := previous at: room + 1.
   weight <= room ifTrue: [
    best := best max: (value + (previous at: room - weight + 1))].
   current at: room + 1 put: best]].
 table].
solve := [:weights :values :capacity |
 | table chosen room |
 table := tableFor value: weights value: values value: capacity.
 chosen := OrderedCollection new. room := capacity.
 weights size to: 1 by: -1 do: [:i |
  ((table at: i + 1) at: room + 1) > ((table at: i) at: room + 1)
   ifTrue: [chosen addFirst: i. room := room - (weights at: i)]].
 {table last last. chosen asArray. table}].
answer := solve value: #(2 2) value: #(5 5) value: 2.
{answer first. answer second}') = (#(5 #(1)))).
assert value: ((Compiler evaluate: '| tableFor solve free empty |
tableFor := [:weights :values :capacity |
 | table |
 ((weights isKindOf: Array) and: [values isKindOf: Array])
  ifFalse: [Error signal: ''Use two Arrays''].
 weights size = values size
  ifFalse: [Error signal: ''Weights and values must have equal size''].
 (capacity isInteger and: [capacity >= 0])
  ifFalse: [Error signal: ''Capacity must be a nonnegative integer''].
 (weights allSatisfy: [:w | w isInteger and: [w >= 0]])
  ifFalse: [Error signal: ''Weights must be nonnegative integers''].
 (values allSatisfy: [:v | v isInteger and: [v >= 0]])
  ifFalse: [Error signal: ''Values must be nonnegative integers''].
 ((weights size + 1) * (capacity + 1)) > 1000000
  ifTrue: [Error signal: ''Teaching limit: one million table entries''].
 table := (1 to: weights size + 1) collect: [:i |
  Array new: capacity + 1 withAll: 0].
 1 to: weights size do: [:i |
  | previous current weight value |
  previous := table at: i. current := table at: i + 1.
  weight := weights at: i. value := values at: i.
  0 to: capacity do: [:room |
   | best |
   best := previous at: room + 1.
   weight <= room ifTrue: [
    best := best max: (value + (previous at: room - weight + 1))].
   current at: room + 1 put: best]].
 table].
solve := [:weights :values :capacity |
 | table chosen room |
 table := tableFor value: weights value: values value: capacity.
 chosen := OrderedCollection new. room := capacity.
 weights size to: 1 by: -1 do: [:i |
  ((table at: i + 1) at: room + 1) > ((table at: i) at: room + 1)
   ifTrue: [chosen addFirst: i. room := room - (weights at: i)]].
 {table last last. chosen asArray. table}].
free := solve value: #(0 0 2) value: #(4 0 7) value: 0.
empty := solve value: #() value: #() value: 0.
{free first. free second. empty first. empty second}') = (#(4 #(1) 0 #()))).
assert value: ((Compiler evaluate: '| tableFor rejected |
tableFor := [:weights :values :capacity |
 | table |
 ((weights isKindOf: Array) and: [values isKindOf: Array])
  ifFalse: [Error signal: ''Use two Arrays''].
 weights size = values size
  ifFalse: [Error signal: ''Weights and values must have equal size''].
 (capacity isInteger and: [capacity >= 0])
  ifFalse: [Error signal: ''Capacity must be a nonnegative integer''].
 (weights allSatisfy: [:w | w isInteger and: [w >= 0]])
  ifFalse: [Error signal: ''Weights must be nonnegative integers''].
 (values allSatisfy: [:v | v isInteger and: [v >= 0]])
  ifFalse: [Error signal: ''Values must be nonnegative integers''].
 ((weights size + 1) * (capacity + 1)) > 1000000
  ifTrue: [Error signal: ''Teaching limit: one million table entries''].
 table := (1 to: weights size + 1) collect: [:i |
  Array new: capacity + 1 withAll: 0].
 1 to: weights size do: [:i |
  | previous current weight value |
  previous := table at: i. current := table at: i + 1.
  weight := weights at: i. value := values at: i.
  0 to: capacity do: [:room |
   | best |
   best := previous at: room + 1.
   weight <= room ifTrue: [
    best := best max: (value + (previous at: room - weight + 1))].
   current at: room + 1 put: best]].
 table].
rejected := [:weights :values :capacity |
 [tableFor value: weights value: values value: capacity. false]
  on: Error do: [:error | true]].
{rejected value: #(1 2) value: #(5) value: 3.
 rejected value: #(1.5) value: #(5) value: 3.
 rejected value: #(1) value: #(5) value: -1.
 rejected value: #() value: #() value: 1000000}') = (#(true true true true))).
oracle := [:ws :vs :capacity |
 | best bestItems |
 best := -1. bestItems := #().
 0 to: (2 raisedTo: ws size) - 1 do: [:mask |
  | totalWeight totalValue items |
  totalWeight := 0. totalValue := 0. items := OrderedCollection new.
  1 to: ws size do: [:i | (mask bitAt: i) = 1 ifTrue: [
   totalWeight := totalWeight + (ws at: i).
   totalValue := totalValue + (vs at: i). items add: i]].
  (totalWeight <= capacity and: [totalValue > best]) ifTrue: [
   best := totalValue. bestItems := items asArray]].
 {best. bestItems}].
0 to: 3 do: [:n |
 0 to: (9 raisedTo: n) - 1 do: [:encoded |
  weights := Array new: n. values := Array new: n. digits := encoded.
  1 to: n do: [:i |
   digit := digits \\ 9. digits := digits // 9.
   weights at: i put: digit // 3. values at: i put: digit \\ 3].
  0 to: 6 do: [:capacity |
   problems := problems + 1.
   answer := solve value: weights value: values value: capacity.
   expected := oracle value: weights value: values value: capacity.
   assert value: (answer first = expected first).
   assert value: (answer second = expected second).
   selectedWeight := answer second inject: 0 into: [:sum :i | sum + (weights at: i)].
   selectedValue := answer second inject: 0 into: [:sum :i | sum + (values at: i)].
   assert value: (selectedWeight <= capacity).
   assert value: (selectedValue = answer first)]]].
assert value: ([solve value: (#(1 2)) value: (#(3)) value: (2). false] on: Error do: [:error | true]).
assert value: ([solve value: (#(-1)) value: (#(3)) value: (2). false] on: Error do: [:error | true]).
assert value: ([solve value: (#(1.5)) value: (#(3)) value: (2). false] on: Error do: [:error | true]).
assert value: ([solve value: (#(1)) value: (#(-3)) value: (2). false] on: Error do: [:error | true]).
assert value: ([solve value: (#(1)) value: (#(3.5)) value: (2). false] on: Error do: [:error | true]).
assert value: ([solve value: (#(1)) value: (#(3)) value: (-1). false] on: Error do: [:error | true]).
assert value: ([solve value: (#(1)) value: (#(3)) value: (2.0). false] on: Error do: [:error | true]).
assert value: ([solve value: (OrderedCollection new) value: (#()) value: (0). false] on: Error do: [:error | true]).
assert value: ([solve value: (#()) value: (OrderedCollection new) value: (0). false] on: Error do: [:error | true]).
assert value: ([solve value: (#()) value: (#()) value: (1000000). false] on: Error do: [:error | true]).
weights := #(0 2 3) copy. values := #(7 5 9) copy.
savedWeights := weights copy. savedValues := values copy.
first := solve value: weights value: values value: 3.
second := solve value: weights value: values value: 3.
assert value: (weights = savedWeights and: [values = savedValues]).
assert value: (first = second).
(first third at: 2) at: 1 put: 999.
assert value: ((first third first at: 1) = 0).
assert value: ((second third at: 2) first = 7).
answer := solve value: #(1 1) value: {10 raisedTo: 50. 10 raisedTo: 50} value: 2.
assert value: (answer first = (2 * (10 raisedTo: 50))).
assert value: (answer second = #(1 2)).
assert value: (problems = 5740).
count
