------------------------------------------------------------------------------
MC logo
Python Assignment 2
[^] Python Home
------------------------------------------------------------------------------
[Python Assignment 1] [Python Assignment 2] [Python Assignment 3] [Python Assignment 4] [Python Assignment 5]
[A2 Example Input 2] [A2 Example Input 3] [Assignment 2 Key]

Text Generator

Assigned
Due
Jan 25
Feb 12
70 pts
Write a Python program which generates an output file based on one-line instructions in an input file.

The input consists of a series of lines each containing one command. Any extra spaces at the start and end of each line should be discarded. Blank lines are ignored. Any line which starts with a # (after stripping any leading spaces) is a comment, and is discarded.

The instructions operate on a single line, which they can modify or print. The output is the sequence of prints. The line is initially empty. Operations are performed at a current position within the line. The current position is always at the start or end of the line, or between two characters. It is at the front of the line initially. Each instruction starts with an optional numeric repeat count which defaults to one. The repeat count simply causes the command to be executed that many times. The parts of each command are separated by spaces. Here are the commands.

[ n ] insert string

Insert the indicated non-blank string into the current line following the current position. Any characters following the insertion move over to make room. The position is left at the end of the insertion.

[ n ] spaces

Insert a space (or n spaces) at the current point leaving the point after the insertion.

[ n ] cut

Cut the next n characters from the current line and copy it into a clipboard. Any existing clipboard contents is replaced. Note: because we replace the existing clipboard for each cut command, running n cut is different from the cut command n times.

[ n ] paste

Insert the contents of the clipboard into the current line following the current position. The position is moved to the end of the insertion. The clipboard does not change.

[ n ] right

Move the current location (n times) to the right. If this moves past the end of the line, the line is extended with spaces.

[ n ] left

Move the current location to the left. If the move goes past the front of the string, stop at the front.

[ n ] start

Move the current location to the start of the current string.

[ n ] end

Move the current location to the end of the current string.

[ n ] generate

Print the current line. Neither the contents of the line, the clipboard, nor the current position changes.

[ n ] clear

Empty the current line and set the position at the start. The clipboard does not change.

For the input:

2 insert go
20 right
2 insert stop
generate
start
2 spaces
15 right
4 cut
generate
start
2 spaces
15 right
4 cut
generate
clear
6 right
20 insert -
2 generate
The program produces:
gogo                    stopstop
  gogo                stopstop
    gogo            stopstop
      --------------------
      --------------------

When your program works, submit over the web here.
<<Python Assignment 1 Python Assignment 3>>