s LearnSqueak.com

SMALLTALK LAB

Count coin-change combinations in Squeak

Build an array of reachable totals and count unordered combinations with unlimited coins.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Workspace in native Squeak and use Print it on one complete checkpoint at a time.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.

01 - Seed one way to make zero

Array index one represents amount zero. Paying zero with no coins counts as one empty combination; positive totals start at zero ways.

Independent Workspace expression - select all, then Print it
| counts |
counts := Array new: 5 withAll: 0.
counts at: 1 put: 1.
counts

Expected: #(1 0 0 0 0)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Seed one way to make zero
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: What amount does array index 4 represent?

Download this checkpoint

02 - Introduce a coin of value one

Walking up from amount one allows each amount to inherit the preceding count. Every total now has one all-ones solution.

Independent Workspace expression - select all, then Print it
| ways |
ways := [:coins :target |
 | counts |
 target < 0 ifTrue: [Error signal: 'Negative target'].
 ((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
  or: [coins asSet size ~= coins size])
   ifTrue: [Error signal: 'Use distinct positive integers'].
 counts := Array new: target + 1 withAll: 0.
 counts at: 1 put: 1.
 coins do: [:coin |
  coin to: target do: [:sum |
   counts at: sum + 1 put:
    ((counts at: sum + 1) + (counts at: sum - coin + 1))]].
 counts].
ways value: #(1) value: 5

Expected: #(1 1 1 1 1 1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Introduce a coin of value one
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Amount 5 is the last entry, at index 6.

Download this checkpoint

03 - Add the coin of value two

For each sum, add ways to make sum minus two. That earlier entry has already processed this denomination, so two can be used more than once.

Independent Workspace expression - select all, then Print it
| ways |
ways := [:coins :target |
 | counts |
 target < 0 ifTrue: [Error signal: 'Negative target'].
 ((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
  or: [coins asSet size ~= coins size])
   ifTrue: [Error signal: 'Use distinct positive integers'].
 counts := Array new: target + 1 withAll: 0.
 counts at: 1 put: 1.
 coins do: [:coin |
  coin to: target do: [:sum |
   counts at: sum + 1 put:
    ((counts at: sum + 1) + (counts at: sum - coin + 1))]].
 counts].
ways value: #(1 2) value: 5

Expected: #(1 1 2 2 3 3)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Add the coin of value two
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: At amount 4 there are three ways: use zero, one, or two coins of value 2.

Download this checkpoint

04 - Count a shortcut without counting orders

Adding denomination five introduces one new way to pay five. Putting denomination on the outside means 2+1 and 1+2 do not become separate payments.

Independent Workspace expression - select all, then Print it
| ways |
ways := [:coins :target |
 | counts |
 target < 0 ifTrue: [Error signal: 'Negative target'].
 ((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
  or: [coins asSet size ~= coins size])
   ifTrue: [Error signal: 'Use distinct positive integers'].
 counts := Array new: target + 1 withAll: 0.
 counts at: 1 put: 1.
 coins do: [:coin |
  coin to: target do: [:sum |
   counts at: sum + 1 put:
    ((counts at: sum + 1) + (counts at: sum - coin + 1))]].
 counts].
ways value: #(1 2 5) value: 5

Expected: #(1 1 2 2 3 4)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Count a shortcut without counting orders
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Write the four combinations that reach amount 5.

Download this checkpoint

05 - Show a target that cannot be paid

An even denomination never creates an odd total from zero. Amount four has two combinations, but amount five has none.

Independent Workspace expression - select all, then Print it
| ways |
ways := [:coins :target |
 | counts |
 target < 0 ifTrue: [Error signal: 'Negative target'].
 ((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
  or: [coins asSet size ~= coins size])
   ifTrue: [Error signal: 'Use distinct positive integers'].
 counts := Array new: target + 1 withAll: 0.
 counts at: 1 put: 1.
 coins do: [:coin |
  coin to: target do: [:sum |
   counts at: sum + 1 put:
    ((counts at: sum + 1) + (counts at: sum - coin + 1))]].
 counts].
ways value: #(2 4) value: 5

Expected: #(1 0 1 0 2 0)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Show a target that cannot be paid
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Replace target 5 with 6; expect two ways at the final entry.

Download this checkpoint

06 - Verify the denomination order does not matter

The intermediate rows depend on the order we introduce denominations, but the final combinations are the same as long as each denomination appears exactly once.

Independent Workspace expression - select all, then Print it
| ways |
ways := [:coins :target |
 | counts |
 target < 0 ifTrue: [Error signal: 'Negative target'].
 ((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
  or: [coins asSet size ~= coins size])
   ifTrue: [Error signal: 'Use distinct positive integers'].
 counts := Array new: target + 1 withAll: 0.
 counts at: 1 put: 1.
 coins do: [:coin |
  coin to: target do: [:sum |
   counts at: sum + 1 put:
    ((counts at: sum + 1) + (counts at: sum - coin + 1))]].
 counts].
(ways value: #(5 2 1) value: 5) = (ways value: #(1 2 5) value: 5)

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Verify the denomination order does not matter
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try the order #(2 1 5) as well.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Count coin-change combinations in Squeak native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

Use a nonnegative integer target and distinct positive integer coin values. Duplicate denominations would count equivalent combinations more than once.

This counts combinations, not ordered payment sequences and not minimum coins. The outer loop introduces one denomination at a time.

The array uses O(target) slots and the program performs O(denominations × target) additions. Large exact integers can make individual additions more expensive.

Three practice challenges

List all four payments

How can you make five with 1, 2 and 5?

Show solution

1+1+1+1+1; 2+1+1+1; 2+2+1; 5.

Notice an impossible total

Why is five unreachable with only 2 and 4?

Show solution

Every allowed coin is even, so every sum is even.

Switch the question

Would an outer loop over amounts still count these combinations?

Show solution

Not in general. It can count sequences that end with different coins separately, so it can distinguish 1+2 from 2+1.

Reference

Carnegie Mellon notes: contrast the ordered-sequence variant §5.2

Continue learning

Knapsack | First Occurrences Of

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.