Ad

Why use the (True, False)[n%2==0] format, this structure in Python is much easier on the eye, at least in my opinion.

Code
Diff
  • power_of_two = lambda n: False if n%2 else True
    • power_of_two = lambda n: (False, True)[n % 2 == 0]
    • power_of_two = lambda n: False if n%2 else True
Code
Diff
  • def digit_sum(number: int) -> int:
        return sum([int(num) for num in str(number) if num.isdigit()])
    
    • def digit_sum(number: int) -> int:
    • return sum(map(int,list(str(abs(number)))))
    • return sum([int(num) for num in str(number) if num.isdigit()])
Code
Diff
  • def power_of_two( x ):
        if x == 1 or x==0: return True
        n, power, numb = 2, 1, 0
        while numb < x:
            numb, power = n**power, power+1
        return True if numb == x else False
    • def power_of_two( x ):
    • if x == 1:
    • return True
    • n = 2
    • power = 1
    • numb = 0
    • while numb <= x:
    • numb = n**power
    • if numb == x:
    • return True
    • power+=1
    • return False
    • if x == 1 or x==0: return True
    • n, power, numb = 2, 1, 0
    • while numb < x:
    • numb, power = n**power, power+1
    • return True if numb == x else False