Thinking Slowly

Ryan: Introduction to Computational Reasoning Fall 2025

In this exercise we learn to give instructions to a simple minded robot to sort a row of playing cards into ascending order (A2345678910JQK).

Ryan: Introduction to Computational Reasoning Fall 2025

Students work in groups of two or three. One, playing the robot, sits with several playing cards, face up, spread out horizontally on the desk. There is an empty space above each card into which it can be moved and cards can be moved (N)orth, (S)outh, (E)ast or (W)est in response to commands given by the other team members who sit with their backs to the robot (that is, they cannot see the cards).

Ryan: Introduction to Computational Reasoning Fall 2025

The goal is to give the robot instructions so that it sorts the cards, lowest to highest, left to right.  The robot cannot see all the cards at once and cannot report the value of a card. It has a limited number of actions:

Move hand West, East, North, South
Raise Up or drop Down the hand to either hover over or rest on card
If the hand is down, touching a card, a move in any direction slides the card in that direction. Movement is blocked, however, if there is already a card in the destination location or if the move would take us outside the bounds of the the cards on  I slide the card up to the "row" above.
Compare the value of the card beneath their hand (whether hovering or touching)  to the card to its right. The robot sets a "FLAG" based on the result: 1 if left > right; 0 if equal; -1 if less than; -2 if there is no card to the right.
Return to the Home position which is hovering above the left-most card

The coder(s) job is to give the robot a sequence of commands that accomplish a task. For example, if we want the robot to swap the position of the first two cards we might instruct D N E U S D W U E N D S

Ryan: Introduction to Computational Reasoning Fall 2025

Ryan: Introduction to Computational Reasoning Fall 2025

Ryan: Introduction to Computational Reasoning Fall 2025

TASK 1: Assume the robot's hand is hovering over a card; give robot commands that will cause it to swap the third card with the one to its right.

TASK1A: Write down a sequence of commands that swap the first card and the last card commands down as a sequence of letters (e.g., D N E U...) and pass the list to the robot to see if it can follow written commands.

TASK 2: give commands to the robot to sort the cards in descending order, left to right.

Ryan: Introduction to Computational Reasoning Fall 2025

Ryan: Introduction to Computational Reasoning Fall 2025

Ryan: Introduction to Computational Reasoning Fall 2025

PART 2

We will expand our robot's capabilities slightly, giving it the capacity to follow a LIST of instructions, a program. Instead of pressing buttons or keys, we write out a list of commands, one to a line with each line numbered:

1 D
2 N
3 E
4 U

The robot keeps track of what command to execute next by a memory it calls LINE.  This also allows us to "jump" to any line with a

GOTO linenum

command.  AND, the robot can conditionally jump to another line based on the value of FLAG after a compare operation.  These commands have the form "if FLAG is 1 (0, -1, -2) set LINE to linenum."

IF1 linenum = GOTO linenum if FLAG is 1
IF0 linenum. = GOTO linenum if FLAG is 0
IFN1 linenum = GOTO linenum if FLAG is -1
IFN2 linenum = GOTO linenum if FLAG is -2

Ryan: Introduction to Computational Reasoning Fall 2025

Ryan: Introduction to Computational Reasoning Fall 2025

Here is the updated version of our card sorting robot:

Ryan: Introduction to Computational Reasoning Fall 2025

Ryan: Introduction to Computational Reasoning Fall 2025

The GOTO command allows us to create "loops" in our code. Consider this set of commands:

1 E
2 E
3 E
4 HOME
5 GOTO 1

This doesn't accomplish anything but it keeps the robot busy forever. Do you see why we say that?  Here's another variation:

This doesn't accomplish anything but it keeps the robot busy forever. Do you see why we say that?  

Ryan: Introduction to Computational Reasoning Fall 2025

Let's try something a little more complex.  Again, we'll move left to right, but at each card we'll do a compare and then slide a card north if it is greater than the one next to it.

This doesn't accomplish anything but it keeps the robot busy forever. Do you see why we say that?  Here's another variation:

Ryan: Introduction to Computational Reasoning Fall 2025

TASK: Practice writing code sequences that do things like 1) move all of the cards to the top row; 2) move any card that is larger than its right-neighbor to the top row.

Ryan: Introduction to Computational Reasoning Fall 2025

Ryan: Introduction to Computational Reasoning Fall 2025

EXTRA

Ryan: Introduction to Computational Reasoning Fall 2025

This technique is called "bubble sorting" because the larger cards "bubble" over to the right.  But we notice two problems: one is we never stop the process, even after all the cards are in order. The other is that we waste a lot of effort making comparisons with cards that have already been sorted at the right end of the row. In practice, since each pass puts the largest card at the end, we can stop one card sooner on each pass.

Before continuing, persuade yourself that you can see this.

Ryan: Introduction to Computational Reasoning Fall 2025

Our robot has a very limited memory: it keeps track of the line it is on and it keeps track of the value of FLAG which it sets when it makes a comparison. Let's add a scratch pad.

PART 3

We have two scratch pads that we call memA and memB.  The robot can set them to any value (ASET and BSET), add or subtract 1 from their value (APLUS, BPLUS, AMIN, BMIN), and can compare them (CMEM) and this operation sets FLAG to 1 if A > B, 0 if A = B, or -1 if A < B. The scratch pad contents persist unless we change them.

Memory Commands

ASET num / BSET num — Set memA or memB to num

APLUS / BPLUS — Add 1 to memA or memB

AMIN / BMIN — Subtract 1 from memA or memB

CMEM — Compare memA to memB and set FLAG to:

1 if A > B

0 if A = B

-1 if A < B

Write out commands for determining how many cards are on the table and storing the result in A.

Ryan: Introduction to Computational Reasoning Fall 2025

Ryan: Introduction to Computational Reasoning Fall 2025

Challenge

What would it take to be able to instruct the robot so it could sort a set of cards of any length?  What else would the robot need to track?

Write instructions that would work for 3 or 4 or 6 cards.