View the step-by-step solution to:

Question

I have a problem implementing the recursive function. I need it to be able to go backwards and forward but I'm

struggling with implenting how it should know when to move backwards and when to otherwise move forward. I would appreciate some help with this. I have the base case down I just don't know how to control it going backwards appropriately or backwards and forward in combinations.readme.JPGpythonquestion.png

pythonquestion.png

Odef row_puzzle (square_row, position = 0):
""Takes as a parameter a list of integers (puzzle made up of row of squares) and returns True if the puzzle
10
is solvable and False if it is not"""
11
12
if position == len(square_row) - 1 and square_row[position] == 0: # base case for if the puzzle finished
13
return True
14
15
if position + square_row[position] < len(square_row) :
16
if row puzzle(square row position + square row[position]) == False:
17
if position - square_row[position] >= 0:
18
return row_puzzle (square_row, position - square_row[position])
19
else:
20
return row_puzzle (square_row, position + square_row[position])
21
22
elif position - square_row[position] >= 0:
23
return row_puzzle(square_row, position - square_row[position])
24
25
26
return False
textcases
27
28
puz_list1 = [1, 3, 2, 1, 3, 4, 0] # False
30
puz_list2 = [2, 4, 5, 3, 1, 3, 1, 4, 0] # True
I need
puz_list3 = [3, 3, 1000, 1, 4, 2, 0, 0] # False
32
puz_list4 = [1,5,4,312,0] # False
33
puz_list5 = [1, 2, 0, 1, 0] # True
puz_list6 = [1, 2, 4, 2, 5, 6, 7, 1, 8, 9, 8, 1, 3, 4, 6, 5, 8, 6, 0, 2, 2, 0] # False
puz_list7 = [1, 2, 4, 2, 5, 6, 7, 1, 8, 9, 9, 1, 3, 4, 6, 5, 8, 6, 0, 2, 2, 0] # True
36
puz_lists = [1 1,1,1,1 2 0] # False
37
puz_list9 = [1 5 4,3 2 0] # False
38
puz_list10 = [2, 3, 1, 5, 0] # True
39
puz_listil = [2, 3, 1, 0, 0] # True
40
print (row_puzzle(puz_list1) )
41
42
print (row_puzzle([0]))

readme.JPG

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Let our 24/7 Python Programming tutors help you get unstuck! Ask your first question.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors