CS21 Lab 3: if/else

Due Saturday, February 17, before midnight


Make sure all programs are saved to your cs21/labs/03 directory. Files outside this directory will not be graded.

$ update21
$ cd ~/cs21/labs/03/

Programming Tips

Topics for this assignment


1. Leap year

A year is a leap year if it is divisible by 4, but not divisible by 100. However, a year that is divisible by 400 is a leap year. For details on why leap years have these strange 100 and 400 year exceptions, see this description of howstuffworks.

Write a program leapyear.py that prompts the user for a year and then prints whether that year is a leap year. Here are some examples of how the program should work:

$ python3 leapyear.py
Enter a year: 2000
2000 is a leap year

$ python3 leapyear.py
Enter a year: 1900
1900 is NOT a leap year

$ python3 leapyear.py
Enter a year: 1984
1984 is a leap year

$ python3 leapyear.py
Enter a year: 2018
2018 is NOT a leap year

Hint: You can use python's mod operator (%) to determine whether one number is evenly divisible by another number.


2. Leet Speak

Leet speak is method of modifying text by replacing some letters with other symbols (such as numbers). We will change every 'e' and 'E' to '3', every 'l' and 'L' to '1', and every 's' and 'S' to '5'.

Write a program, leetspeak.py, that takes a string as input and prints out the leetspeak version of the string.

$ python3 leetspeak.py
Enter string: We love Computer Science
Leet version: W3 1ov3 Comput3r 5ci3nc3

$ python3 leetspeak.py
Enter string: Swarthmore College
Leet version: 5warthmor3 Co113g3

$ python3 leetspeak.py
Enter string: ninja
Leet version: ninja

3. Speeding

Write a program, speeding.py, that requests the speed limit and clocked speed of a driver and then determines whether the driver should be issued a ticket and fine based on the following table.

MPH over limit Result Fine
1 to 9 warning $0
10 to 19 ticket $50
20 to 29 ticket $75
30 and higher ticket $100

Here are some examples of how your program should work.

$ python3 speeding.py
Enter speed limit: 55
Enter clocked speed: 45
Driver's speed is within the legal limit.

$ python3 speeding.py
Enter speed limit: 55
Enter clocked speed: 62
Driver is speeding! Clocked at 7 mph over the limit.
Issue a warning.

$ python3 speeding.py
Enter speed limit: 55
Enter clocked speed: 85
Driver is speeding! Clocked at 30 mph over the limit.
Issue a ticket with a $100 fine.

4. Factorization

Write a program, factorization.py, that finds all the factors of a given integer. For example, the number 12 factors into 1x12, 2x6, and 3x4. Prime numbers, like 13, have no factors other than 1 and themselves.

Here are some examples of how your program should work.

$ python3 factorization.py
Enter a number to factor: 30
1 x 30 = 30
2 x 15 = 30
3 x 10 = 30
5 x 6 = 30

$ python3 factorization.py
Enter a number to factor: 17
1 x 17 = 17
17 is a prime number

$ python3 factorization.py
Enter a number to factor: 16
1 x 16 = 16
2 x 8 = 16
4 x 4 = 16

Notice that the program does not repeat reversed factorizations. For example when factoring 30, it shows 5 x 6, but not 6 x 5. When you look for factors, you need only check for them up to the square root of the original number. Anything beyond that point would be repeating already discovered factors.

The sqrt() function in the math library can be used the calculate the square root of a number. Recall that you'll need to use an import statement at the top of your program to bring in the math library.


5. Answer the Questionnaire

Each lab has a short questionnaire at the end. Please edit the QUESTIONS-03.txt file in your cs21/labs/03 directory and answer the questions in that file.


Turning in Your Labs

Don't forget to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.