Convert to a Binary String

Eliud tojásai
Eliud tojásai a(z) Python kurzusban
def egg_count(display_value):
    binary_value = bin(display_value)[2:] # <- Slice off the first two characters.
    eggs = 0
    for digit in binary_value:
        eggs += int(digit)
    return eggs

This approach uses bin() to convert display_value to a binary string. Next, the first two characters of the binary string are removed via slicing, as the string has "0b" as a prefix before the binary digits.

After the binary digits are obtained, this solution loops across all of them, turning each one into an int and adding it to eggs. This counts up all of the instances of "1" in the binary string, as 0 and 1 are the only valid binary digits.

Those less familiar with binary may find this approach to be simpler than the others. However, it does have the added overhead of converting to and from a string.

Note

There are three other Pythonic ways of obtaining a binary string. These strategies use the string format specification in different ways.

The first is to use an f-string:

# This uses the 'binary' format code.
binary_value = f"{display_value:b}"

Another is to use str.format():

# This also uses the 'binary' format code with different syntax.
binary_value = "{:b}".format(display_value)

Lastly, you could use the format() built-in:

# This uses the 'binary' format code passed as a quoted string.
binary_value = format(display_value, "b")

These methods have the added benefit of not producing the "0b" prefix that then needs to removed. However, some variations of this approach use other ways to get around the prefix.

Variation #1: Using sum() with a Generator Expression

def egg_count(display_value):
    return sum(int(digit) for digit in bin(display_value)[2:])

This variant uses a generator expression with the sum() built-in to collect the digits into the result. Otherwise, it is the same as the previous version.

Variation #2: Using sum() Without String Slicing

def egg_count(display_value):
    return sum(1 for digit in bin(display_value) if digit == "1")

Similar to the previous variant, this one uses sum() with a generator expression. The main difference is that it avoids slicing and copying the binary string by using an if clause in the generator expression. This also avoids the overhead of calling int() on each digit.

Variation #3: Using len() Instead of sum()

def egg_count(display_value):
    return len([True for digit in bin(display_value) if digit == "1"])

This variant replaces the generator expression with a list comprehension. This way, it can use len() to get the number of ones after the comprehension filters out the other digits using an if clause.

Here, True is used for the list elements, but we could use any other value as well, as we only care about the length of the list.

Variation #4: Using map() Instead of a Generator Expression or List Comprehension

def egg_count(display_value):
    return sum(map(int, bin(display_value)[2:]))

Here, we directly map the elements of the binary string to int() without using a generator expression or a list comprehension.

This variant is the most concise, but it may be not very comprehensible to those unfamiliar with functional programming. It also somewhat hides the overhead that is incurred by calling int() on every digit.

Variation #5: Using filter() with a lambda

def egg_count(display_value):
    return len(list(filter(lambda digit: digit == "1", bin(display_value))))

Another alternative to a generator expression (or list comprehension) is to use the filter() built-in along with a lambda expression. However, the creation and repeated calling of the lambda adds unnecessary overhead to the solution, and filter() typically runs slower than the equivalent list comprehension.

Variation #6: Using functools.reduce() with a lambda

from functools import reduce

def egg_count(display_value):
    return reduce(lambda eggs, digit: eggs + int(digit), bin(display_value)[2:], 0)

This variant uses reduce() from the functools module along with a lambda expression. Here, functools.reduce() calls the lambda on each codepoint in bin(display_value)[2:], accumulating the value of eggs from the initial 0.

Similar to the previous variant, the creation and repeated calling of the lambda causes extra overhead, as does importing the functools module.

2026. szeptember 23. · Hasznosnak találtad?