SMALLTALK LAB
De Bruijn Sequences in Squeak: Every Bit Pattern in One Ring
Build a cyclic binary sequence in which every short pattern appears once. Enumerate cyclic windows, then construct a ring with a stack.
Before you begin
Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.
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 show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.
01 - See why overlap saves space
Two length-three words normally take six characters. By overlapping their shared 01, four characters show both. A ring takes this trick one step further.
| text |
text := '0010'.
{text copyFrom: 1 to: 3. text copyFrom: 2 to: 4}Expected: {'001'. '010'}

Try it: Name the two shared characters before you run it.
02 - Read across the seam
One window starts at each position, including the last. Convert the index to zero-based, wrap with remainder, then add one again for Smalltalk indexing.
| scan |
scan := [:ring :width | (1 to: ring size) collect: [:start |
String streamContents: [:s |
0 to: width - 1 do: [:offset |
s nextPut: (ring at: (((start - 1 + offset) \\ ring size) + 1))]]]].
(scan value: '0011' value: 2) asArrayExpected: {'00'. '01'. '11'. '10'}

Try it: The final window uses the last character and then the first.
03 - Encode a shifted two-bit node
For width three, graph nodes carry two bits. Node 2 is 10. Appending 0 or 1 traverses edge 100 or 101 and retains the rightmost two bits as the next node.
| node |
node := 2.
{((node bitShift: 1) + 0) bitAnd: 3.
((node bitShift: 1) + 1) bitAnd: 3}Expected: #(0 1)

Try it: Try node 1 (01). It should lead to 2 and 3.
04 - Build a cyclic ring
Each node tracks which of its two outgoing edges to use next. Push the destination when an unused edge exists; otherwise pop it into the reverse trail. Reading the low bits of the finished trail gives the ring, up to rotation. Use width two or greater with this encoding.
| make |
make := [:width | | mask next stack back digits |
mask := (1 bitShift: width - 1) - 1.
next := Array new: mask + 1 withAll: 0.
stack := OrderedCollection with: 0.
back := OrderedCollection new.
[stack notEmpty] whileTrue: [ | node bit newNode |
node := stack last.
bit := next at: node + 1.
bit < 2 ifTrue: [
next at: node + 1 put: bit + 1.
newNode := ((node bitShift: 1) + bit) bitAnd: mask.
stack addLast: newNode]
ifFalse: [back addLast: stack removeLast]].
digits := back reversed allButFirst.
String streamContents: [:s | digits do: [:node |
s nextPut: ((node bitAnd: 1) = 0 ifTrue: [$0] ifFalse: [$1])]]].
make value: 3Expected: '01011100'

Try it: Start at a different compatible node and you may get a rotation or a different valid ring.
05 - Prove this output covers every window
There are eight starting positions and eight distinct windows, so all binary three-bit words occur exactly once. This checks the property directly rather than relying on the appearance of a particular string.
| scan |
scan := [:ring :width | (1 to: ring size) collect: [:start |
String streamContents: [:s |
0 to: width - 1 do: [:offset |
s nextPut: (ring at: (((start - 1 + offset) \\ ring size) + 1))]]]].
(scan value: '01011100' value: 3) asSet sizeExpected: 8

Try it: Change one bit and repeat the check. For example, change the final 0 to 1 and look for a repeated window.
06 - Try width four
There are two outgoing edges per three-bit node and eight nodes. Using every edge once produces 16 bits. Both checks return 16: the generated ring length and the count of unique windows.
| make scan result |
make := [:width | | mask next stack back digits |
mask := (1 bitShift: width - 1) - 1.
next := Array new: mask + 1 withAll: 0.
stack := OrderedCollection with: 0.
back := OrderedCollection new.
[stack notEmpty] whileTrue: [ | node bit newNode |
node := stack last.
bit := next at: node + 1.
bit < 2 ifTrue: [
next at: node + 1 put: bit + 1.
newNode := ((node bitShift: 1) + bit) bitAnd: mask.
stack addLast: newNode]
ifFalse: [back addLast: stack removeLast]].
digits := back reversed allButFirst.
String streamContents: [:s | digits do: [:node |
s nextPut: ((node bitAnd: 1) = 0 ifTrue: [$0] ifFalse: [$1])]]].
scan := [:ring :width | (1 to: ring size) collect: [:start |
String streamContents: [:s |
0 to: width - 1 do: [:offset |
s nextPut: (ring at: (((start - 1 + offset) \\ ring size) + 1))]]]].
result := make value: 4.
{result size. (scan value: result value: 4) asSet size}Expected: #(16 16)

Try it: Change both width arguments together to 2. Expect four positions and four distinct windows.
Replay the checkpoints

Download the native cyclic-window demo. Do it in Workspace. Close the window, change shown := 1. to a number from 2 to 8, and run again.
Reason about correctness and limits
An Euler trail consumes edges once; this application makes each edge a different window. A Gray code solves a different problem: its successive whole words differ by one bit.
The output size doubles per width. Keep the example small. The starter code encodes widths at least two; width one is simply 01.
Three practice challenges
Find the seam
Which width-three word starts at the last bit of 01011100?
Show solution
001: take the final 0, then the leading 0 and 1.
Count nodes
How many nodes and edges are used for width five?
Show solution
16 nodes, 32 edges.
Verify a ring
How do you know a 16-bit candidate is valid for width four?
Show solution
Extract all 16 cyclic windows and check that the set has size 16.
Further reading
N. G. de Bruijn’s 1946 paper, A Combinatorial Problem. The Workspace examples above are original.
Continue learning
Euler Trail | Gray Code | Integer Bitsets
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.