0
0
DSA Pythonprogramming~10 mins

Why Math and Number Theory Appear in DSA Problems in DSA Python - Test Your Knowledge

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

Complete the code to calculate the greatest common divisor (GCD) of two numbers using Euclid's algorithm.

DSA Python
def gcd(a, b):
    while b != 0:
        a, b = b, a [1] b
    return a
Drag options to blanks, or click blank then click option'
A+
B%
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of modulo
Using addition or multiplication incorrectly
2fill in blank
medium

Complete the code to check if a number n is prime by testing divisibility up to its square root.

DSA Python
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n[1]0.5) + 1):
        if n % i == 0:
            return False
    return True
Drag options to blanks, or click blank then click option'
A**
B*
C//
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of exponentiation
Using integer division which truncates
3fill in blank
hard

Fix the error in the code to compute the modular exponentiation (a^b mod m) efficiently.

DSA Python
def mod_exp(a, b, m):
    result = 1
    a = a % m
    while b > 0:
        if b & 1 == 1:
            result = (result * a) [1] m
        a = (a * a) % m
        b = b >> 1
    return result
Drag options to blanks, or click blank then click option'
A+
B-
C*
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication without modulo
Using addition or subtraction instead of modulo
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps numbers to their squares only if the number is even.

DSA Python
squares = {x: x[1]2 for x in range(1, 11) if x [2] 2 == 0}
Drag options to blanks, or click blank then click option'
A**
B+
C%
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of exponentiation
Using subtraction instead of modulo
5fill in blank
hard

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

DSA Python
ascii_map = { [1]: [2] for [1] in map(chr, range(65, 91)) if ord([3]) > 65 }
Drag options to blanks, or click blank then click option'
Aletter
Bord(letter)
Dchar
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names
Using char instead of letter in condition