Review of Weeks 1–8¶
This lab includes one exercise for each week of the semester leading up to Fall Break. Use it as an opportunity to review and reinforce what you’ve learned so far.
Instructions
- Unzip the provided review1.zip in your
CS149folder. - Work together to complete the review exercises below.
- The bullet points provide a few hints; not all are needed.
- After each exercise, run the corresponding test function.
Tip
You may complete these exercises in any order.
The first three weeks might seem difficult, because those topics are from so long ago.
Week 1: Statements¶
print()withsep=andend=input()with/without prompt- escape sequences (Ex:
\n)
Exercise 1 (week1.py)
On Canvas, the User Settings page shows both
a Display Name (Ex: Ada Lovelace) and a Sortable Name (Ex: Lovelace, Ada).
Write a program that inputs first and last names, and then prints the corresponding Display Name and Sortable Name, separated by a slash:
First name: Ada
Last name: Lovelace
Ada Lovelace / Lovelace, Ada
Your program may not use f-strings, may not use the + operator,
and must have two print statements: one for Display Name, and one for Sortable Name.
Week 2: Expressions¶
- f-strings, format specs
- arithmetic operators
- The
mathmodule
Exercise 2 (week2.py)
Write a program that inputs \(a\), \(b\), and \(c\) as floats and solves the equation \(ax^2 + bx + c = 0\) using the quadratic formula:
The program should display no prompts and output the two results on separate lines. Each result should be formatted to three decimal places as shown:
1
4
-21
3.000
-7.000
Week 3: Branches¶
if,elif,elseand,or,not- operator precedence
Exercise 3 (week3.py)
Interstate highways in the United States use a numbering system based on their direction. North-south routes are assigned odd numbers, and east-west routes are assigned even numbers.
Write a program that inputs an integer between 1 and 99. No prompt should be displayed. You do not have to check if the input is within range. The program should output one line, using only one print statement. The possible outputs are:
| Input | Output |
|---|---|
| even integer not a multiple of 5 | east-west |
| even integer and a multiple of 5 | east-west coast-to-coast |
| odd integer not a multiple of 5 | north-south |
| odd integer and a multiple of 5 | north-south border-to-border |
Your code may not use [] to index the input like a string.
Your code must use nested if statements—the and operator is not allowed.
The elif keyword is also not allowed.
Week 4: Functions¶
def,return,None- global vs local scope
- docstrings and
help()
Exercise 4 (week4.py)
Rewrite your solution to Exercise 2 as a function:
- Define a function named
quadraticthat takes three floats as parameters.- You don't need to read the floats using
input(), because the floats are passed to the function.
- You don't need to read the floats using
- Apply the quadratic formula, and return the results as a tuple of two floats.
- You don't need to display the results using
print(), because the results are returned from the function. - You don't need to format the results to three decimal places, because the results aren't being printed.
- You don't need to display the results using
For example, quadratic(1, 4, -21) should return (3, -7).
Week 5: Containers¶
list,tuple,set,dictin,not in,is,is notmin(),max(),sum()
Exercise 5 (week5.py)
Implement the following functions with one line of code each.
Your code may not use any if statements or for/while loops.
def is_valid(color):
"""Determine whether a tuple is a valid RGB color.
Args:
color (tuple): 3 integers that represent (red, green, blue)
Returns:
bool: True when each integer is between 0 and 255 (inclusive)
"""
min() and max()
def is_unique(numbers):
"""Determine whether all the numbers in a list are unique.
Args:
numbers (list): integers to consider (can be any length)
Returns:
bool: True when the numbers list contains no duplicate values
"""
len() and set()
Week 6: While Loops¶
whileconditionbreakandcontinue- The
randommodule
Exercise 6 (week6.py)
Write a program (not a function) that reads input from the user, one line at a time, until the user inputs STOP.
No prompts should be displayed.
Whenever the user inputs a line that contains super, the program should output duper!.
For example (user input in bold font):
Hello, everyone.
I hope you have a super day.
duper!
Best regards,
–superman
duper!
STOP
Week 7: For Loops¶
forvariableiniterablerangewith 1, 2, or 3 valuesenumerate()– index, value
Exercise 7 (week7.py)
Implement the following function:
def count_words(text, magic):
"""Count the number of magic words in a sentence.
Args:
text (str): a sentence (all lowercase, no punctuation)
magic (set): words that are known to have magical power
Returns:
int: how many words in text are in the magic word set
"""
str.split() method.
Week 8: Unit Testing¶
frommoduleimportfunctionif __name__ == "__main__"- The Python Standard Library
Exercise 8 (test_review.py)
As a team, discuss the provided test code.
- The tests for weeks 1–3 and 6 use
run_module()to capture output printed to the screen. - The tests for weeks 4–5 and 7 import the module and call the functions directly to get the result.
Answer the following questions as a team:
- How is testing programs that print output different from testing functions?
- What techniques demonstrated in
test_review.pycan be applied to PA1?
Submission¶
Upload week1.py to week7.py to Gradescope (all 7 files at the same time).