@extends('template') @section('title') Lab 7: Part 3. Nested For Loops @stop @section('head') @stop @section('content') # Lab 7: Part 3. 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 lab07 folder. ### Task 1. `scatterStars`
Partner A
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:__ ```py >>> scatterStars(2) * * - * * - >>> scatterStars(4) * * * * - * * * * - * * * * - * * * * - ``` ### Task 2. `scatterHeightStars`
Partner B
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:__ ```py >>> scatterHeightStars(2,5) * * * * * - * * * * * - >>> >>> scatterHeightStars(4,3) * * * - * * * - * * * - * * * - ``` ### Task 3. `scatterSpacedStars`
Partner A
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:__ ```py >>> scatterSpacedStars(2,5) * * * * * - * * * * * - ``` ```py >>> scatterSpacedStars(4,3) * * * - * * * - * * * - * * * - ``` ### Task 4. `starCount`
Partner B
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: ```py [ '--*-', '----', '-*--', '---*', ] ``` 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: ```py >>> 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
Partner A
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:__ ```py 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
Partner B
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:__ ```py 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: ```py flatten([[1,2,[3]],[[4]]]) [1, 2, [3], [4]] ``` ### Task 7. distribute
Partner A
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. ```py >>> 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']] ``` @include('/labs/lab07/_toc') @stop