BYU logo Computer Science

Lab 10 — Lists

In this lab you will practice using lists. Download the lab 10 files to get started. Write your code in the file called practice_with_lists.py.

Basics

  • create a variable called foods that has a list of your favorite foods
  • create a variable called primes that has the prime numbers from 1 to 20

Print out each list.

UP and down

Write a function called up_and_down(l) that loops over the list l and prints the itmes out, starting with uppercase for the first one, lowercase for the second one, and then alternating cases. For example:

>>> up_and_down(['wow','this','is','incredible'])
['WOW','this','IS','incredible']

Write three doctests for this function.

Sleeping in

Write a function called sleeping_in(days) that takes a list of days, loops over them, and prints “It’s [day of the week], wake up early!” if it is a weekday and “It’s [day of the week], sleep in!” if it is a weekend.

For example:

>>> sleeping_in(['Monday','Saturday','Tuesday'])
"It's Monday, wake up early!"
"It's Saturday, sleep in!"
"It's Tuesday, wake up early!"

Write three doctests for this function.

Read a list

Write a function called read_a_list(filename). This function takes one parameter:

  • filename: the name of a file

The file contains a list of words, one per line. The function returns a list of all of the words in the file. For example, if a file called fruits.txt contains:

banana
apple
kiwi
pineapple
mango
apple

then:

>>> read_a_list('fruits.txt')
['banana', 'apple', 'kiwi', 'pineapple', 'mango', 'apple']

Create three files and then write a doctest for each file.

Customer spending

Write a function called customer_spending(customers, amounts). This function takes two arguments:

  • customers: a list of customers, as strings
  • amounts: a list of amounts spent, as decimal numbers

The function loops through both lists, printing how much each customer spent. For example:

>>> customers = ['Emma', 'Ammon', 'Malala', 'Amir']
>>> amounts = [50.21, 10.52, 153.77, 78.12]
>>> customer_spending(customers, amounts)
Emma spent $50.21.
Ammon spent $10.52.
Malala spent $153.77.
Amir spent $78.12.

Write three doctests for this function.

Class days

Write a function called class_days(days, classes). This function takes two arguments:

  • days: a list of days of the week, as strings
  • classes: a list of boolean (True, False)

The function loops through both lists, printing out “I have class because it is [day of the week].” for every day where classes has ‘True’ listed. Assume both lists have the same number of items. For example:

>>> days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
>>> classes = [True, False, True, True, False, False, False]
>>> class_days(days, classes)
"I have class because it is Monday.
I have class because it is Wednesday.
I have class because it is Thursday.

For this problem, you will want to loop through the list of days using for i in range(len(days)). Then you can use the index i to access both lists.

Write three doctests for this function.

Convert days

Write a function called convert_days(words). This function takes one parameter:

  • words: a list of words

It also uses two constants:

SHORT_DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
LONG_DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

The function returns a new list that has all the same words, but if any of the words in SHORT_DAYS appears, it replaces it with the corresponding word in LONG_DAYS. For example:

>>> convert_days(['I','have','a','meeting','on','Mon','and','Thu','this','week'])
['I','have','a','meeting','on','Monday','and','Thursday','this','week']

Hints:

  • If you are looking at a given word, what can you use to check whether that word is in the list of SHORT_DAYS?
  • If you know a word is in SHORT_DAYS, how can you find the corresponding word in LONG_DAYS?

Write three doctests for this function.

Interleave

Write a function called interleave(list1, list2). This function takes two parameters:

  • list1: a list
  • list2: a list

Both lists have the same length. The function returns a new list that alternates items from list1 and list2, starting with list1. For example:

>>> interleave(['banana', 'apple', 'kiwi', 'pineapple', 'mango'],['cup', 'plate', 'bowl', 'fork', 'spoon'])
['banana', 'cup', 'apple', 'plate', 'kiwi', 'bowl', 'pineapple', 'fork', 'mango', 'spoon']

Write three doctests for this function.

Substitution

Write a function, substitution(filename1, filename2, filename3). This function takes three parameters:

  • filename1: a file name
  • filename2: a file name
  • filename3: a file name

The function first reads all three files into three different lists. Use the function read_a_list() that you wrote, above. The function then loops through the first list, looking for any words that appear in the second list. If it finds a word in the second list, it subsitutes it with the word in the third list. It returns a new list. For example, if you have a file called fruits.txt:

banana
apple
kiwi
pineapple
mango
apple

and a file called find_these.txt:

apple
pineapple

and a file called use_these_instead.txt:

pear
lemon

then:

>>> substitution_game('fruits.txt', 'find_these.txt', 'use_these_instead.txt')
['banana', 'pear', 'kiwi', 'lemon', 'mango', 'pear']

Write three doctests for this function.

Lessons

What we want you to get from this lab:

  • You can use lists in a variety of ways.

  • You are more comfortable parsing the lines in a file, one line at a time

  • You can clearly document your functions with docstrings

  • You can test you functions with doctests

  • You can figure out what went wrong when something unexpected happens

  • Hopefully you had fun!

Points

Turn in a zip file that has your code.

Every function should have a docstring and doctests.

TaskDescriptionPoints
BasicsYour solution works2
UP and downYour solution works2
Sleeping inYour solution works2
Read a listYour solution works2
Customer spendingYour solution works2
Class daysYour solution works2
Convert daysYour solution works2
InterleaveYour solution works2
SubstitutionYour solution works2
DocumentationAll functions have good docstrings2
DoctestsAll functions have doctests2