A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 one year ago
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
anonymous
 one year ago
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

This Question is Closed

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0No 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?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Well, 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.

rsmith6559
 one year ago
Best ResponseYou've already chosen the best response.1If 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.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Thanks, it looks like the function is finding a shift that creates a word, but it's the wrong shift. Back to the drawing board.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.