As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab17 $ cd lab17
Write a function sum_powers(n)
, which
computes the sum of the numbers in the set:
Write a function called compute_product(n)
, which
computes the series:
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.
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.
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.
Function Parameters | Expected Output |
---|---|
2, 29, 2004 | True |
2, 29, 2015 | False |
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.
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]\).