Lab 7: Part 3. Nested For Loops

More nested loops

Create a new file called nestedLoops.py in your lab07 folder.

Task 1. scatterStars

Write a function called scatterStars that takes a single integer parameter, num and then 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.

Here's a diagram annotating the columns made of stars:

Diagraming labeling the columns of stars

Examples:

>>> scatterStars(2)
  *
  *
  -
  *
  *
  -
>>> scatterStars(4)
  *
  *
  *
  *
  -
  *
  *
  *
  *
  -
  *
  *
  *
  *
  -
  *
  *
  *
  *
  -

Task 2. scatterHeightStars

Write a function called scatterHeightStars that takes num and height integers and 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.

Examples:

>>> scatterHeightStars(2,5)
*
*
*
*
*
-
*
*
*
*
*
-
>>>
>>> scatterHeightStars(4,3)
*
*
*
-
*
*
*
-
*
*
*
-
*
*
*
-

Task 3. scatterSpacedStars

Write a function called scatterSpacedStars that takes num and height integers and 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.

Here is a diagram that labels the spacing of the stars:

Diagraming labeling the number of spaces in front of each star in a row. The columns of stars end up looking like slanted lines, with the topmost star in the leftmost column, and each successive star pushed one space to the right. So the bottom star ends with height minus one spaces to the right of it.

Examples:

>>> scatterSpacedStars(2,5)
*
 *
  *
   *
    *
-
*
 *
  *
   *
    *
-

>>> scatterSpacedStars(4,3)
*
 *
  *
-
*
 *
  *
-
*
 *
  *
-
*
 *
  *
-

Task 4. starCount

Write a function called starCount which takes a list of strings as an argument. Each string will contain some number of dashes and also maybe some stars, like this:

[
  '--*-',
  '----',
  '-*--',
  '---*',
]

We can visualize this as a grid where each string is a row. Your function should print out the number of rows in the grid, the number of columns (you may assume that each row will be the same length) in the grid, and the number of stars anywhere in the grid, like this:

>>> starCount(['--*-', '----', '-*--', '---*'])
Rows: 4
Columns: 4
Stars: 3
>>> starCount(['***', '*-*', '***', '-*-'])
Rows: 4
Columns: 3
Stars: 9
>>> starCount(['***---', '-*-*-*', '*-**-*'])
Rows: 3
Columns: 6
Stars: 10
>>> starCount(['-****---', '---*-*-*', '**--**-*', '---**---', '-*-*---*'])
Rows: 5
Columns: 8
Stars: 17

You should use a double-for-loop to iterate down the rows and across each column.

Task 5. findStars

Write a function called findStars, which given a grid of stars, prints the row and column location of each star in the grid. Rows and columns should be numbered starting from zero. Here's an example of how it will work:

>>> findStars(['--*-', '----', '-*--', '---*'])
Found a star at row 0 column 2
Found a star at row 2 column 1
Found a star at row 3 column 3
>>> findStars(['--*', '-*-', '**-'])
Found a star at row 0 column 2
Found a star at row 1 column 1
Found a star at row 2 column 0
Found a star at row 2 column 1
>>> findStars(['*-', '-*', '--', '**', '*-'])
Found a star at row 0 column 0
Found a star at row 1 column 1
Found a star at row 3 column 0
Found a star at row 3 column 1
Found a star at row 4 column 0

Task 6. Mix and Match

Write a function called mixAndMatch that takes two lists of strings and a verb (a string), and returns a list of sentences of the form word1 verb word2 where word1 is from the first list, word2 is from the second list, and the verb is provided.

Examples:

people = ['Eni', 'Carolyn', 'Sohie']
stuff = ['python','chocolate']
bigIdeas = ['my life', 'my sleep', 'my romance']
>>> mixAndMatch(people, stuff, 'likes')
['Eni likes python', 'Eni likes chocolate', 'Carolyn likes python', 
 'Carolyn likes chocolate', 'Sohie likes python', 'Sohie likes chocolate']

>>> mixAndMatch(stuff, bigIdeas, 'rules')
['python rules my life', 'python rules my sleep', 'python rules my romance', 
'chocolate rules my life', 'chocolate rules my sleep', 'chocolate rules my romance']

Task 7. Flatten a nested list

Write a function called flatten that takes a nested list (a list of lists) and returns a new list that places each row into a single long list one after the other. Use nested loops in your flatten function.

Examples:

flatten([[0,1],[2,3,4]])
[0, 1, 2, 3, 4]

flatten([[2,4,6,8],[],[10,12]])
[2, 4, 6, 8, 10, 12]

flatten([[1,2,3],[4,5,6],[7,8]])
[1, 2, 3, 4, 5, 6, 7, 8]

flatten([[1,2,3],[4,5,6],[7,8],[9],[10]])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note that flatten only flattens lists that are nested one level deep. Deeper nested lists are not flattened, as can be seen in the example below:

flatten([[1,2,[3]],[[4]]])
[1, 2, [3], [4]]

Task 8. distribute

Write a function called distribute(list1, list2) that takes two lists and returns a new list of lists. There should be the same number of nested lists as there are elements in list1. Each nested list contains a combination of one word from list1 and each word from list2, in other words, each element of list1 should be distributed across list2. See below for examples.

>>> distribute(['a','b'],['x','y','z'])
[['ax', 'ay', 'az'], ['bx', 'by', 'bz']]

>>> fruit = ['banana', 'pear','apple','grape','peach','strawberry','watermelon']
>>> distribute(fruit,['NY', 'CA'])
[['bananaNY', 'bananaCA'], ['pearNY', 'pearCA'], ['appleNY', 'appleCA'], 
   ['grapeNY', 'grapeCA'], ['peachNY', 'peachCA'], 
   ['strawberryNY', 'strawberryCA'], ['watermelonNY', 'watermelonCA']]

>>> greetings = ['hola','bonjour','ciao','nihao']
>>> distribute(greetings,['ellen','portia'])
[['holaellen', 'holaportia'], ['bonjourellen', 'bonjourportia'], 
   ['ciaoellen', 'ciaoportia'], ['nihaoellen', 'nihaoportia']]

Table of Contents