Question: I need help on my python code.... This program should: Use three functions to iterate over 2D lists in different ways. These functions will return

I need help on my python code....

This program should:

  • Use three functions to iterate over 2D lists in different ways. These functions will return the number of times a given thing appears in the rows, columns, or grids of the 2D list. Here is a recommendation for solving these problems:
    1. Write a nested for loop that prints the indices for the desired values.
    2. Use these indices to count the number of things in one unit of the problem (i.e. one row, one column, one grid).
    3. Repeatedly append that count to a list within the for loop. After the for loop, return that list.

Here are the specifications for the three functions:

  • countInRows(l, thing)
    • Takes a 2D list of any size and a string thing.
    • Counts the number of times thing appears in each row.
    • Returns a list holding the counts of thing for each row.
  • countInCols(l, thing)
    • Takes a 2D list of any size and a string thing.
    • Counts the number of times thing appears in each column.
    • Returns a list holding the counts of thing for each column.
  • countIn2x2Grids(l, thing)
    • Takes a 2D list of any even dimensions and a string thing.
    • Counts the number of times thing appears in each 2 X 2 grid by calling countInGrid with the upper-left index of every grid, then append that number to the final list.
    • Returns a list holding the counts of thing for each 2 X 2 grid.
  • countInGrid(l, thing, start_i, start_j)
    • Takes a 2D list, a string thing, a start index, and an end index.
    • Use the start and end indices to isolate one 2x2 grid and count the number of times thing appears only in that 2x2 grid.
    • Return the count of thing found in the grid as an int.

Example Input:

l = [ ["A", 1 , 2 , 3 , 4 , 5], ["A","A", 2 , 3 , 4 , 5], [ 1 , 2 , 3 , 4 , 5 , 5 ], ["A","A","A","A","A","A"], ["A", 3 ,"A", 4 ,"A","A"], [ 1 , 3 , 5 ,"A", 5 ,"A"] ] 

Example Outputs:

countInRows(l, thing):

countInRows(l, 'A') = [1, 2, 0, 6, 4, 2] 

countInCols(l, thing):

countInCols(l, 'A') = [4, 2, 2, 2, 2, 3] 

countIn2x2Grids(l, thing):

countIn2x2Grids(l, 'A') = [3, 0, 0, 2, 2, 2, 1, 2, 3] 

This is the code provided:

def countInRows(l, thing): #loop over each row of the 2D list and count the occurences of 'thing' in list 'l' #append count for each row in 'row_count' list and return it row_count=[] return row_count

def countInCols(l, thing): #loop over each column of the 2D list and count the occurences of 'thing' in list 'l'. #append count for each column in 'col_count' list and return it col_count=[] return col_count

def countIn2x2Grids(l, thing): #For a given grid with even number of rows and columns, this function should loop through each 2x2 grid. #call function countInGrid for each possible 2x2 grid in this function inside a nested for-loop #pass the starting row and column for each grid #append count values for each grid in count_grids list count_grids =[] return count_grids def countInGrid(l, thing, start_i, start_j): #should return count of 'thing' in given grid. Should be a number not a list c = 0 return c

def main(): l = [ ["A", 1 , 2 , 3 , 4 , 5], ["A","A", 2 , 3 , 4 , 5], [ 1 , 2 , 3 , 4 , 5 , 5 ], ["A","A","A","A","A","A"], ["A", 3 ,"A", 4 ,"A","A"], [ 1 , 3 , 5 ,"A", 5 ,"A"] ] print(countInRows(l,"A")) print(countInCols(l, "A")) print(countIn2x2Grids(l, "A")) if __name__ == "__main__": main()

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!