Starting from:

$15

Easter Egg Hunt soln

Lab Overview This lab uses an Easter egg hunt to review the use of files. Please download the file lab07_files.zip from the Piazza site. This includes a set of large text files named 1.txt, 2.txt, etc. Inside these files 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 file. Then, you must execute that file within Wing. You will have to do a bit file parsing to get there. Collect them all! Checkpoint 1: Simple text processing This checkpoint is very easy, should take you less than 10 minutes to do. Write a simple function called parse_line that takes as input a line of text such as: 12/3/4/Here is some random text, like 5/4=3. You should then convert this to a four tuple of the form: (12,3,4,"Here is some random text, like 5/4=3") where the first three values are converted to integer, and the remainder of the text is a string. The first values are always assumed to be separated by a slash. You must however do some error checking. If you do not have a line in this given format, or the values contained in the first three slots are not integers, then your function should return None. Here are some example runs of this function: parse_line("12/3/4/Here is some random text, like 5/4=3") (12, 3, 4, 'Here is some random text, like 5/4=3') parse_line("as12/3/4/Here is some random text, like 5/4=3") None parse_line("12/a/412/3/4/Here is some random text, like 5/4=3") None parse_line("12/32/4 Here is some spaces ") None parse_line("12/12/12/ Again some spaces\n") (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. To complete Checkpoint 1: Show your code and output once you are finished. Checkpoint 2: Parsing some files Copy your file from checkpoint 1 to a new file called check2.py. 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 file into the same folder as the text files 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: get_line(fname,parno,lineno) which takes as input a file 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 file, 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 file 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 of paragraph 2 and line 3, your function should return the string grounds that\n. Do not remove the newline, that will be important for the next checkpoint. Write a program that asks the user a file number, a paragraph number and a line number. Then, call this function to find the line at this given location. For example, using the files given to you, here are some example runs: Please file number == 1 Please enter paragraph number == 5 Please enter the line number == 5 2/3/3/import webbrowser Please file number == 2 Please enter paragraph number == 3 Please enter the line number == 3 2/4/4/webbrowser.open("http://hackertyper.com/") Please file number == 2 Please enter paragraph number == 4 Please enter the line number == 4 0/0/0/END 2 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 5 contains the line: 2/3/3/import webbrowser This means two things: • The first line of the program you are reading is: import webbrowser • The next line of your program is at file 2.txt, paragraph 3 and line 3. The line at 2/3/3/ contains the content: webbrowser.open("http://hackertyper.com/") This line points you to the next line, which is at 2/4/4. The line at this location has the content: 0/0/0/END 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 webbrowser.open("http://hackertyper.com/") Your Easter egg hunt starts at a given line and continues to find 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 file called check3.py. As in checkpoint 2, you will ask from the user a starting point: file number, paragraph number and the line number. You will first find the first line of the program. Then, you will construct the program using a WHILE loop, until you reach the line 0/0/0/END. Write the lines you found in a file called program.py. When done, execute the recovered program. Try the following starting points: 1/5/5 8/27/6 1/82/10 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. 3 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 sophisticated to find 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 figure out where they are or how to find them. Some of these eggs are actually quite elaborate programs, so happy hunting! 4

More products