In: Computer Science
I am creating a crop watering "simulator" in Python. I have the user input an array and then must compare the placement of water and crops to determine if all the crops in the array are watered. The user will either input a "w" for water or "c" for crop when creating the array. A w cell can water 8 cells around it, including itself. My end result must determine if all the crops will be watered or not. How do I compare the values in the cells to output this? And how do I output the crops not watered, if they are not all watered?
Here is an example, given the following array:
cccw
wccc
cccc
cwcc
How do I ouput that not all the crops are watered, and the crops in positions (2,3) and (3,3) are not watered?
Here is my code so far as well:
print("Input number of rows and columns in the crop field:")
rows = int(input("ROWS>"))
columns = int(input("COLUMNS>"))
array = []
for i in range(columns):
array.append([])
for j in range(rows):
current_input=input(f"Type in the value for row={i}, column={j}: ")
array[i].append(current_input)
for i in array:
print(i)
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code with no comments, for easy copying
def neighbors(array, r, c):
values = []
if (r - 1) >= 0:
values.append((r - 1, c))
if (r - 1) >= 0 and (c - 1) >= 0:
values.append((r - 1, c - 1))
if (r - 1) >= 0 and (c + 1) < len(array[r]):
values.append((r - 1, c + 1))
if (r + 1) < len(array):
values.append((r + 1, c))
if (r + 1) < len(array) and (c - 1) >= 0:
values.append((r + 1, c - 1))
if (r + 1) < len(array) and (c + 1) < len(array[r]):
values.append((r + 1, c + 1))
if (c - 1) >= 0:
values.append((r, c - 1))
if (c + 1) < len(array[r]):
values.append((r, c + 1))
return values
print("Input number of rows and columns in the crop field:")
rows = int(input("ROWS> "))
columns = int(input("COLUMNS> "))
array = []
for i in range(rows):
array.append([])
for j in range(columns):
current_input = input(f"Type in the value for row={i}, column={j}: ")
array[i].append(current_input)
for i in array:
print(i)
for i in range(rows):
for j in range(columns):
if array[i][j] == 'w':
array[i][j] = 'x'
for r,c in neighbors(array,i,j):
array[r][c]='x'
not_watered=[]
for i in range(rows):
for j in range(columns):
if array[i][j]=='c':
not_watered.append((i,j))
if len(not_watered)==0:
print("All crops are watered")
else:
print("Not all crops are watered; unwatered crops are in position(s): ",end='')
print(*not_watered,sep=', ')
print()
#same code with comments, for learning
# helper method defined to return the row,column indices of all cells around r,c
# assuming r and c are valid
def neighbors(array, r, c):
# creating a list
values = []
# if r-1 is a valid index, adding tuple (r-1,c) to values
if (r - 1) >= 0: # top center
values.append((r - 1, c))
# similarly checking addresses of all cells around (r,c), adding to values
if (r - 1) >= 0 and (c - 1) >= 0: # top left
values.append((r - 1, c - 1))
if (r - 1) >= 0 and (c + 1) < len(array[r]): # top right
values.append((r - 1, c + 1))
if (r + 1) < len(array): # bottom center
values.append((r + 1, c))
if (r + 1) < len(array) and (c - 1) >= 0: # bottom left
values.append((r + 1, c - 1))
if (r + 1) < len(array) and (c + 1) < len(array[r]): # bottom right
values.append((r + 1, c + 1))
if (c - 1) >= 0: # left
values.append((r, c - 1))
if (c + 1) < len(array[r]): # right
values.append((r, c + 1))
# returning values
return values
print("Input number of rows and columns in the crop field:")
rows = int(input("ROWS> "))
columns = int(input("COLUMNS> "))
array = []
for i in range(rows):
array.append([])
for j in range(columns):
current_input = input(f"Type in the value for row={i}, column={j}: ")
array[i].append(current_input)
for i in array:
print(i)
# we read and print the 2d list, now we can mark all watered crops with 'x'
# looping through each row and column
for i in range(rows):
for j in range(columns):
# checking if value at i,j is 'w'
if array[i][j] == 'w':
# marking current position with value 'x'
array[i][j] = 'x'
# looping through each neighbor cell of i,j and marking them
for r, c in neighbors(array, i, j):
array[r][c] = 'x'
# now creating a list
not_watered = []
# looping and adding row,col indices of all unwatered crops as tuples into not_watered list
for i in range(rows):
for j in range(columns):
if array[i][j] == 'c': # value is still 'c', means unwatered crop
not_watered.append((i, j))
# if not_watered list is empty, then all crops are watered
if len(not_watered) == 0:
print("All crops are watered")
else:
# else, not all crops are watered
print("Not all crops are watered; unwatered crops are in position(s): ", end='')
print(*not_watered, sep=', ') # printing tuples in not_watered separated by ', '
print()
#output
Input number of rows and columns in the crop field:
ROWS> 4
COLUMNS> 4
Type in the value for row=0, column=0: c
Type in the value for row=0, column=1: c
Type in the value for row=0, column=2: c
Type in the value for row=0, column=3: w
Type in the value for row=1, column=0: w
Type in the value for row=1, column=1: c
Type in the value for row=1, column=2: c
Type in the value for row=1, column=3: c
Type in the value for row=2, column=0: c
Type in the value for row=2, column=1: c
Type in the value for row=2, column=2: c
Type in the value for row=2, column=3: c
Type in the value for row=3, column=0: c
Type in the value for row=3, column=1: w
Type in the value for row=3, column=2: c
Type in the value for row=3, column=3: c
['c', 'c', 'c', 'w']
['w', 'c', 'c', 'c']
['c', 'c', 'c', 'c']
['c', 'w', 'c', 'c']
Not all crops are watered; unwatered crops are in position(s): (2, 3), (3, 3)