Sharing notes from my ongoing learning journey — what I build, break and understand along the way.
Python Practice Exercises – Part 2: Logic, Validation, and Number Systems
Python Practice Exercises – Part 2
In this week’s exercises, we focus on logical problem-solving with loops, conditionals, mathematical operations, and input validation.
Each task builds your understanding of how to combine basic programming concepts to solve real-world problems efficiently.
Try solving each problem first — then scroll down to see the solutions at the end.
LEVEL 1 – Logical & Mathematical Problems
1- Factorial – Largest Number Below 1,000,000,000
Write a script that determines the largest number n such that its factorial n! is still below 1,000,000,000.
Hint:
The factorial of 5 (short notation: 5!) is1 * 2 * 3 * 4 * 5 = 120
2- Valid Date Check
The Meyer GmbH needs a module that checks whether a given date is valid.
If the date is correct, the variable datok should be set to 1, otherwise to 0.
Examples:
29.02.1999 → datok: 0
29.02.2000 → datok: 1
13.05.2000 → datok: 1
32.05.2000 → datok: 0
24.13.2000 → datok: 0
For the year, the following must hold:year > 1900 AND year < 2100
3- Powers of Two
Write a program that prints all powers of 2 whose result is less than 10,000.
Hint: use a loop and the ** operator.
Bonus: Format your output as follows:
2 ** 0 = 1
2 ** 1 = 2
2 ** 2 = 4
2 ** 3 = 8
2 ** 4 = 16
...
4- Multiplication – Factor Pairs
Write a program that determines how many integer multiplier–multiplicand combinations exist for the product 8,420,000,
where both the multiplier and multiplicand are less than 10,000.
1000 * 8420 and 8420 * 1000 count only once.
5- Roman Numerals → Decimal Conversion
Write a Python program that converts a Roman numeral into its decimal (integer) equivalent.
The program should correctly handle subtractive notation such as:IV = 4, IX = 9, XL = 40, XC = 90, CM = 900, etc.
Solutions
1- Factorial – Largest Number Below 1,000,000,000
limit = 1_000_000_000
result = 1
n = 1
while True:
result *= n
if result >= limit:
n -= 1
break
n += 1
print(f"The largest n such that n! < {limit} is: {n}")
2- Valid Date Check
date_str = input("Enter a date in the format DD.MM.YYYY: ")
datok = 0 # 1 = valid, 0 = invalid
try:
day_str, month_str, year_str = date_str.split(".")
day = int(day_str)
month = int(month_str)
year = int(year_str)
except ValueError:
print(f"{date_str} datok: 0 (invalid format)")
else:
def is_valid_date(day, month, year):
if not (1900 < year < 2100):
return False
if not (1 <= month <= 12):
return False
# Leap year check
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
feb_days = 29
else:
feb_days = 28
days = [0, 31, feb_days, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
return 1 <= day <= days[month]
if is_valid_date(day, month, year):
datok = 1
print(f"{date_str} datok: {datok}")
3- Powers of Two
limit = 10_000
exp = 0
while True:
result = 2 ** exp
if result >= limit:
break
print(f"2 ** {exp} = {result}")
exp += 1
4- Multiplication – Factor Pairs
product = 8_420_000
max_value = 10_000
count = 0
for a in range(1, max_value):
if product % a == 0:
b = product // a
if b < max_value and a <= b:
count += 1
print(f"a: {a}, b: {b}, check: {a*b}")
print("Number of valid (a, b) pairs:", count)
5- Roman Numerals → Decimal
roman_values = {
"I": 1, "V": 5, "X": 10, "L": 50,
"C": 100, "D": 500, "M": 1000
}
roman = input("Enter a Roman numeral: ").upper()
total = 0
length = len(roman)
for i in range(length):
if roman[i] not in roman_values:
raise ValueError(f"Invalid Roman numeral: {roman[i]}")
for i in range(length):
value = roman_values[roman[i]]
if i + 1 < length:
next_value = roman_values[roman[i + 1]]
if value < next_value:
total -= value
else:
total += value
else:
total += value
print(f"The value of {roman} is {total}")
