Editor

Find the Largest Binary Number

Intermediate · Write the code · from Binary Numbers and Bits: How Computers Represent Data

From the lesson: How do you compare and order binary numbers?

To compare two binary numbers by hand, line them up so they have the same number of digits by writing zeros on the left. Leading zeros never change a value, the same way 007 is still 7. Then read from the left: the first position where the numbers differ decides, and the number with the 1 in that position is larger.

Read this part of the lesson

Write largest_binary(numbers). It takes a list of binary numbers written as strings and returns the one with the largest value, exactly as it appears in the list (keep any leading zeros). The list always has at least one item, and no two items have the same value.

largest_binary(['101', '1000', '11'])  ->  '1000'

Here max() on the text would wrongly pick '11'.

def largest_binary(numbers):
# Compare values, not text
pass

What Check runs

Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

RunsExpected output
print(largest_binary(["101", "1000", "11"]))1000
print(largest_binary(["0110", "111"]))111
print(largest_binary(["1"]))1

More Binary Numbers exercises

All Python exercises · 50 practice problems