CPSC120B
Fundamentals of Computer Science I

Lab 17

Review

Practice 1

Create a function area_of_hexagon(side_length), which takes an integer representing the length of a single side of a hexagon as a parameter. Your function should return the area of a regular hexagon with the specified side length.

If a is the length of a side of a hexagon, the area can be computed using the equation:

\[ area = \frac{3 \times \sqrt{3}}{2} \times a^2 \]


Practice 2

Write a function called clamp_even(n, minimum, maximum), which returns the value of n "clamped" to the range \([minimum, maximum]\) only if n is even. If n is odd,it should return n. Clamping is a process where values less than the specified minimum get set to the minimum, and values greater than the maximum get set to the maximum. Any value in the range stays the same.


Practice 3

Write a function is_prime(x), which takes an integer x as a parameter and returns True if x is prime. It should return False in all other cases.

Recall that a number is not prime if it is divisible by any integer in the range \([2, x)\).


Date Validation

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. This causes havoc when trying to validate a date, but we can simplify things by adding functions and using our conditionals wisely.

Details

Copy for file called date_validation.py from the last lab.

$ cd ~/cs120/labs/lab17
$ cp ../lab16/date_validation.py .

In this, write an additional function days_in_month(month, year), which takes a integer parameter in the range \([0, 12)\) which specifies the month, and an integer that specifies the year. Your function should return the number of days in the month. Recall:

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

Test this function before you go on.

Once you have tested your days_in_month function, you can write the validate_date(month, day, year) function. This function takes 3 integers: The first specifies the month, the second specifies the day, and the final specifies the year. This function should return True if the date specified is valid, and False otherwise.

Make sure you test this function with various parameters before you have it checked. Make sure you also follow all of the course's style guidelines.

Sample Test Cases

Function Parameters Expected Output
2, 29, 2004 True
2, 29, 2015 False

Hint

  • You need to make sure that you don't send any parameters to days_in_month function that are outside of the ranges. If you operate under this assumption, you can write this function using only 4 conditionals: one for months with 31 days, one for months with 30 days, one for February non-leap year (28), and one for February leap years (29). You will need to use your is_leap_year function here.

  • Validate date needs to check to make sure the month is a valid integer for a month and the day specified is within the ranges for the month they specified. If both of these conditions are satisfied, then the date is valid.

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

Challenge

Write an additional function called validate_date_time(month, day, year, hours, minutes). This 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]\).

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.