Assignment 5: Functions in Python
1. Basic Function Creation
Write a function greet() that prints "Hello, World!".
2. Function with Parameters
Write a function square(num) that returns the square of a number.
3. Multiple Parameters & Return Value
Write a function add_subtract(a, b) that returns the sum and difference of two numbers.
4. Default Arguments
Write a function power(base, exponent=2) that returns base raised to the power of
exponent.
5. Function Documentation (Docstring)
Write a function area_of_circle(radius) with a docstring explaining its purpose and formula.
6. Variable Number of Arguments (*args)
Write a function sum_all(*numbers) that returns the sum of any number of numeric
arguments.
7. Keyword Arguments (**kwargs)
Write a function print_person_info(**info) that prints the details of a person given as key-
value pairs.
8. Scope (Local vs Global Variables)
Demonstrate the difference between local and global variables using a function that
modifies a global variable.
9. Lambda Function
Write a lambda function to find the maximum of two numbers and use it in your code.
10. Functions Returning Functions (Closures)
Write a function multiplier(factor) that returns a function to multiply numbers by that
factor.
11. Recursion
Write a recursive function factorial(n) to calculate the factorial of a number.
12. Higher-Order Function with map()
Use map() and a lambda function to convert a list of temperatures in Celsius to Fahrenheit.
13. Filter Function
Use filter() to extract even numbers from a given list.
14. Reduce Function
Use functools.reduce() to compute the product of all numbers in a list.
15. Function as an Argument
Write a function apply_operation(operation, numbers) that applies any given function (like
sum, square, etc.) to a list of numbers.
16. Unique Elements Across Multiple Lists
Problem: Write a function that accepts multiple lists and returns a sorted list of unique
elements across all the lists combined.
Sample Input: unique_elements([1, 2, 3], [2, 3, 4], [5])
Sample Output: [1, 2, 3, 4, 5]
17. Frequency Counter for Words
Problem: Given a string, count the frequency of each word, ignoring case and punctuation.
Sample Input: word_frequency("Hello world! Hello Python.")
Sample Output: {'hello': 2, 'world': 1, 'python': 1}
18. Tuple Sorting by Second Element
Problem: Sort a list of tuples by the second element of each tuple in ascending order.
Sample Input: sort_by_second([(1, 3), (4, 1), (2, 2)])
Sample Output: [(4, 1), (2, 2), (1, 3)]
19. Merge Dictionaries with Summation
Problem: Merge two dictionaries by adding values of common keys.
Sample Input: merge_sum({'a': 2, 'b': 3}, {'b': 4, 'c': 5})
Sample Output: {'a': 2, 'b': 7, 'c': 5}
20. Shopping Cart Total
Problem: Given a shopping cart as a list of tuples (item_name, price_per_unit, quantity),
return the total cost.
Sample Input: cart_total([("apple", 10, 2), ("banana", 5, 5)])
Sample Output: 45
21. Playlist Duration
Problem: Given a playlist dictionary where keys are song names and values are (minutes,
seconds), return the total playlist duration as (minutes, seconds).
Sample Input: playlist_duration({"song1": (3, 15), "song2": (4, 50)})
Sample Output: (8, 5)
22. Common Elements in Multiple Sets
Problem: Find common elements across all given sets.
Sample Input: common_in_sets({1, 2, 3}, {2, 3, 4}, {3, 2, 5})
Sample Output: {2, 3}
23. Dictionary Inversion
Problem: Invert a dictionary so that values become keys and keys are collected into lists for
duplicate values.
Sample Input: invert_dict({'a': 1, 'b': 2, 'c': 1})
Sample Output: {1: ['a', 'c'], 2: ['b']}
24. Student Marks Summary
Problem: Given a dictionary of student names and their marks list, return a summary with
each student’s average, maximum, and minimum marks.
Sample Input: student_summary({"A": [80, 90, 85], "B": [70, 75]})
Sample Output: {'A': {'average': 85.0, 'max': 90, 'min': 80}, 'B': {'average': 72.5, 'max': 75,
'min': 70}}
25. Group Anagrams
Problem: Group words that are anagrams of each other.
Sample Input: group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
Sample Output: [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]
26. Transaction Summary
Problem: Given a list of (account_number, amount) transactions, return the final balance for
each account.
Sample Input: transaction_summary([(101, 200), (102, -50), (101, 300)])
Sample Output: {101: 500, 102: -50}
27. Flatten Nested Lists (Recursive)
Problem: Flatten a deeply nested list into a single list.
Sample Input: flatten_list([1, [2, [3, 4], 5], 6])
Sample Output: [1, 2, 3, 4, 5, 6]
28. Matrix Transpose
Problem: Return the transpose of a given matrix.
Sample Input: transpose([[1, 2], [3, 4], [5, 6]])
Sample Output: [[1, 3, 5], [2, 4, 6]]
29. Dictionary of Factorials
Problem: Create a dictionary mapping integers from 1 to n to their factorial values.
Sample Input: factorial_dict(5)
Sample Output: {1: 1, 2: 2, 3: 6, 4: 24, 5: 120}
30. Custom Zip
Problem: Implement a custom version of the zip() function that pairs elements from two
lists up to the shortest length.
Sample Input: custom_zip([1, 2, 3], ['a', 'b'])
Sample Output: [(1, 'a'), (2, 'b')]