Coding Challenges: Modules & Packages
8 HackerRank-style challenges, auto-graded right in your browser —
no setup required. Write your function in the code box, then click
Run Tests to check it against a set of test cases
(including edge cases like 0 and leap years). Stuck?
Every challenge has a Show Solution reveal at the
bottom. Only uses what Chapters 1-11 have covered so far —
def, everything through decorators, and the
math and datetime modules. No
try/except, file I/O, classes, or any other
import.
math and datetime, but
random's whole point is that its output is different
every time — so it's saved for the practice bank instead, where
you check a property of the result instead of an exact match. Dates
below are always passed in and returned as plain strings (like
"2026-07-05"), since that's the only date format that
survives being sent as JSON test data.
1 Hypotenuse Easy
Write a function hypotenuse(a, b) that returns the length of the hypotenuse of a right triangle with legs a and b, using math.sqrt, rounded to 2 decimal places.
a and b are non-negative numbers; round the final result with round(..., 2).
hypotenuse(3, 4) → 5.0hypotenuse(5, 12) → 13.0hypotenuse(0, 5) → 5.0
Show Solution
import math
def hypotenuse(a, b):
return round(math.sqrt(a ** 2 + b ** 2), 2)
2 Round Up to the Nearest Multiple Easy
Write a function round_up_to_multiple(n, multiple) that rounds n up to the nearest multiple of multiple, using math.ceil(n / multiple) * multiple — the same "how many boxes do I need" pattern from a warehouse packing problem.
n may be 0 or already an exact multiple (in which case it stays the same).
round_up_to_multiple(23, 5) → 25round_up_to_multiple(20, 5) → 20round_up_to_multiple(0, 5) → 0
Show Solution
import math
def round_up_to_multiple(n, multiple):
return math.ceil(n / multiple) * multiple
3 Circle Area Easy
Write a function circle_area(radius) that returns the area of a circle (math.pi * radius ** 2), rounded to 2 decimal places.
radius may be 0; round the final result with round(..., 2).
circle_area(1) → 3.14circle_area(2) → 12.57circle_area(0) → 0.0
Show Solution
import math
def circle_area(radius):
return round(math.pi * radius ** 2, 2)
4 Rounded Power Easy
Write a function power_rounded(base, exponent) that returns base raised to exponent using math.pow, rounded to 2 decimal places.
exponent may be 0 (any base to the 0th power is 1.0); base/exponent may be a float.
power_rounded(2, 10) → 1024.0power_rounded(2, 0) → 1.0power_rounded(1.5, 2) → 2.25
Show Solution
import math
def power_rounded(base, exponent):
return round(math.pow(base, exponent), 2)
5 Days Between Two Dates Medium
Write a function days_between_iso(date_a, date_b) that takes two ISO-formatted date strings (like "2026-07-05"), converts each to a datetime.date with datetime.date.fromisoformat(...), and returns the number of days from date_a to date_b as an integer (using date subtraction and .days). Dates are passed and returned as plain strings so the test cases can be written as ordinary JSON.
date_b may come before date_a (return a negative number); the two dates may be equal (return 0); test cases include a leap year.
days_between_iso("2026-01-01", "2026-07-05") → 185days_between_iso("2026-01-01", "2026-01-01") → 0days_between_iso("2026-07-05", "2026-01-01") → -185
Show Solution
import datetime
def days_between_iso(date_a, date_b):
a = datetime.date.fromisoformat(date_a)
b = datetime.date.fromisoformat(date_b)
return (b - a).days
6 Day of the Week Medium
Write a function day_of_week_name(year, month, day) that builds a datetime.date(year, month, day) and returns its weekday name as a string (like "Sunday"), using .strftime("%A"). The date is passed in as three separate integers (year, month, day) since that's a JSON-safe way to represent a date without needing a special date type in the test data.
year/month/day always form a valid calendar date.
day_of_week_name(2026, 7, 5) → "Sunday"day_of_week_name(2026, 1, 1) → "Thursday"day_of_week_name(2000, 1, 1) → "Saturday"
Show Solution
import datetime
def day_of_week_name(year, month, day):
d = datetime.date(year, month, day)
return d.strftime("%A")
7 Add Days to a Date Medium
Write a function add_days_iso(date_str, days) that takes an ISO date string and an integer number of days, and returns a new ISO date string that many days later (or earlier, if days is negative). Convert with datetime.date.fromisoformat(...), add a datetime.timedelta(days=days), then convert back to a string with .isoformat() so the result stays JSON-safe.
days may be 0 or negative; results may cross a month or year boundary.
add_days_iso("2026-01-01", 30) → "2026-01-31"add_days_iso("2026-01-01", 0) → "2026-01-01"add_days_iso("2026-01-01", 365) → "2027-01-01"
Show Solution
import datetime
def add_days_iso(date_str, days):
d = datetime.date.fromisoformat(date_str)
result = d + datetime.timedelta(days=days)
return result.isoformat()
8 Age in Full Years Medium
Write a function age_in_years(birth_date_str, on_date_str) that takes two ISO date strings and returns how many full years old someone born on birth_date_str would be on on_date_str — the same logic a real signup form uses to check a minimum age. Compute the plain year difference first, then subtract 1 if the birthday hasn't happened yet that year (compare (month, day) tuples).
on_date_str is always on or after birth_date_str; test cases include a birthday that hasn't happened yet in the target year, and a Feb 29 birth date.
age_in_years("2000-07-05", "2026-07-05") → 26age_in_years("2000-07-06", "2026-07-05") → 25age_in_years("2000-01-01", "2026-12-31") → 26
Show Solution
import datetime
def age_in_years(birth_date_str, on_date_str):
birth = datetime.date.fromisoformat(birth_date_str)
on_date = datetime.date.fromisoformat(on_date_str)
years = on_date.year - birth.year
if (on_date.month, on_date.day) < (birth.month, birth.day):
years -= 1
return years