arr.append(ele) #arr = [4, 4, 1, 5, 2, 6, 3, 4, 2, 0] # make visited array vis = [0] * len(arr) if fun(arr, len(arr), 0): print("Solvable")
Course:
QUESTION:
In the following program given code of [ python programming language ] below. Add and use Artificial Intelligence
vis = []
# function will return 1 if can jump to last
# else 0
def fun(arr, n, i):
# base condition
# if reached the last index
# return 1
if i == n - 1:
return 1
# if i is out of bounds return 0
if i > n or i < 0:
return 0
# if we have already visited return 0
if vis[i] == 1:
return 0
# make this index as visited one.
vis[i] = 1
# check for left and right jump
# if any of the jump leads to the last index
# return 1
if fun(arr, n, i + arr[i]) or fun(arr, n, i - arr[i]):
return 1
# else 0
return 0
# example array
arr = []
# number of elements as input
n = int(input("Enter number of elements : "))
# iterating till the range
for i in range(0, n):
ele = int(input())
arr.append(ele)
#arr = [4, 4, 1, 5, 2, 6, 3, 4, 2, 0]
# make visited array
vis = [0] * len(arr)
if fun(arr, len(arr), 0):
print("Solvable")
else:
print("Non-Solvable")
Note: solve as soon as possible
Step by step
Solved in 3 steps with 3 images