Simple programming training puzzle

In summary, This conversation is about an old programming language based on an imaginary machine that has two peripherals - paper tape reader and paper tape punch. The language only consists of 13 instructions and 4 basic types of instruction. The conversation also includes three programming tasks to be solved using this language. The first task is to duplicate the paper tape, the second is to read the paper tape until three asterisks in a row are read, and the third is to stop all I/O once three asterisks in a row are punched during the copying process. The conversation also includes different attempts and solutions to these tasks.
  • #1
rcgldr
Homework Helper
8,888
649
This is a repost of an old thread, maybe some new members can give it a shot. This puzzle was included at the start of a casual IBM Fortran programming class for high school students back in 1969. (Yes, I'm that old). I don't know the origin of this puzzle.

An extremely simple and old (1960's) programming language based on an imaginary machine. The machine has two peripherals, at paper tape reader, and a paper tape punch. The character set only includes two characters, Asterisk and Hypen. The CPU has 20 memory locations, which hold instructions, but only the first 10 locations, 0 through 9 are addressable via the branch instruction.

There are only 13 instructions, and 4 basic types of instruction:

T - Read a character from the tape reader. If the character read is a hyphen, execute next instruction. If the character read is an asterisk, skip the next instruction, and execute the instruction following the next instruction. If no character is read because the paper tape is past the end, then the machine will stop.

H - Punch a hypen, then execute next instruction.

A - Punch an asterisk, then execute next instruction.

0 - branch to location 0 and continue execution there.
1 - branch to location 1 and continue execution there.
2 - branch to location 2 and continue execution there.
...
9 - branch to location 9 and continue execution there.

Instruction execution can continue past memory location 9, but these memory locations can't be the target of a branch instruction.

Programming tasks:

1. Write a program to duplicate the paper tape in the reader using the paper tape punch.

2. Write a program to read the paper tape until 3 asterisks in a row are read, then start duplicating the paper tape as in program 1.

3. Write a program to read the paper tape until 3 asterisks in a row are read, then start duplicating the paper tape, but stop all I/O once 3 asterisks in a row are punched during the copy process.

Note that since there are only 13 instructions, trial and error will be good enough to write any of these programs.

To avoid spoilers, please white out your repsonse, or merely indicate that you've solved how to create programs 1, 2, and/or 3.

Sample program:

Code:
0 1 2 3 4 5 6 7 8 9 X X X X X X X X X X
T H A 0

This program will punch a hyphen and asterisk for every hyphen read, and punch an asterisk for every asterisk read. It's a broken copy program that you'll fix with program assignment 1.
 
Technology news on Phys.org
  • #2
Ok Jeff I'll kick it off with the first one.



0 1 2 3 4 5 6 7 8 9 X X X X X X X X X X
T 4 A 0 H 0
 
  • #3
The first two are easy enough:
0123456789----------
T4A0H0

0123456789----------
7T5A1H1T7T7T71

But I can't get the third one to work; the lack of addressable locations makes it difficult.
 
  • #4
CRGreathouse said:
But I can't get the third one to work; the lack of addressable locations makes it difficult.
With only 4 basic instructions, there's always trial and error. Try doing the 2nd problem a bit differently.
 
Last edited:
  • #5
So there are 4^20 basic types of programs (about a trillion), and 13^20 total programs.
 
  • #6
CRGreathouse said:
So there are 4^20 basic types of programs (about a trillion), and 13^20 total programs.
True, but take a look at the very first instruction, it can't be an H or an A, so that only leaves two possibilities, a branch or a T. The next instruction after any T will normally be a branch. The 2nd instruction after a T will be an A or a branch. The next instruction after an A or an H will be either a branch or a T. Taking this into account and the number of possibilities is greatly reduced.
 
  • #7
Neat puzzle, thanks for posting it. Here's my attempt.

1.
0123456789xxxxxxxxxx
T4A0H0

2.
0123456789xxxxxxxxxx
7T5A1H1T7T7T71

3.
0123456789xxxxxxxxxx
T0T0T08HT7AT7AT7A
 
  • #8
Bill_B said:
Neat puzzle, thanks for posting it. Here's my attempt.
Almost, in the case of problem 3, running past the end of the last instruction isn't allowed. You need to figure out another way to stop all I/O. You're close though. Note that problem 2 could be implemented similar to problem 3 (same starting sequence).

Note that this was an exercise for students just learning how to program, yet it's challenging even for experienced programmers, being that it's more puzzle oriented. Then again, one issue that seems to be forgotten is that there are only 4 instructions, so if you get stuck, there aren't a lot of reasonable variations to try.
 
  • #9
OK, I assumed (incorrectly) that reaching the end of the instructions would terminate the program.

Since there doesn't appear to be a way to actually terminate execution while the input tape still has data, this solution at least terminates the I/O (though it makes me cringe a little :smile: ) -


3.
0123456789xxxxxxxxxx
T0T0T097HT8AT8AT8A7
 
  • #10
Bill_B said:
Since there doesn't appear to be a way to actually terminate execution while the input tape still has data, this solution at least terminates the I/O (though it makes me cringe a little)
The problem didn't state anything about terminating execution, just I/O, so you've solved the puzzle. By the way, what is your computer doing while you read this post?
 
  • #11
Well, the CPU isn't pegged, so it's not stuck in an infinite loop :smile: but I get what you mean - it's not shut down, and is just sitting there idling.

I understand why that aspect of the solution has to be the way it is, but I'll just continue to pretend that there's an undocumented idle delay implemented somewhere :smile:
 

FAQ: Simple programming training puzzle

What is a simple programming training puzzle?

A simple programming training puzzle is a small, self-contained problem that requires basic programming skills to solve. It is often used as a practice exercise for beginners to improve their coding abilities.

Why is it important to practice simple programming training puzzles?

Practicing simple programming training puzzles helps to improve problem-solving skills, familiarize oneself with programming concepts and techniques, and build confidence in coding abilities. It also helps to prepare for more complex programming challenges in the future.

What are some common features of simple programming training puzzles?

Simple programming training puzzles usually involve a specific goal or task to be achieved, a set of given inputs or parameters, and a desired output or result. They may also include constraints or rules that must be followed in order to solve the puzzle.

Where can I find simple programming training puzzles to practice?

There are many websites and resources available online that offer a variety of simple programming training puzzles for practice. These include coding challenge websites, coding bootcamps, and online courses. Some programming languages also have their own dedicated websites or forums for practicing puzzles specific to that language.

How can I effectively solve a simple programming training puzzle?

To effectively solve a simple programming training puzzle, it is important to read and understand the problem carefully, plan out a solution before coding, break the problem down into smaller parts if necessary, and test your code as you go. It is also helpful to review and analyze your code after solving the puzzle to identify areas for improvement.

Similar threads

Replies
1
Views
3K
Replies
7
Views
4K
Replies
15
Views
3K
Replies
15
Views
3K
Replies
37
Views
3K
Replies
13
Views
5K
Replies
23
Views
2K
Replies
1
Views
1K
Back
Top