S1Q1 · Check If Even Two-Digit Number¶
⚡ Quick Reference
Function: is_even_two_digit_number(num: int) -> bool
Core idea: check two conditions - exactly two digits (ignoring sign) AND even.
Key rules:
- Two-digit: 10 ≤ abs(num) ≤ 99
- Even: num % 2 == 0
- Negative numbers like -36 count as two-digit (ignore the sign)
- Both conditions must hold
Problem Statement¶
Problem
Write a function is_even_two_digit_number(num) that returns True if num is both a two-digit number (ignoring sign) and even.
Examples:
24
True
-36
True
5
False
101
False
-19
False
Tracing all examples¶
num |
abs(num) |
Two-digit? | Even? | Result |
|---|---|---|---|---|
24 |
24 | 10≤24≤99 ✅ | 24%2=0 ✅ | True |
-36 |
36 | 10≤36≤99 ✅ | -36%2=0 ✅ | True |
5 |
5 | 5<10 ❌ | - | False |
101 |
101 | 101>99 ❌ | - | False |
-19 |
19 | 10≤19≤99 ✅ | -19%2≠0 ❌ | False |
42 |
42 | ✅ | ✅ | True |
99 |
99 | ✅ | 99%2≠0 ❌ | False |
-48 |
48 | ✅ | ✅ | True |
Solution approaches¶
Chained comparison 10 <= abs(num) <= 99 checks both bounds at once. Short-circuit and skips the even check if the number isn't two digits.
Key takeaways¶
abs() handles the sign
abs(num) strips the negative sign before checking the digit range. -36 has two digits (ignoring the sign) just like 36 - abs(-36) = 36 which is in range.
10 ≤ x ≤ 99 for two-digit numbers
Two-digit numbers range from 10 to 99 inclusive. Python's chained comparison 10 <= x <= 99 is cleaner than x >= 10 and x <= 99.
Check two-digit before even
The two-digit check uses abs(num); the even check uses num. Keep them separate - don't accidentally check abs(num) % 2 for evenness, which would also make -19 appear even.