Please do not give solution in image format thanku Python3 please! I'm having trouble understanding why my output is automatically sorted instead of the correct traversing path. Below is my code along with my assignment instructions and some testing inputs with the correct output and my output. Thank you! Assignment: My code: form.html (you can ignore this, but this is where i input numbers via a website to test my code): main.py: #!/usr/local/bin/python3 import cgi, cgitb cgitb.enable() print("Content-type: text/html\n") # Retrieve form data form = cgi.FieldStorage() text = form.getvalue('text') numbers = list(map(int, text.split())) visited = [False] * len(numbers) # function to return required answer def path_can_win(current, numbers, visited): # If the current position is out of boundary or if the beginning # position has a value of 0, return False. if current >= len(numbers) or current < 0 or numbers[0] == 0: return False # If the current position is already visited, return False. if visited[current]: return False # If the current position is the end, return True. if current == len(numbers) - 1: visited[current] = True return True # Mark the current position as visited visited[current] = True # Making recursive calls on both left and right side. if path_can_win(current - numbers[current], numbers, visited) or \ path_can_win(current + numbers[current], numbers, visited): return True else: visited[current] = False return False if path_can_win(0, numbers, visited): """ USED TO BE: path = [str(i) for i, visited in enumerate(visited) if visited] """ path = [] # Newly added pos = 0 # Newly added while pos != len(numbers)-1: # Newly added path.append(str(pos)) # Newly added pos+=numbers[pos] # Newly added, where issue occurs path.append(str(pos)) # Newly added print(" --> ".join(path) + " --> Win") else: print("No solution") Testing (1): 10 8 6 4 2 6 1 3 5 7 9 5 1 1 2 1 1 Must output (1): 0 --> 10 --> 1 --> 9 --> 16 --> Win OR 0, 10, 1, 9, 2, 8, 3, 7, 4, 6, 5, 11, 16 --> Win My output BEFORE ADDITIONS (1): 0 --> 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 10 --> 11 --> 16 --> Win My output AFTER ADDITIONS (1): pos+=num[pos] – IndexError: list index out of range Testing (2): 10 8 6 4 2 6 1 3 5 7 9 0 Must output (2): 0 --> 10 --> 1 --> 9 --> 2 --> 8 --> 3 --> 7 --> 4 --> 6 --> 5 --> 11 --> Win My output BEFORE ADDITIONS (2): 0 --> 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 10 --> 11 --> Win My output AFTER ADDITIONS (2): pos+=num[pos] – IndexError: list index out of range
Please do not give solution in image format thanku
Python3 please! I'm having trouble understanding why my output is automatically sorted instead of the correct traversing path. Below is my code along with my assignment instructions and some testing inputs with the correct output and my output. Thank you!
Assignment:
My code:
form.html (you can ignore this, but this is where i input numbers via a website to test my code):
<form action='main.py' method='POST'>
<textarea rows='10' cols='20' name='text'></textarea>
<input type='submit' name='submit' value='Submit'>
</form>
main.py:
#!/usr/local/bin/python3
import cgi, cgitb
cgitb.enable()
print("Content-type: text/html\n")
# Retrieve form data
form = cgi.FieldStorage()
text = form.getvalue('text')
numbers = list(map(int, text.split()))
visited = [False] * len(numbers)
# function to return required answer
def path_can_win(current, numbers, visited):
# If the current position is out of boundary or if the beginning
# position has a value of 0, return False.
if current >= len(numbers) or current < 0 or numbers[0] == 0:
return False
# If the current position is already visited, return False.
if visited[current]:
return False
# If the current position is the end, return True.
if current == len(numbers) - 1:
visited[current] = True
return True
# Mark the current position as visited
visited[current] = True
# Making recursive calls on both left and right side.
if path_can_win(current - numbers[current], numbers, visited) or \
path_can_win(current + numbers[current], numbers, visited):
return True
else:
visited[current] = False
return False
if path_can_win(0, numbers, visited):
""" USED TO BE:
path = [str(i) for i, visited in enumerate(visited) if visited]
"""
path = [] # Newly added
pos = 0 # Newly added
while pos != len(numbers)-1: # Newly added
path.append(str(pos)) # Newly added
pos+=numbers[pos] # Newly added, where issue occurs
path.append(str(pos)) # Newly added
print(" --> ".join(path) + " --> Win")
else:
print("No solution")
Testing (1): 10 8 6 4 2 6 1 3 5 7 9 5 1 1 2 1 1
Must output (1): 0 --> 10 --> 1 --> 9 --> 16 --> Win OR 0, 10, 1, 9, 2, 8, 3, 7, 4, 6, 5, 11, 16 --> Win
My output BEFORE ADDITIONS (1): 0 --> 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 10 --> 11 --> 16 --> Win
My output AFTER ADDITIONS (1):
pos+=num[pos] – IndexError: list index out of range
Testing (2): 10 8 6 4 2 6 1 3 5 7 9 0
Must output (2): 0 --> 10 --> 1 --> 9 --> 2 --> 8 --> 3 --> 7 --> 4 --> 6 --> 5 --> 11 --> Win
My output BEFORE ADDITIONS (2): 0 --> 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 10 --> 11 --> Win
My output AFTER ADDITIONS (2):
pos+=num[pos] – IndexError: list index out of range
Step by step
Solved in 3 steps