Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 12 Online
OpenStudy (anonymous):

I'm trying to complete problem set 4 and the find_best_shifts_rec function. It works for the most part, but for some reason, it is unable to completely decode the fable. One of the recursive calls fails at the same spot in the fable. Any help would be appreciated Here is my code: def find_best_shifts_rec(wordlist, text, start): for shift in range(27): s = text[:start] + apply_shift(text[start:], shift) space = s.find(" ", start) if space > 0 and is_word(wordlist, s[start:space]): return [(start, shift)] + find_best_shifts_rec(wordlist, s, space + 1) if space < 0 and is_word(wordlist, s[start:]): return [(start, shift)] if space < 0 and not is_word(wordlist, s[start:]): continue

OpenStudy (anonymous):

No comment on whether this'll solve anything (I'm actually trying to figure out how to do this question myself), but shouldn't space be <= 0?

OpenStudy (anonymous):

Well, I thought about putting that in there. But then I realized: space = s.find(" ", start) the above variable actually holds an index to where the space is found. So if a space is found at index 0, the text won't be a word, and so the three 'if' statements should evaluate to false in order to move onto the next shift (or integer in the for loop). Thanks for the help though.

OpenStudy (rsmith6559):

If you know where in the fable the program is failing, manually step through your code with the data that it's working with at the time.

OpenStudy (anonymous):

Thanks, it looks like the function is finding a shift that creates a word, but it's the wrong shift. Back to the drawing board.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!