Day 18. Continuing to write epics (wannabe.py) This shows an interesting data structure! Not a simple int, e.g. handoutDay18.txt - so they can see up close the texts and the dictionaries built for them. Version 1 review: [wannabe_starter2.py] Look at your handouts. Sample text and the dictionary that is built. Talk through build_dict: we haven't seen any words yet, so last_word is "" the dictionary is empty, to start get the words in the line. use split, to create a list of the words in the line for each of those words: if the last word we saw is already in the dictionary, then update its entry: w, our current word, follows the last word; so, add w to the end of that list. if it isn't, create a new entry, initializing to the list containing only w. this means: we just saw last_word for the first time in the text. So far w is the only word we have seen following last word. Talk through write_epic: current_word is "" - we don't have any words yet. loop: in each iteration, we add one word to our epic just read the code; it is straightforward. starting things off? See: there is an entry in the dictionary with key "". What do our epics ALWAYS start with? 'Happy'. Ok, now look at the code and example, and trace through. Write down what is printed. # do it on the board, I guess, since they are on paper Now, the second version: Back in lecture mode. Look at the second example on the handout. prefix = 2 we are going to remember what followed every PAIR of words in the text. use a moving window happy day day and and Happy Happy day day but but Sad etc. so, what changes in our code? the keys in the dictionary are tuples. initial context: ('','') prefix == 2 the last 2 words are nothing, nothing ('','','') prefix == 3 the last 3 words are nothing, nothing, nothing ('','','','') prefix == 4 the last 4 words are nothing, nothing, nothing, nothing the code? context = ('',) you need the "," so python knows it is a tuple. just something you need to remember/check in code you saw before for i in range(context_length-1): suppose context_length = 3. Then, this is: for i in [0,1]: so, we'll do this twice. we already have the first one. context = context + ('',) + words for tuples too. show in the shell. OK? We read through the words just the same way. instead of last_word, we have context - compare the two pieces of code - you have them on your handout. the interesting thing: updating the context. suppose: context = (but, Sad) word = minute we just added new entry word_dict[(but, Sad)] = [minute] so, we are HERE in the text - show on the board with arrows we want to slide our window over 1 to the right want context to be (Sad, minute) we want to kick one word off the last, and slot in word to the right draw a picture context[1:] + (word,) suppose we are running it with prefix == 3 context = (day, but, Sad) word = minute context[1:] is (but, Sad) word is minute so, context[1:] + (minute,) is (but, Sad, minute)