Lecture
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]
Previous lessonString Case Alteration - Solution ExplanationNext lessonCreating a Power of Two List - Solution Guide
Lessons in this chapter · Introduction to Python Algorithms
- 1. Why You Need to Learn Algorithms?
- 2. Review of Algorithm Complexity Concepts
- 3. Life is Practical! Algorithm Problem Solving
- 4. Coding Quiz - Reverse String
- 5. String Reversal - Solution
- 6. Coding Quiz - String Case Conversion
- 7. String Case Alteration - Solution Explanation
- 8. Coding Quiz - Create a List of Powers of 2
- 9. Creating a Power of Two List - Solution Guide
- 10. Coding Quiz - Sorting Tuples by Score
- 11. Sort Tuples by Score - Solution
- 12. Coding Quiz - Find Union and Intersection
- 13. Calculating Union and Intersection - Problem Solution
- 14. Coding Quiz - Create a Dictionary from Key/Value Pairs
- 15. Create Dictionary from Key/Value Pairs - Solution
- 16. Coding Quiz - Determine Grade Using Conditional Statements
- 17. Calculating Grades with Conditional Statements - Solution
- 18. Coding Quiz - Diagonal Traverse of 2D Array
- 19. Diagonal Traversal of a 2D Array - Problem Solution
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help