Right, so I'm working on Problem Set 4, and have found myself stuck on the 'find best shift' part. I've gotten everything before it, I think, but right now every time my recommended shift is '26'.
Here's my current code for it:
ctr = -1
Yaaay = False
while ctr<=25 and Yaaay == False:
ctr +=1
apply_coder(text, build_decoder(ctr))
current = 0
spacelist = [-1]
while current
So the code didn't get completely through. Here's the actual code: ctr = -1 Yaaay = False while ctr<=25 and Yaaay == False: ctr +=1 apply_coder(text, build_decoder(ctr)) current = 0 spacelist = [-1] while current<len(text): if text[current] == ' ': spacelist.append(current) current +=1 k = 1 good = True if len(spacelist)>1: while k<len(spacelist) and good == True: Word = text[spacelist[k-1]+1:spacelist[k]] if Word in wordlist: good = True else: good = False k +=1 else: if text in wordlist: good = True else: good = False Yaaay = good return ctr
I've returned your function below, I've put in a bunch of print statements that show some of the values as your loop walks through each value. Once you see the values, try uncommenting a block of code I put in that is commented out by 4 hash tags: ### TODO ctr = -1 Yaaay = False while ctr<=25 and Yaaay == False: ctr += 1 ## print "text before apply_coder = ", text ## apply_coder(text, build_decoder(ctr)) ## function is changing text, but not assigning that value to any variable codedText = apply_coder(text, build_decoder(ctr)) ## try this #### print "text after apply_coder = ", text #### words = codedText.split() #### print 'words = ', words, 'ctr =', ctr #### for word in words: #### if is_word(wordlist, word): #### print True current = 0 spacelist = [-1] ## print "len(text) = ", len(text) while current<len(text): if codedText[current] == ' ': ## print "text[current] = ", text[current] spacelist.append(current) current +=1 k = 1 good = True ## print 'spacelist = ', spacelist if len(spacelist)>1: while k<len(spacelist) and good == True: # will always only go through once, good will always be set to False Word = codedText[spacelist[k-1]+1:spacelist[k]] print "k = ", k, "codedText = ", codedText, " codedText[spacelist[k-1]+1:spacelist[k]] = ", codedText[spacelist[k-1]+1:spacelist[k]] if Word in wordlist: ## when evaluating Word, this doesn't account for any punctuation. print "word is in wordlist" good = True else: print 'good is false, ', Word, 'is not a word.' good = False k += 1 else: if codedText in wordlist: print "good = True" good = True else: print 'good is false, ', codedText, 'is not a word.' good = False Yaaay = good print 'returning, ctr = ', ctr, '. Yaaay = ', Yaaay return ctr
Join our real-time social learning platform and learn together with your friends!