S1Q1 · Tens Digit Greater Than Ones Digit¶
⚡ Quick Reference
Function: is_tens_digit_greater(num: int) -> bool
Core idea: Extract digits using // and %, then compare.
Key rules:
- Tens digit = num // 10
- Ones digit = num % 10
- Return True only if tens digit is strictly greater
Problem Statement¶
Problem
Write a function is_tens_digit_greater(num) that takes a positive two-digit integer and returns True if the tens digit is strictly greater than the ones digit, False otherwise.
Examples:
is_tens_digit_greater(54)
True
is_tens_digit_greater(33)
False
is_tens_digit_greater(27)
False
Understanding the problem¶
For any two-digit number, integer division and modulo extract the two digits:
num |
num // 10 (tens) |
num % 10 (ones) |
tens > ones? |
|---|---|---|---|
| 54 | 5 | 4 | ✅ True |
| 33 | 3 | 3 | ✗ False (equal, not greater) |
| 27 | 2 | 7 | ✗ False |
| 91 | 9 | 1 | ✅ True |
// and % are the standard digit extraction tools
For a two-digit number: num // 10 drops the ones place (integer division by 10), and num % 10 isolates the ones place (remainder when divided by 10). This generalises -- for any n-digit number, % 10 always gives the last digit and // 10 removes it.
Solution approaches¶
Key takeaways¶
Digit extraction with // and %
num // 10 gives the tens digit; num % 10 gives the ones digit. This is the standard arithmetic approach -- no string conversion needed. It generalises to any position: // 100 % 10 gives the hundreds digit, and so on.
Comparison returns a bool directly
tens > ones already evaluates to True or False -- no need to wrap it in if tens > ones: return True else: return False. The comparison expression is the return value.
String alternative: str(num)[0]
An alternative is int(str(num)[0]) > int(str(num)[1]) -- convert to string, index the characters, convert back to int. It works but is slower and less idiomatic than the arithmetic approach for simple digit extraction.