Practice Top 30 Python Programming Questions on Decorators and Generators
Q1. Write a generator function count_up_to(n) that yields numbers from 1 to n.
Input:
Enter n: 5
Expected Output:
1
2
3
4
5
Q2. Write a generator function even_numbers(n) that yields even numbers up to n.
Input:
Enter n: 6
Expected Output:
2
4
6
Q3. Create a generator expression to generate squares of numbers from 1 to 5.
Expected Output:
1
4
9
16
25
Q4. Write a generator infinite_sequence() that yields numbers starting from 1 indefinitely. Print the first 5 numbers from the generator.
Expected Output:
1
2
3
4
5
Q5. Write a decorator log_function_call that logs a message before and after calling a function. Use it on a function greet() that prints "Hello!".
Expected Output:
Calling function greet...
Hello!
Finished calling function greet.
Q6. Write a generator function reverse_string(s) that yields characters of a string in reverse order.
Input:
Enter a string: Python
Expected Output:
n
o
h
t
y
P
Q7. Write a decorator uppercase that converts the result of a function returning a string to uppercase. Apply it to a function say_hello() that returns "hello".
Expected Output:
HELLO
Q8. Write two generator functions, generate_numbers(n) to yield numbers up to n, and square_numbers(gen) to yield squares of numbers from another generator.
Input:
Enter n: 3
Expected Output:
1
4
9
Q9. Write a decorator timer to calculate the time taken by a function to execute. Apply it to a function slow_function() that sleeps for 1 second.
Expected Output:
Execution time: 1.00 seconds.
Q10. Write a generator fibonacci(n) that yields the first n numbers in the Fibonacci sequence.
Input:
Enter n: 5
Expected Output:
0
1
1
2
3
Q11. Write a decorator validate_positive that checks if the input to a function is a positive number. If not, raise a ValueError. Apply it to a function double(n) that doubles the input number.
Input:
Enter a number: -5
Expected Output:
ValueError: Input must be a positive number.
Q12. Write a generator function filter_odd(numbers) that takes a list of numbers and yields only the odd ones.
Input:
Enter numbers: [1, 2, 3, 4, 5]
Expected Output:
1
3
5
Q13. Write a decorator count_calls that counts the number of times a function is called. Apply it to a function greet() that prints "Hello!".
Input:
Call greet() three times.
Expected Output:
Hello!
Hello!
Hello!
Function greet was called 3 times.
Q14. Write two generator functions:
- generate_letters(s) to yield each letter of a string.
- capitalize_letters(gen) to yield the capitalized version of letters from another generator.
Input:
Enter a string: abc
Expected Output:
A
B
C
Q15. Write a decorator memoize that caches the results of a function. Apply it to a function factorial(n) that calculates the factorial of a number.
Input:
Enter a number: 5
Expected Output:
Factorial of 5 is 120.
Q16. Write a generator function nested_numbers() that yields lists of numbers, and another generator flatten(gen) to flatten these lists into a single sequence of numbers.
Input:
nested_numbers() yields: [[1, 2], [3, 4], [5]]
Expected Output:
1
2
3
4
5
Q17. Write a decorator add_greeting that adds "Hi, " before the output of a function that returns a name. Apply it to a function get_name() that returns a name.
Input:
Name: Alice
Expected Output:
Hi, Alice
Q18. Write a generator function generate_primes(n) that yields prime numbers less than or equal to n.
Input:
Enter n: 10
Expected Output:
2
3
5
7
Q19. Write a decorator repeat(times) that repeats the execution of a function a specified number of times. Apply it to a function say_hello() that prints "Hello!".
Input:
Repeat 3 times.
Expected Output:
Hello!
Hello!
Hello!
Q20. Write a generator multiplication_table(n) that yields the multiplication table of n.
Input:
Enter n: 3
Expected Output:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
Q21. Write a decorator log_arguments that logs the arguments of a function call. Apply it to a function add(a, b) that returns the sum of two numbers.
Input:
Add 3 and 5.
Expected Output:
Calling add with arguments: (3, 5)
8
Q22. Write a generator function repeat_characters(s) that takes a string and yields each character repeated twice.
Input:
Enter a string: Hello
Expected Output:
HH
ee
ll
ll
oo
Q23. Write a decorator generator_timer to calculate the time taken by a generator function to yield all its values. Apply it to a generator count_to(n) that counts up to n.
Input:
Count to 3.
Expected Output:
1
2
3
Q24. Write a generator char_indices(s) that yields each character in a string along with its index.
Input:
Enter a string: abc
Expected Output:
(0, 'a')
(1, 'b')
(2, 'c')
Q25. Write a decorator type_check to validate that the arguments of a function are integers. Apply it to a function add_numbers(a, b) that adds two numbers.
Input:
Enter a: 5
Enter b: abc
Expected Output:
TypeError: Arguments must be integers.
Q26. Write a generator sum_numbers(n) that yields numbers from 1 to n, and then returns their sum.
Input:
Enter n: 4
Expected Output:
1
2
3
4
Sum is 10.
Q27. Write a decorator check_access that allows function execution only if the username matches a given value. Apply it to a function access_resource() that simulates accessing a resource.
Input:
Enter username: admin
Expected Output:
Access granted to admin.
Q28. Write a generator powers_of_two(n) that yields powers of two up to n.
Input:
Enter n: 16
Expected Output:
1
2
4
8
16
Q29. Write a decorator execute_if_positive that only allows the decorated function to execute if the argument is positive. Apply it to a function square_number(n) that returns the square of a number.
Input:
Enter a number: -3
Expected Output:
Condition not met. Function not executed.
Q30. Create a generator expression to generate even numbers from 1 to 10.
Expected Output:
2
4
6
8
10
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!