Lab 8: Nested For Loops
More nested loops
These are additional ungraded practice exercises beyond what is in this week's exercises notebook. You can access the solutions via the main lab page.
Create a new file called nestedLoops.py
in your lab08 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:
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:
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. Mix and Match
Write a function called mixAndMatch
that takes two lists of strings and
a verb (a string), and returns a list of all 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 6. 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 7. 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']]