Question: Please define these functions in python: GridString: a string of periods (.), capital O's, and newlines (' '). When printed, it would look like a
Please define these functions in python:
GridString: a string of periods (.), capital O's, and newlines (' '). When printed, it would look like a rectangle of periods (dead cells) and O's (living cells), and we think of this as the grid representation
1. 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.
Hint: s.split() is your friend. What should you split by?
o read_coords("O.. .OO ") [(0,0), (1,1), (1,2)]
o read_coords(" O.. .OO ") [(0,0), (1,1), (1,2)] # ignore blank lines
o read_coords("..... ..... ") []
2. get_dimensions(s): Given a GridString s, find out how many rows and columns are represented in the GridString, and return them as a tuple: (numrows, numcols). Remember that any blank lines must be ignored (skipped). If the rows don't all have the same number of items in them, or any unexpected characters are present, this function returns None.
o Assume: s is a GridString.
o get_dimensions("O.. .OO ") (2,3)
o get_dimensions("OOOOO ... OO ......") None # not rectangular!
o get_dimensions(" OO .. .O O. .. ") (5,2) # note ignored b
3. build_grid(s): Given a GridString s, determine the dimensions, build a grid of that size, and make alive each cell that should be alive.
o Assume: s is a GridString. o Hint: try to use read_coords, get_dimensions, and build_empty_grid in your solution.
o build_grid("O.. .OO ") [[True, False, False], [False, True, True]]
o build_grid(".. .O OO ") [[False,False], [False, True], [True, True]]
o build_grid("OO.O..") [[True, True, False, True, False, False]]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
