Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

=+5 Evaluate whether the CCT program was effective.

Answered: 1 week ago

Question

=+Identify the type of global assignment for which CCT is needed.

Answered: 1 week ago