CPSC120B
Fundamentals of Computer Science I

Lab 16

Chained Conditionals

Practice 1

Write the function rps_string(integer) that returns the string 'rock', 'paper', 'scissors', or 'error' depending on the value of the specified integer. If integer is 0, the function should return 'rock'. If integer is 1, the function should return 'paper'. If integer is 2, the function should return 'scissors'. If integer is not 0, 1, or 2, the function should return 'error'. Use chained conditionals to simplify the code.


Practice 2

Write the function normalize(integer) that returns the normalizes and clamps the specified integer. If integer is less than 0, the function should return 0.0. If integer is greater than 100, the function should return 1.0. If integer is in the range [0, 100], the function should return integer / 100.0. Use chained conditionals to simplify the code.


Grade Conversion

You would think students would be used to the letter grade system. However, I still have students ask how their letter grade is relates to their numeric grade. Let's write a program to help these students, applying what you just learned about conditionals.

Details

In a file called grades.py, write a function called convert_grade(numeric). This function takes an integer which should be greater than 0, and represents some students' grades. Your function should return a string with a single character, either 'A', 'B', 'C', 'D', or 'F'. Use the following scale for your conversions. Do not use any logical operators for this exercise.

A
\([90, 100]\)
B
\([80, 90)\)
C
\([70, 80)\)
D
\([60, 70)\)
F
\([0, 60)\)

Note that square brackets mean inclusive, and parenthesis means exclusive.

Make sure to test your function by calling the function multiple times with different parameters. Make sure your code follows the course's code conventions.

Test Cases

Function Parameters Expected Output
95 A
90 A
89 B
80 B
79 C
70 C
69 D
60 D
59 F

Hint

Make sure you pay attention to order. If you chose your order carefully (such as starting at the F range) you can simplify your conditions greatly.

Syntatically we don't care about having multiple returns in a function. However, stylistically we want to limit the number of return statements. For the time being, only use one return statement. To do so, store what you are returning in a variable, that you create at the beginning of the function.

Challenge

Most users like more levels of specification. For example, on the Roanoke College campus we use the +/- system as well. Add code to your function so that it can also output A+'s and D-'s. Recall that there is no such thing as an F+ or F-.

For all levels (except for F), you get a + if the 1's place is 7 or greater. You get a - if the 1's place is 3 or less.

Leap Years

One of the most confusing aspects of validating dates is accounting for the number of leap years in that period. Every year, there is roughly an extra \(\frac{1}{4}\) day. To account for this, almost every 4 years we add an additional day to the month of Februray. The rules for this are straight-forward, but the reasoning and implementation of them is sometimes confusing.

Details

In a file called date_validation.py, write a set of test cases to ensure your is_leap_year(year) function operates under the following specification.

A leap year occurs almost every 4 years. All leap years are divisible by 4, but not all years divisible by 4 are leap years. A year is a leap year if it is divisible by 4, unless it is divisible by 100 and not divisible by 400. Your function should return True if the year parameter is a leap year, and False otherwise.

Sample Test Cases

Function Parameters Expected Output
1900 False
2000 True
1996 True
2015 False

Hint

  • You can use the % (mod) operator to test divisibility. If year % 4 is 0, then year is divisible by 4.

  • Dont forget that you can use your logical constructs to reduce the complexity of the condition checks.

Challenge

Another approach to testing is exhaustive enumeration. There have been 2016 years since the beginning of the common era. Write code that checks all of the years since 0 A.D. Compute the total number of leap years in that time frame.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.