@extends('template')
@section('title')
Lab 6: Part 1 - For Loops & Lists
@stop
@section('head')
@stop
@section('content')
# Lab 6: Part 1 - For Loops & Lists
## Preface: Sample lists
In this first part of today's lab, we'll be writing functions that work with a list of words.
To help with testing your functions, we've included the following files which contain long lists of words:
| File name | Variable(s) created |
|: ---------- | :---------------- |
| `sampleLists.py` | `greetings`, `colors`, `fruit`, etc. |
| `wordLists.py` | `tinyWordList`, `smallWordList` |
| `songLyrics.py` | `beautifulLyricList`, `beforeHeCheatsLyricList`, etc. |
| `vocabulary.py` | `englishwords` |
**Note: DO NOT attempt to open `vocabulary.py` in Thonny (although you're free
to open the other files and look at what's in them).** `vocabulary.py` contains
so many words on a single line of code that Thonny on some computers will slow
down and hang if you try to open it. You may have to force-quite and restart
Thonny if this happens.
Here is a sample of what the contents of `songLyrics.py` looks like:
```py
# CS111 Lab 6
# Sample lists for testing, composed of song lyrics
beautifulLyricList = ["You're", 'insecure', "Don't", 'know', 'what', 'for', ...
beforeHeCheatsLyricList = ['Right', 'now', "he's", 'probably', 'slow', ...
iGetByWithALittleHelpLyricList = ['What', 'would', 'you', 'think', 'if', ...
...
```
To access these lists in your code, add the name of the relevant file at the top of your code, e.g.:
```py
# Import songLyrics for access to beautifulLyricList, beforeHeCheatsLyricList, etc.
from songLyrics import *
```
Note: to use the debugger, you may have to comment out the import for
`vocabulary.py`, because otherwise the debugger will get stuck at this import.
## Preface: Useful Python reminders
### Length
`len()` returns the number of items in a list (or a string), e.g.:
```py
len(["happy","birthday"]) # Results: 2
len("mississippi") # Results: 11
```
{{--
no longer needed with Python 3
### Number type conversion
Python's float method converts integers to floating point numbers, e.g.:
```py
float(21) # Results: 21.0
```
This is useful when you need to divide an integer by an integer and get a decimal result, e.g.
```py
5/4 # Results: 1
float(5)/4 # Results: 1.25
```
--}}
### Add to a list
The append method can be used to add new items to the end of a list, e.g.
```py
myFriends = [] # Start with an empty list
myFriends.append('taylor') # Add taylor
myFriends # Results: ['taylor']
myFriends.append('ed') # Next add ed
myFriends # Results: ['taylor','ed']
pets = ['charlotte','wilbur'] # Start a new list
pets.append('templeton') # Add templeton
print (pets) # Results: ['charlotte','wilbur','templeton']
```
## Your task: List Manipulations
Create a new file called `listPractice1.py` and write functions to answer the following questions.
### 1. wordsStartWithA
Partner A
How many words in a given word list begin with the lowercase letter `a`?
Define a function named `wordsStartWithA` which will return the number of words
from a list of words that start with 'a'. The function should have a single
parameter, which is the list of words to look at.
Hint: Use a variable to keep track of how many words start with `a`,
looking at each word one at a time as you loop through the list of
words. Below is a sample iteration table to help you see how the
function should work with the list
```py
wordsStartWithA(['apple', 'orange', 'apricot','Avocado', 'pear'])
```
####Iteration table
word | count |
N/A | 0 |
'apple' | 1 |
'orange' | 1 |
'apricot' | 2 |
'Avocado' | 2 |
'pear' | 2 |
__Examples:__
```py
from songLyrics import *
from wordLists import *
from vocabulary import *
# For any of the list functions you write today, you may want to start off
# with a very small, hard-coded list where you can easily identify
# the expected outcome, e.g.:
print (wordsStartWithA(['apple', 'orange', 'apricot', 'Avocado', 'pear']))
# Expected outcome: 2
# Then, you can test on the bigger lists described on the top of this page:
print (wordsStartWithA(smallWordList)) # 65
print (wordsStartWithA(loveStoryLyricList)) # 27
print (wordsStartWithA(englishwords)) # 3685
print (wordsStartWithA(beautifulLyricList)) # 17
print (wordsStartWithA(imSoFancyLyricList)) # 17
```
### 2. averageLength
Partner B
What is the average word length of a given list of words?
Here's an example:
```py
miniList = ['apple','pie']
```
The total length of the words in `miniList` is 5 + 3 = 8.
8/2 = 4.0, so the average length of works in `miniList` is 4.0.
[Round](https://docs.python.org/3/library/functions.html#round) your results to 2 digits after the decimal point.
__Examples:__
```py
print (averageLength(imSoFancyLyricList)) # 4.06
print (averageLength(beautifulLyricList)) # 4.03
print (averageLength(beforeHeCheatsLyricList)) # 4.27
print (averageLength(loveStoryLyricList)) # 3.87
print (averageLength(thriftShopLyricList)) # 4.44
print (averageLength(iGetByWithALittleHelpLyricList)) # 3.66
print (averageLength(tinyWordList)) # 5.7
print (averageLength(smallWordList)) # 6.08
print (averageLength(englishwords)) # 8.37
```
### 3. countYou
Which song uses the word "you" the most?
Hint: Make sure you count `"You"` and `"YOU"` and not just `"you"`.
__Examples:__
```py
print (countYou(["you", "probably", "think", "this", "song", "is", "about", "you"])) # 2
print (countYou(imSoFancyLyricList)) # 17
print (countYou(beautifulLyricList)) # 43
print (countYou(beforeHeCheatsLyricList)) # 1
print (countYou(loveStoryLyricList)) # 16
print (countYou(thriftShopLyricList)) # 5
print (countYou(iGetByWithALittleHelpLyricList)) # 10
```
### 4. wordsWithLess
Partner A
Which words in a given word list contain the word `less`?
Hint: `append` is useful here to build a list of words that contain `less` in them. Start with an empty list and append each word that meets the criteria. Then return the list.
**Task 4a:** Fill in this iteration table before writing your function (the first row has been done for you).
```py
>>> wordsWithLess(['careless','apple','lessen','pear','mindless'])
['careless', 'lessen', 'mindless']
```
word | newlist |
N/A | [ ] |
careless | |
apple | |
lessen | |
pear | |
mindless | |
After you and your partner fill in the table, click here.
word | newlist |
N/A | [ ] |
careless | ['careless'] |
apple | ['careless'] |
lessen | ['careless', 'lessen'] |
pear | ['careless', 'lessen'] |
mindless | ['careless', 'lessen','mindless'] |
**Task 4b:** Define the function wordsWithless
(see examples below).
__Examples:__
```py
print (wordsWithLess(['careless', 'careful']))
# Results: ['careless']
print (wordsWithLess(smallWordList))
# Results: ['painless']
print (wordsWithLess(englishwords))
# Results: a list with 401 words
# ['ageless', 'agelessness', 'aimless', ..., 'worthlessness']
print (wordsWithLess(beforeHeCheatsLyricList))
# Results: []
```
### 5. longestWords
Partner B
This function returns a list with the longest word(s) in a song or word list (without repeating words).
__Examples:__
```py
print (longestWords(["apple", "apricot", "pear", "coconut", "orange"]))
# Results: ['apricot', 'coconut']
print (longestWords(imSoFancyLyricList))
# Results: ['department', 'Chandelier', 'expensive,']
print (longestWords(beautifulLyricList))
# Results: ['overwhelmed', 'desperately']
print (longestWords(beforeHeCheatsLyricList))
# Results: ['bleached-blond']
print (longestWords(loveStoryLyricList))
# Results: ['http://www.elyrics.net']
print (longestWords(thriftShopLyricList))
# Results: ['hand-me-downs?']
print (longestWords(iGetByWithALittleHelpLyricList))
# Results: ['Ah-ah-ah-ah-ah-ah-ah-ah-ah']
print (longestWords(englishwords))
# Results: ['electroencephalographs']
```
### 6. [Challenge problem] wordsContainAllChars
Partner A
Which words in the wordlist contain all the characters in a given string (e.g. `love`)?
Assume none of the given strings have duplicate letters (i.e. you won't see a search term like `dud`).
**Task 6a:** Fill in this iteration table before writing your function (the first row has been done for you).
```py
>>> wordsContainAllChars(["ab", "abc", "cba", "abracadabra", "xyz"], "abc")
['abc', 'cba', 'abracadabra']
```
word | keeper |
N/A | [ ] |
ab | |
abc | |
cba | |
abracadabra | |
xyz | |
After you and your partner fill in the table, click here.
word | keeper |
N/A | [ ] |
ab | [ ] |
abc | ['abc'] |
cba | ['abc','cba' ] |
abracadabra | ['abc','cba','abracadabra'] |
xyz | ['abc','cba','abracadabra'] |
**Task 6b:** Define the function wordsContainAllChars
(see examples below).
Hint: First, figure out how to tell if one word contains all the letters of another string. Can you
and your partner define a predicate to do so?
__Examples:__
```py
print (wordsContainAllChars(["ab", "abc", "cba", "abracadabra", "xyz"], "abc"))
# Results: ['abc', 'cba', 'abracadabra']
print(wordsContainAllChars(smallWordList, "pam"))
# Results: ['amorphous', 'atmosphere', 'campanile', 'imperial', 'pam',
# 'pamela', 'polynomial', 'program', 'scamper', 'temptation']
print(wordsContainAllChars(loveStoryLyricList, "love"))
# Results: ['love', 'love', 'love', 'love', 'love', 'love']
print(wordsContainAllChars(thriftShopLyricList, "ough"))
# Results: ['bought', 'bought', 'bought', 'through', 'dough', 'through']
print(wordsContainAllChars(englishwords,"evrngast"))
# Results: ['advertising', 'asseverating', ..., 'vegetarianism', 'vegetarians']
```
## More list manipulations (time permitting)
Create a new file called `listPractice2.py` in your `lab06` folder.
Note that the lab download folder included a file called `sampleLists.py` which contains sample lists you can use for testing. Study this file to see what lists are available, and then add `from sampleLists import * ` to the top of your `listPractice2.py` file so they're available for use in your file.
Using these sample lists, create the following functions.
### 1. filterDrop
Partner B
Write a function called `filterDrop(value, mylist)` that takes a value and a list and returns a new list with all the occurrences of the given value dropped.
__Examples:__
```py
print (filterDrop('James', skyfall))
# Results: ['M', 'Q', 'Moneypenny', 'JamesBond', 'Bond', 'Silva', 'Patrice']
print (filterDrop('Q', skyfall))
# Results: ['M', 'Moneypenny', 'James', 'JamesBond', 'Bond', 'Silva', 'Patrice']
```
### 2. filterLength
Partner A
Write a function called `filterLength` that when given a maxLength and a list of pairs, returns a list of only the pairs that, when summed together, are shorter than the maxLength.
__Examples:__
```py
print (filterLength(10, roster))
# Results: [['Yoyo','Ma'],['Sohie','Lee'],['Jean','Herbst'],['Santa','Claus']]
print (filterLength(12, roster))
# Results: [['Yoyo','Ma'],['Sohie','Lee'],['Jean','Herbst'],['Brian','Tjaden'],['Santa','Claus'],['Happy','Camper'],['Harry','Styles'],['Taylor','Swift']]
```
### 3. mapPluralize
Partner B
Write a function called `mapPluralize(mylist)` that takes a list of words and returns a new list with the plurals of each of the words.
The nouns are pluralized by adding the suffix `s`, e.g. the plural of `bagel` is `bagels`. You do not have to handle special cases like `kiss` -> `kisses`.
__Examples:__
```py
print (mapPluralize(['assignment']))
# Results: ['assignments']
print (mapPluralize(['donut', 'muffin', 'bagel']))
# Results: ['donuts', 'muffins', 'bagels']
print (mapPluralize(['tree' ,'witch' , 'kiss', 'moose', 'alpaca']))
# Results: ['trees', 'witchs', 'kisss', 'mooses', 'alpacas']
```
@include('/labs/lab06/_toc')
@stop