Why use the (True, False)[n%2==0] format, this structure in Python is much easier on the eye, at least in my opinion.
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
from random import randint for x in range(5): n = 2 ** randint( 21, 25 ) test.assert_equals( power_of_two(n), True ) for x in range(5): n = 2 ** randint( 21, 25 )+(-1) ** randint( 1, 4 ) test.assert_equals( power_of_two(n), False )
- from random import randint
- for x in range(5):
n = 2 ** randint( 21, 25 )test.assert_equals( power_of_two(n), True )- n = 2 ** randint( 21, 25 )
- test.assert_equals( power_of_two(n), True )
- for x in range(5):
n = 2 ** randint( 21, 25 )+(-1) ** randint( 1, 4 )test.assert_equals( power_of_two(n), False )- n = 2 ** randint( 21, 25 )+(-1) ** randint( 1, 4 )
- test.assert_equals( power_of_two(n), False )
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 Truen = 2power = 1numb = 0while numb <= x:numb = n**powerif numb == x:return Truepower+=1return 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
from random import randint for x in range(5): n = 2 ** randint( 21, 25 ) test.assert_equals( power_of_two(n), True ) for x in range(5): n = 2 ** randint( 21, 25 )+(-1) ** randint( 1, 4 ) test.assert_equals( power_of_two(n), False )
- from random import randint
- for x in range(5):
n = 2 ** randint( 21, 25 )test.assert_equals( power_of_two(n), True )- n = 2 ** randint( 21, 25 )
- test.assert_equals( power_of_two(n), True )
- for x in range(5):
n = 2 ** randint( 21, 25 )+(-1) ** randint( 1, 4 )test.assert_equals( power_of_two(n), False )- n = 2 ** randint( 21, 25 )+(-1) ** randint( 1, 4 )
- test.assert_equals( power_of_two(n), False )