Coding Quiz - Create a List of Powers of 2
In this coding quiz, you will write a function that generates a list of numbers, each a power of 2.
You will receive an integer n from the user and return a list containing values from 2^0
to 2^n
.
For example, if the input integer is 3
, the returned list should be [1, 2, 4, 8]
.
Code Solution
def solution(n): # Write your code here return
Constraints
-
The input integer n must be 0 or greater.
-
You can assume the result of 2^n does not exceed the integer range.
Input and Output Examples
-
Input:
3
-
Output:
[1, 2, 4, 8]
-
Input:
5
-
Output:
[1, 2, 4, 8, 16, 32]
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help