Computer Science 1 CSci 1100 Lab 7: Easter Egg Hunt




Computer Science 1 | CSci 1100
Lab 7 | Easter Egg Hunt
Lab Overview
This lab uses an Easter egg hunt to review the use of les. Please download the le from the Piazza site. This includes a set of large text les named 1.txt,
2.txt, etc. Inside these les are hidden Python programs, total 6 of them. To hunt them
down, you need to recover the individual lines and write them down into a program le.
Then, you must execute that le within Wing. You will have to do a bit of le parsing to
get there. Collect them all!
Checkpoint 1: More Text Processing Practice
Write a function called parse_line that takes as input a line of text such as
Here is some random text, like 5/4=3./12/3/4
and converts this into a four tuple of the form:
(12,3,4,”Here is some random text, like 5/4=3.”)
where the last three values are converted to integers, and the remainder of the text is a
string. The last values are always assumed to be separated by a slash.
You must however do some error checking. If you do not have a line that has at least three
/ in this given format or if the values contained in the last three slots are not integers,
then your function should return None. Here are some example runs of this function:
>>>parse_line(“Here is some random text, like 5/4=3./12/3/4”)
(12, 3, 4, Here is some random text, like 5/4=3. )
>>>parse_line(“Here is some random text, like 5/4=3./12/3/4as”)
>>>parse_line(“Here is some random text, like 5/4=3./12/412/a/3/4″)
>>>parse_line(” Here is some spaces 12/32/4″)
>>>parse_line(” Again some spaces\n/12/12/12″)
(12, 12, 12, Again some spaces\n )
Some advice here. You can use a limited version of split for this or use more complex
processing using find. We recommend using split, appropriate slicing operations, and
perhaps join. This approach will save you time.
To complete Checkpoint 1: Show your code and output once you are nished.
Checkpoint 2: Parsing some les
Copy your le from checkpoint 1 to a new le called We will not use the
function you have just implemented in this part, but it is good to keep it around for the
next checkpoint. Make sure you save the program le into the same folder as the text les
given in the ZIP folder.
This checkpoint is the second step to building your Easter egg hunt code. This is probably
the most complex part of your lab. You will write a new function:
which takes as input a le name and two numbers. Basically, the two numbers tell you
which paragraph and which line your Easter egg hunt lies. Paragraphs are separated by
any number of blank lines, containing nothing but the new line character and spaces.
You must open the given le, skip all the paragraphs up to the requested one (parno), skip
all the lines until the given line (lineno), read and return the requested line. For example,
suppose a le contains the following text:
I love deadlines. I love the whooshing
noise they make as they go by.
I refuse to answer
that question on the
grounds that
I don t know the answer.
If we are looking for paragraph 2 and line 3, your function should return the string
grounds that. You should remove any trailing whitespace such as \n , but be sure
to preserve leading whitespace. Refer back to lecture 13 and the str.rstrip() function if you
are having problems with this part.
Write a program that asks the user a le number, a paragraph number and a line number.
Then, call this function to nd the line at this given location. For example, using the les
given to you, here are some example runs:
Please enter the file number ==> 1
Please enter the paragraph number ==> 5
Please enter the line number ==> 4
import webbrowser/2/3/3
Please enter the file number ==> 2
Please enter the paragraph number ==> 3
Please enter the line number ==> 3“”)/2/4/4
Please enter the file number ==> 2
Please enter the paragraph number ==> 4
Please enter the line number ==> 4
To accomplish this, think of using two variables: one for keeping track of paragraph number
and the second one for the line number in that paragraph. Now, keep updating these
numbers as you read a new line from the le.
To complete Checkpoint 2, show a TA or mentor your code once you have checked it
for these test cases.
Now for the Easter egg hunt!
If we look at the above examples, we can see the following. File 1, paragraph 5, line 4
contains the line: import webbrowser/2/3/3
This means two things:
• The rst line of the program you are reading is: import webbrowser
• The next line of your program is at le 2.txt, paragraph 3 and line 3.
The line at 2/3/3/ contains the content:“”)
This line points you to the next line, which is at 2/4/4. The line at this location has the
content: END/0/0/0
This means, you are at the end of the program. By following these clues about where the
next line is, you have read a program with the following lines:
import webbrowser“”)
Your Easter egg hunt starts at a given line and continues to nd the next line until you
reach the end. All the lines you found are part of a small Python program.
Checkpoint 3: Putting it all together
Now, we are going to put these together. Copy your code from the previous checkpoint to
a le called
As in checkpoint 2, you will ask from the user a starting point: le number, paragraph
number and the line number.
You will rst nd the rst line of the program. Then, you will construct the program using
a WHILE loop, until you reach the line END/0/0/0. Write the lines you found in a le
called When done, execute the recovered program. Try the following starting
You program must report failure if your parsing fails, which would happen if you have a
bug in your code for counting paragraphs and lines or an incorrect starting point is given.
To complete Checkpoint 3, show your code to the TA who will test it with a given
Easter egg.
Things to think about
Well, you actually do not need to be told where the eggs are. There is an algorithm to
recover them. Can you outline it or even better write it? Hint: it involves a type of while
loop and very doable at your current level of programming.
There is a very easy algorithm that works for some eggs. But you need to get more sophis-
ticated to nd all of them. Remember: they are not all that well hidden.
Please post to Piazza your ideas of how to discover all the eggs, but do not post the locations.
See if others can gure out where they are or how to nd them. Some of these eggs are
actually quite elaborate programs, so happy hunting!