Lab 8
Studying Nested Loops
Submit by the end of lab time

Overview

This lab involves studying and explaining how nested loops work, both based on iteration loops and index-based loops.

Using lab zip folder

Download the zip folder for lab 8. Unzip it and study the code in lab8.py. Because colors is already defined for you, you may test out both functions using colors as an argument.

Questions

For the following, either answer the question or provide the requested code:

  1. color_match returns a list of pairs. How is each pair represented?
  2. If color_match is given a list of 9 colors, how many pairs does it produce? Show how you can calculate the answer.
  3. Write a modification of color_match and call it color_display so that it does the following:
    • Omits pairs that have the same color. For example, green green will no longer be a pair in the list.
    • Instead of returning the list, color_display prints each pair by hyphenating the pair. For example, green yellow will be printed as green-yellow.
  4. If color_display is given a list of 9 colors, how many pairs does it produce? Can you provide a general way of calculating that number? For example, if the list has x colors, how many pairs are produced?
  5. Note that color_blend uses index-based loops. Does it show blue-green and green-blue? Given the list of colors, can you always predict the order that the colors in a color pair will appear (e.g. will it be green-blue or blue-green).
  6. Can you predict how many unique pairs are produced from a list of 9? Do you have a general process for calculating that? For example, if the list has x colors, how many pairs are produced?
  7. Optional but required for assignment 7: Write a function called exotic_fruits that takes two parameters: a list of colors (represented as strings, e.g. the colors variable) and a list of fruits (represented as string, e.g. the fruits variable). The function should return a list of all possible exotic fruits that combine a color with a fruit (e.g. "blue melon"). Question: if exotic_fruits is given 5 colors and 4 fruits, how many exotic fruits does it produce? How can you calculate that mathematically?

Deliverable

Create a text file called lab8.txt that contains your answers and requested functions.

At the top of the lab, provide a statement of your experience with the lab and whether you worked with others.

Submit your lab8.txt file to D2L.

Grading

Your lab submission will be graded using the following rubric:

  • + .5 --- Your submission is clearly formatted.
  • + .5 --- Your submission includes a summary statement and includes how you collaborated.
  • + .5 / 1.0 --- You submitted most of the lab (0.5) or you submitted all of the lab (1.0).
  • + .5 --- Your lab submission is generally correct.

Labs submitted late are subject to a half point penalty.