Grade Conversion
Create the function convert_grade(numeric_grade: float) ->
str
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) |
Valid inputs to the function are in the range of [0, 100]. The function should use an assert statement produce an error the input is outside of this range.
Test Cases
import test def convert_grade(numeric_grade: float) -> str: # Put your code here def main() -> None: test.equal(convert_grade(?), ?) # Put more test cases here return None main()
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: int) ->
bool
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.
Another way to put this is the three rules of leap years:
- Every year that is divisible by 400 is a leap year.
- Every year that is divisible by 100 is not a leap year, except for those years covered by the above rule.
- Every year this is divisible by 4 is a leap year, except for those years covered by the above rules.
- Every year is not a leap year, except for those years covered by the above rules.
Negative years don't exist in the Gregorian calendar, so the function should use an assert statement to produce an error if the input year is negative.
Test Cases
import test def is_leap_year(year: int) -> bool: # Put your code here def main() -> None: test.equal(is_leap_year(?), ?) # Put more test cases here return None main()
-
You can use the
%
(mod) operator to test divisibility. Ifyear % 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 2019 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: int, month: int,
year: int) -> bool
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 2036. The function
should return True
if the date is valid
and False
otherwise.
Test Cases
import test def is_valid_date(day: int, month: int, year: int) -> bool: # Put your code here def main() -> None: test.equal(is_valid_date(?, ?, ?), ?) # Put more test cases here return None main()
-
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: int, year: int) -> int
that returns the number of days in a month. Remember:30 days hath September, April, June, and November
February has either 28 or 29 days depending on whether it is a leap year. Hint: use the
All the rest have 31
Except for February, the only one.is_leap_year
function you wrote to help with this. Test this function before using it in theis_valid_date
function.
Challenge
Write an additional function validate_date_time(month: int,
day: int, year: int, hours: int, minutes: int) -> bool
. 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].