Question: read_coords(s): Given a GridString s, read through it and create a list of int pairs for all live cells. Each pair is a (row, column)

read_coords(s): Given a GridString s, read through it and create a list of int pairs for all live cells. Each pair is a (row, column) coordinate. If the rows don't all have the same number of spots indicated, or if any unexpected characters are present, this function returns None. Must be ordered by lowest row, and lowest column when rows match.

I found this solution online yet, it is not perfect. I was wondering if someone could fix it to make it work better.

def read_coords(s): each = s.split(' ') x = 0 result = [] for i in each: y = 0 if len(i) > 0: for j in i: if j == '0': result.append((x,y)) elif j != '0': return None y = y+1 x = x +1 print (result)

the problem is that the printed result is the correct answer, but the code will always return nothing. Is there a way to fix the code so that the printed answer matches the returned answer?

Here are some testers:

read_coords("O.. .OO ") = [(0,0), (1,1), (1,2)]) 
read_coords(" O.. O.O ") = [(0,0), (1,0), (1,2)] 
read_coords("..... ..... ") = [] 
read_coords("... . ") = None 
read_coords("OOO OOO ") = [(0,0),(0,1),(0,2),(1,0),(1,1),(1,2)] 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!