Write two recursive functions (no loops and no ** operator — each function must call itself): power(base, exponent) returns base to the power of exponent (a whole number 0 or more). Base case: any number to the power 0 is …
Write a recursive function sum_to(n) that returns the sum of all whole numbers from 1 to n. For example sum_to(4) is 4 + 3 + 2 + 1 = 10. Base case: sum_to(0) returns 0. Recursive case: sum_to(n) is n …
Products are stored as (name, price) tuples. Write two functions with def that use lambdas inside: sort_by_price(products) returns a new list sorted from cheapest to most expensive (use sorted() with a lambda key). Do not change the original list. cheap_names(products, …
Using lambda (not def), create: square — returns a number multiplied by itself is_even — returns True if a number is even, otherwise False Then use them with map() and filter() on numbers = [1, 2, 3, 4, 5, 6] …
Write two functions: build_profile(name, **details) returns a dictionary that contains the key “name” plus every keyword argument. For example build_profile(“Selam”, city=”Asmara”, age=16) returns {‘name’: ‘Selam’, ‘city’: ‘Asmara’, ‘age’: 16}. count_args(*args, **kwargs) returns a tuple: (number of positional arguments, number of …
Define average(*numbers) that accepts any number of numbers and returns their average. If no numbers are given, return 0. Print average(70, 80, 90). Expected output: 80.0 The checker will also try average(5), average() and a list unpacked with *.
This function should put a student into a group. If no group is given, it should start a new, empty group. But it has the mutable default bug: run it and you will see Abel appear in Selam’s group. Fix …
Define make_greeting(name, greeting=”Selam”) that returns a string in this form: Selam, Abel! The greeting comes first, then a comma and a space, then the name and an exclamation mark. If no greeting is given, the default “Selam” is used. Print …
Define mark_summary(marks) that takes a list of marks and returns three values: the lowest mark, the highest mark, and the average rounded to 1 decimal place. Then unpack the result into low, high and avg for the marks [70, 85, …
Define a function total_price(price, quantity) that returns (not prints) the price multiplied by the quantity. Then print the result of total_price(25, 3) — 3 notebooks at 25 Nakfa each. Expected output: 75
