The problem
Full the operate that takes a non-negative integer n
as enter, and returns an inventory of all of the powers of two with the exponent starting from 0 to n
(inclusive).
Examples
n = 0 ==> [1] # [2^0]
n = 1 ==> [1, 2] # [2^0, 2^1]
n = 2 ==> [1, 2, 4] # [2^0, 2^1, 2^2]
The answer in Python code
Choice 1:
def powers_of_two(n):
out = []
for i in vary(n+1):
out.append(2**i)
return out
Choice 2:
def powers_of_two(n):
return [2**i for i in range(n+1)]
Choice 3:
def powers_of_two(n):
return [1<<x for x in range(n + 1)]
Check circumstances to validate our resolution
import check
from resolution import powers_of_two
@check.describe("Fastened Exams")
def fixed_tests():
@check.it('Fundamental Check Instances')
def basic_test_cases():
check.assert_equals(powers_of_two(0), [1])
check.assert_equals(powers_of_two(1), [1, 2])
check.assert_equals(powers_of_two(4), [1, 2, 4, 8, 16])