# Lab. Nested loop practice #Task 1 def scatterStars(num): ''' Uses nested for loops to print num columns of num stars. Each column of stars ends with a single dash -. scatterStars is a None function.''' for r in range(num): for c in range(num): print('*') print('-') def scatterHeightStars(num, height): ''' Uses nested for loops to print num columns of height stars. Each column of stars ends with a single dash -. scatterHeightStars is a None function.''' for r in range(num): for c in range(height): print('*') print('-') def scatterSpacedStars(num,height): '''Both num and height are integers. Uses nested for loops to print num columns of height stars. Each column of stars ends with a single dash -. Each successive star in a given column is spaced one more space to the right than the star above it. scatterSpacedStars is a None function. ''' for r in range(num): space = '' for c in range(height): space += ' ' print(space + '*') print('-') # Task 4 def mixAndMatch(list1, list2, verb): '''Returns a list of sentences built from combining word1 + verb + word2 where word1 is each word in list1 and word2 is each word in list2''' newlist = [] space = ' ' for word1 in list1: for word2 in list2: newlist.append(word1 + space + verb + space + word2) return newlist # Task 5 def flatten(nestedList): """ Flattens a nested list into one simple list, using nested loops. Returns a new list (does not modify the original list). """ result = [] for inner in nestedList: for element in inner: result.append(element) return result # Task 6 distribute def distribute(list1,list2): """ Distributes the contents of list1 across list2, creating a new list element for every combination. Returns a new nested list that contains the same number of nested lists as the original list1, and each nested list has the same length as the original list2. For example, distribute(['a','b'],['x','y','z']) returns [['ax', 'ay', 'az'], ['bx', 'by', 'bz']] """ result = [] minilist = [] for item1 in list1: for item2 in list2: minilist.append(item1+item2) result.append(minilist) minilist = [] # now start a new nested list from scratch again return result # Task 7A Grid Sum def gridSum(grid): """ listOfLsits should be a list of lists containing numbers. This function returns the sum of all of the numbers in each row (i.e., the sum of the entire grid). """ result = 0 for row in grid: # Each row is itself a list of numbers for number in row: result += number return result # Task 7B Coverage def gridNoZeroes(grid): """ Returns True if the grid has no zeroes in it. """ for row in grid: for number in row: if number == 0: return False # we found a zero, we can return immediately # We survived the entire loop without finding a zero, so there must # not be any in the whole grid! return True # Task 7B Max Cell def maxCell(grid): """ Returns the row, column coordinates of the cell in the grid with the largest value, as a pair of numbers. Both row and column are counted starting from 0, not 1. If there are multiple cells tied for largest, this implementation returns the coordinates of the first such cell in top-to-bottom left-to-right order. """ # First find the maximum value of the whole grid: gridMax = None for row in grid: for num in row: # Update our max if we didn't have one or if this num is greater if gridMax == None or num > gridMax: gridMax = num # Next look through the grid until we find an entry that equals the # max, and return the coordinates of that entry: for r in range(len(grid)): for c in range(len(row)): # We index twice here, using row and column indices if grid[r][c] == gridMax: return (r, c) # The loop above should always be able to find a max value! print("ARGGHH. THIS SHOULDN'T BE POSSIBLE!") def maxCellAlt(grid): """ An alternate implementation of maxCell that only uses a single nested loop instead of a nested loop plus a follow-up loop. """ maxSoFar = None bestCoords = None for r in range(len(grid)): # We iterate using numerical indices ('r' for row index), # but immediately make a 'row' variable that holds the same value # we'd get if our loop was `for row in grid`: row = grid[r] for c in range(len(row)): # Likewise, we iterate by indices but grab the value # immediately, as if we were doing `for num in row`: num = row[c] # If we didn't have a max yet, or if we did and this number # is greater than that max, we'll update our max and record # these coordinates as the best ones. if maxSoFar == None or num > maxSoFar: maxSoFar = num bestCoords = (r, c) # After the entire loop, bestCoords should hold the coordinate of a # cell that's at least tied for maximum value. return bestCoords