0
0
DSA Pythonprogramming~10 mins

Fast Exponentiation Power in Log N in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to multiply the result by base when exponent is odd.

DSA Python
def fast_power(base, exponent):
    result = 1
    while exponent > 0:
        if exponent % 2 == 1:
            result = result * [1]
        base = base * base
        exponent = exponent // 2
    return result
Drag options to blanks, or click blank then click option'
A1
Bexponent
Cresult
Dbase
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying by exponent instead of base.
Multiplying by result itself causing wrong calculations.
2fill in blank
medium

Complete the code to update the exponent by dividing it by 2 using integer division.

DSA Python
def fast_power(base, exponent):
    result = 1
    while exponent > 0:
        if exponent % 2 == 1:
            result = result * base
        base = base * base
        exponent = [1]
    return result
Drag options to blanks, or click blank then click option'
Aexponent % 2
Bexponent / 2
Cexponent // 2
Dexponent - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using normal division (/) which returns float.
Using modulo (%) which gives remainder, not division.
3fill in blank
hard

Fix the error in the base update step to correctly square the base.

DSA Python
def fast_power(base, exponent):
    result = 1
    while exponent > 0:
        if exponent % 2 == 1:
            result = result * base
        base = base [1] base
        exponent = exponent // 2
    return result
Drag options to blanks, or click blank then click option'
A*
B+
C-
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' which adds instead of multiplies.
Using '-' or '//' which are incorrect for squaring.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps numbers to their powers of 2 for numbers less than 5.

DSA Python
powers = {x: 2[1]x for x in range(5) if x [2] 5}
Drag options to blanks, or click blank then click option'
A**
B<
C>
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '**' for power.
Using '>' instead of '<' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase letters to their ASCII codes if the code is greater than 65.

DSA Python
ascii_map = [1]: ord([2]) for [3] in 'ABCDEF' if ord([2]) > 65}
Drag options to blanks, or click blank then click option'
Aletter
Dchar
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Not matching variable names in keys and ord() calls.