CPSC150A
Scientific Computing

Activity 8

Conditionals

Single Digit

Write the function single_digit(integer), that returns True if the parameter integer is a positive, single digit integer and False otherwise.

Example

Test Code Output
print(single_digit(0)) True
print(single_digit(9)) True
print(single_digit(10)) False
print(single_digit(-1)) False

Rock, Paper, Scissors

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’.

Example

Test Code Output
print(rps_string(0)) rock
print(rps_string(1)) paper
print(rps_string(2)) scissors
print(rps_string(3)) error
print(rps_string(-1)) error

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 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.

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.

Details

Write the function is_leap(year). The function should return True if the year parameter is a leap year, and False otherwise.

Example

Test Code Output
print(is_leap(1900)) False
print(is_leap(2000)) True
print(is_leap(1996)) True
print(is_leap(2015)) False
print(is_leap(2016)) True

Hint

  1. You can use the mode, %, operator to test divisibility. If year % 4 is 0, then year is divisible by 4.

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

Submission

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