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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Processes of 3D Graphics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY