In: Computer Science
Develop a python program to
- Create a 2D 10x10 numpy array and fill it with the random numbers between 1 and 9 (including 0 and 9).
- Assume that you are located at (0,0) (upper-left corner) and want to move to (9,9) (lower-right corner) step by step. In each step, you can only move right or down in the array.
- Develop a function to simulate random movement from (0,0) to (9,9) and return sum of all cell values you visited.
- Call the function 3 times to show current the sum.
Note 1: 2D numpy array will be created and randomly filled just once.
Note 2: You cannot move out of array, up, or left.
##IF YOU ARE SATISFIED WITH THE CODE, KINDLY LEAVE A LIKE, ELSE COMMENT TO GET YOUR DOUBTS CLEARED
CODE:
#FUNCTION TO CALCULATE SUM:
def print_sum(array):
sum_array = 0
pos_x = 0
pos_y = 0
while pos_x!=9 and pos_y!=9:
if pos_x == 9:
pos_y += 1
elif pos_y == 9:
pos_x += 1
else:
value = np.random.randint(0,2)
if value == 0:
pos_x += 1
else:
pos_y += 1
sum_array += array[pos_x][pos_y]
print("\nSUM is: ",sum_array)
#driver code:
import numpy as np
array = np.random.randint(1,10, size=(10, 10))
print("Original Array:")
print(array)
print_sum(array)
print_sum(array)
print_sum(array)
OUTPUT: