Processing math: 100%

CPSC120A
Fundamentals of Computer Science

Lab 14

Chained Conditionals

Normalize

Create the function normalize(number) that normalizes a number in the range [0,100]. If number is less than 0, the function should return 0.0. If number is greater than 100, the function should return 1.0. If number is in the range [0,100], the function should return number / 100. Use chained conditionals to simplify the code.

Test Cases

print('Input: ?\tActual:', normalize(?), '\tExpected: ?')
    

Grade Conversion

Create the function convert_grade(numeric_grade) that converts numeric grades to letter grades. The function has one parameter numeric_grade that is a number in the range [0,100] that represents a student's grade. The function should return a string the equivalent letter grade as a string, either 'A', 'B', 'C', 'D', or 'F'. Do not use any logical operators for this exercise Use the following scale for your conversions.

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

Test Cases

print('Input: ?\tActual:', convert_grade(?), '\tExpected: ?')
    

Leap Year

The reason we have leap years added to our calendar is because they do not take into account the fact that the Earth actually takes (about) 365.25 days to revolve once around the sun. While it sounds like this should be an easy thing to account for, there's actually some fairly complex math involved in dealing with this inconsistency.

Details

Write the Python function is_leap_year(year) that determines whether a year is a leap year. The function should return True if year is a leap year and False otherwise.

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.

Test Cases

print('Input: ?\tActual:', is_leap_year(?), '\tExpected: ?')
    
  • 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 2017 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.


Date Validation

Writing programs with dates and time is notoriously hard. We also want to create programs that don't crash. So we need to create programs that verify if the user enters a valid date. For example what happens when a user enters a birthday of January 32nd?

Details

Write the Python function is_valid_date(day, month, year) that determines whether a date is valid. The parameters day, month, and year are all integers that specify a date. For example the day February 5th 2015 is day 5, month 2, and year 2015. The function should return True if the date is valid.

Test Cases

print('Input: ?, ?, ?\tActual:', is_valid_date(?, ?, ?), '\tExpected: ?')
    
  • Validating a date can be broken into three steps, validate the day, validate the month, and validate the year. If all three are valid, the function should return True.

  • Validating the year and month can be done by testing if the year and month are inside of valid ranges. The year should be positive and the month should be between 1 and 12.

  • Validating the day is also testing if it is in a valid range, but the range depends on the month. To make testing the day range easier write an addition function, days_in_month(month, year) that returns the number of days in a month. Remember:

    30 days hath September, April, June, and November
    All the rest have 31
    Except for February, the only one.

    February has either 28 or 29 days depending on whether it is a leap year. Hint: use the is_leap_year function you wrote to help with this.

    Test this function before using it in the is_valid_date function.

Challenge

Write an additional function validate_date_time(month, day, year, hours, minutes). The function should validate the date and the time specified. Assume the time is specified as two integers, and their values should be in the ranges [0,12] and [0,60].