""" Author: Sohie Lee Consulted: Peter Mawhorter Purpose: Lab 7, Nested Loops Filename: nestedLoops.py """ 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, with zero spaces before the first star. scatterSpacedStars is a None function. ''' for r in range(num): space = '' for c in range(height): print(space + '*') space += ' ' print('-') def starCount(starGrid): """ Takes a list of strings representing a grid full of dashes and/or stars. Prints the number of rows, the number of columns, and the number of stars in the grid. """ rows = len(starGrid) cols = len(starGrid[0]) count = 0 for row in starGrid: for letter in row: if letter == '*': count += 1 # Print our messages print("Rows:", rows) print("Columns:", cols) print("Stars:", count) 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 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