0%

🪄 The Number Hidden In The Mess

Even scattered digits follow rules. Sort them, read them, and the top number quietly appears.
🔮 sorted(str(n))[-1]

A number spills its digits in chaos. 583291. No order. No rhythm. But hidden inside, a ruler waits. The largest digit. The one that stands above all others. You just need to know how to ask. The expression sorted(str(n))[-1] works in three steps. First, convert the number to a string. Second, sort the characters alphabetically (which works for digits because ‘0’ to ‘9’ are in order). Third, take the last element, which is the largest digit.

🕯️ Magic Note

When you sort a string of digits, Python arranges them in ascending order based on their Unicode code points. For digits ‘0’ to ‘9’, this matches numerical order. The smallest digit comes first, the largest last. Index [-1] always gives the final element. Together, they extract the maximum digit without loops or conditions.

The syntax sorted(str(n))[-1] takes your number n, turns it into a string like “583291”, sorts the characters to [“1″,”2″,”3″,”5″,”8″,”9”], then picks the last item “9”. The largest digit emerges from the chaos. The mess becomes order.
  • Works for both positive and negative integers, but negative signs become ‘-‘ which sorts before digits
  • Returns a string, convert with int() if you need a number
  • Alternative: max(str(n)) is simpler and more direct
  • For the smallest digit, use [0] instead of [-1]
💡 The simpler way to find the largest digit is max(str(n)). The max() function directly returns the highest character in the string. The sorted()[-1] approach is useful when you also need the sorted list for other purposes. For a single largest digit, max() is clearer and more efficient.
Input NumberConvert to StringSortedLargest Digit
583291“583291”[“1″,”2″,”3″,”5″,”8″,”9”]“9”
9427“9427”[“2″,”4″,”7″,”9”]“9”
55555“55555”[“5″,”5″,”5″,”5″,”5”]“5”
10203“10203”[“0″,”0″,”1″,”2″,”3”]“3”
9876543210“9876543210”[“0″,”1″,”2″,”3″,”4″,”5″,”6″,”7″,”8″,”9”]“9”
⚠️ This method includes the negative sign when dealing with negative numbers. For n = -583, str(n) becomes “-583”. Sorted gives [“-“,”3″,”5″,”8”], and the largest digit would be “8”, not including the sign. However, [-1] still works correctly. For the smallest digit on negative numbers, be careful because ‘-‘ is smaller than digits. Also, this returns a string, not an integer.
Examples

Python

# Finding the largest digit using sorted

n = 583291

largest = sorted(str(n))[-1]

print(largest)

# Output: 9

Python

# Simpler approach with max()

n = 583291

largest = max(str(n))

print(largest)

# Output: 9

# Much cleaner than sorted()[-1]

Python

# Getting both smallest and largest digits

n = 749218

digits = sorted(str(n))

smallest = digits[0]

largest = digits[-1]

print(f”Smallest: {smallest}, Largest: {largest}”)

# Output: Smallest: 1, Largest: 9

print(f”All digits in order: {”.join(digits)}”)

# Output: All digits in order: 124789

Common Mistakes
  • Forgetting to convert the number to a string, causing TypeError: ‘int’ object is not iterable
  • Using sorted(n) instead of sorted(str(n))
  • Not realizing that the result is a string, then using it in a numeric context without converting with int()

⚡ Whisper

Chaos hides order. Scattered digits wait for a hand to sort them. Line them up from smallest to largest. The one at the end was always the ruler. Quietly, patiently, the top number appears. The mess becomes music.