S1Q1 · Check If a Number is a Decreasing 4-Digit Number¶
⚡ Quick Reference
Function: is_decreasing_number(n: int) -> bool
Core idea: convert to string, check each digit is strictly greater than the next.
def is_decreasing_number(n: int) -> bool:
s = str(n)
return all(s[i] > s[i+1] for i in range(len(s) - 1))
Key rules:
- Strictly decreasing -equal digits (1111) return False
- Input is guaranteed 4-digit (1000–9999)
- String comparison works for single digits: '9' > '8' is True
Problem Statement¶
Problem
Write a function is_decreasing_number(n: int) -> bool that returns True if the digits of the 4-digit number n are strictly decreasing from left to right, and False otherwise.
Examples:
4321
True
4312
False
1111
False
3210
True
Understanding the problem¶
For a number to be strictly decreasing, every digit must be greater than the digit immediately to its right. Equal digits do not qualify.
4321 → 4 > 3 > 2 > 1 ✅
4312 → 4 > 3, 3 > 1, but 1 < 2 ❌
1111 → 1 = 1 = 1 = 1 ❌ (equal, not strictly decreasing)
3210 → 3 > 2 > 1 > 0 ✅
String comparison for single digits
For single digit characters, Python's string comparison matches numeric order: '9' > '8' > ... > '0'. So comparing digits as characters works correctly -no need to convert each to int first.
Tracing the examples¶
n |
str(n) |
Comparisons | Result |
|---|---|---|---|
| 4321 | "4321" |
4>3 ✅ 3>2 ✅ 2>1 ✅ |
True |
| 4312 | "4312" |
4>3 ✅ 3>1 ✅ 1>2 ❌ |
False |
| 9876 | "9876" |
9>8 ✅ 8>7 ✅ 7>6 ✅ |
True |
| 1111 | "1111" |
1>1 ❌ |
False |
| 3210 | "3210" |
3>2 ✅ 2>1 ✅ 1>0 ✅ |
True |
Solution approaches¶
def is_decreasing_number(n: int) -> bool:
s = str(n)
return all(s[i] > s[i+1] for i in range(len(s) - 1))
Convert to string, check each adjacent pair with all(). Short-circuits on the first non-decreasing pair.
def is_decreasing_number(n: int) -> bool:
s = str(n)
for i in range(len(s) - 1):
if s[i] <= s[i+1]: # not strictly greater
return False
return True
Walk through adjacent pairs explicitly. Return False as soon as a violation is found.
def is_decreasing_number(n: int) -> bool:
d1 = n // 1000 # thousands digit
d2 = (n // 100) % 10 # hundreds digit
d3 = (n // 10) % 10 # tens digit
d4 = n % 10 # units digit
return d1 > d2 > d3 > d4
Extract each digit arithmetically and use Python's chained comparison d1 > d2 > d3 > d4. No string conversion needed. Works specifically because the input is guaranteed 4 digits.
zip(s, s[1:]) pairs each digit with the next one: ('4','3'), ('3','2'), ('2','1'). all() checks every pair. More Pythonic than indexing -avoids manually tracking i.
def is_decreasing_number(n: int) -> bool:
s = str(n)
return all(map(lambda pair: pair[0] > pair[1], zip(s, s[1:])))
map(lambda pair: pair[0] > pair[1], zip(s, s[1:])) applies the comparison to each adjacent pair. all() confirms every pair satisfies it. Purely functional -no explicit loop or comprehension.
Key takeaways¶
zip(s, s[1:]) for adjacent pairs
Pairing a sequence with its own shifted version gives all adjacent pairs without manual indexing. zip("4321", "321") → [('4','3'), ('3','2'), ('2','1')].
Chained comparisons
d1 > d2 > d3 > d4 is valid Python and evaluates left to right. Equivalent to d1 > d2 and d2 > d3 and d3 > d4 -cleaner for fixed-length comparisons.
all() short-circuits
all() stops as soon as it finds a False. For 1111, it stops at the very first pair '1' > '1' which is False.