Review for the exam Only 55 minutes - due to the evaluations. === Suppose we want to print: x = 54.33333333 st = "hello there bear" x=54.3333333333 and st="hello there bear" i = 4 Let's try it with a plain print: print "x=",x, "and st=", st, "and i=",i the simple print statement always includes a space here's a fancier version: print string with placeholders % (variables filling in the placeholders) placeholders: %d for an int %s for a string %f for a float print "x=%f and st=%s and i=%i" % (x, st,i) Suppose we want just two decimal digits: print "x=%.2f and st=%s and i=%i" % (x, str,i) if you don not want to skip a line, put the comma at the end: print "x=%f and st=%s" % (x, st), print "and i=%d" % (i) === tips: let's review a couple things from the last exam which will be relevant for this one too. Write a program - i'll have a question similar to this one, just with a different function. "Program" - you'll have an if __name__ == '__main__' part Figure out the function: what parameters what am i returning, if anything? do I want a return statement? if so, what? what am i inputting, if anything? what am i outputting, if anything? Then, so what is "leftover" to put in the main program, and figure out how to call the function. For just the function questions - don't write a main program. Make sure you understand first what to do. ==Tips for what to focus on assignment 3: make sure you (something in this will be on exam) know the data structures (pictures, faces, ...) understand the interaction with the user how we read from a file You can focus on a3_sol1.py - that's the longer one that is probably easier to understand. ==wannabe.py we spent two lectures on this! it was not in the material covered by the last exam. it was an interesting data structure! let's look at it again: build a dictionary, which remembers what words follows others in an input text. Then, it uses that dictionary to generate nonsense text that is the same style. Remind you using an example: simple_1_txt_and_dict.txt Happy day and Happy day but Sad minute so Sad minute drats! {('Happy', 'day'): ['and', 'but'], ('', 'Happy'): ['day'], ('minute', 'so'): ['Sad'], ('Sad', 'minute'): ['so', 'drats!'], ('day', 'and'): ['Happy'], ('minute', 'drats!'): [''], ('', ''): ['Happy'], ('but', 'Sad'): ['minute'], ('and', 'Happy'): ['day'], ('day', 'but'): ['Sad'], ('so', 'Sad'): ['minute']} key: ('Happy','day') value: ['and','but'] see why? Let's try another one: I added this to the webpage simple_2* Happy day and Happy day and Happy day but Sad minute so Sad minute drats! {('but', 'Sad', 'minute'): ['so'], ('Happy', 'day', 'but'): ['Sad'], ('Sad', 'minute', 'so'): ['Sad'], ('Sad', 'minute', 'drats!'): [''], ('minute', 'so', 'Sad'): ['minute'], ('', '', 'Happy'): ['day'], ('day', 'but', 'Sad'): ['minute'], ('so', 'Sad', 'minute'): ['drats!'], ('and', 'Happy', 'day'): ['and', 'but'], ('Happy', 'day', 'and'): ['Happy', 'Happy'], ('day', 'and', 'Happy'): ['day', 'day'], ('', 'Happy', 'day'): ['and'], ('', '', ''): ['Happy']} Give me an example of a key and a value ('Happy', 'day', 'and'): ['Happy', 'Happy'] == explain: ('', '', ''): ['Happy'] - try to get them to. this is how we start things off. context = ('',) for i in range(context_length-1): context = context + ('',) Let's trace it: context_length = 3 context_length - 1 = 2 context ('',) i 0 SO: this gives us the right size tuple SHOW, with your fingers: conceptually: '' '' '' is the beginning of the text Happy day and Happy day and Happy day but Sad minute so Sad minute drats! ('', '', ''): ['Happy'] ('', '', 'Happy'): ['day'] ('', 'Happy', 'day'): ['and'] ('Happy', 'day','and'): ['Happy'] ('day', 'and', 'Happy'): ['day'] ('and', 'Happy', 'day'): ['and'] and so on ... So, while building the dictionary, context goes through all the triples of the input text. context = a tuple with context_length '' for each line in the text: split the line into a list of words for word in word_list: add word to the dict entry for context update context to the next triple in the text how do we do the last line? suppose context is: ('day', 'and', 'Happy'): word is 'day' updated dict: ('day', 'and', 'Happy'): ['day'] Now we need to move onto the next triple, which is ('and', 'Happy', 'day') How do we do this in a general way? drop one word off left and add 'word' to the right context = context[1:] + (word, ) Then, once we have the dictionary built, we generate an epic. talk through the code. ==string search Remember: we went through some incorrect solutions; then a correct one [debug*py on the schedule] then we looked at three versions of the code to search through a string. this also illustrated two interesting things: timing - how we can compare the efficiency of code las-cruces.cs.pitt.edu:0:22> python timing_all.py Elapsed time for get_indices: 0.0018949508667 Elapsed time for basic_stringmatch: 0.764273881912 Elapsed time for early_exit_stringmatch: 0.121440172195 las-cruces.cs.pitt.edu:0:23> python Python 2.4.1 (#1, Jun 16 2005, 14:50:07) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 0.764 / 0.121 6.3140495867768598 >>> 0.764 / 0.002 382.0 >>> 0.121/.002 60.5 We'll return to this topic after the exam. Also, we illustrated what __nam__ == '__main__' is good for. You should have plenty of time on the exam - it's the same as the others. Good idea? Trace your code on examples to determine if you are correct. Also, watch out for special cases to take care of! ==tracing: I understand you had good sessions in lab with Nick going over this. to practice - trace code on the webpage! do it on paper, check yourself by tracing in WING Questions will use code in labs and on the webpage. Remember, some of the code is wrong! As I said last time, I want you to study by tracing the code. So, that's what you'll do on the exam as well. As you can see from the test2 information: new material: lab7, 8, 10 - I will not ask anything about java. click on the solution files - see if they have questions lectures since 10/12 again, click on the files, and see if they have questions. this includes the files going over options for the exam.