Level of Difficulty:
Objective: This workout provides more practice creating calculations and filters with functions.
Easy
Create a function that takes an integer and returns the factorial of that integer. That is, the integer multiplied by all positive lower integers.
Examples:
factorial(3) ➞ 6
factorial(5) ➞ 120
factorial(13) ➞ 6227020800
Notes:
Assume all inputs are greater than or equal to 0.
Medium:
Write a function that takes a list of numbers and returns a list with two elements:
- The first element should be the sum of all even numbers in the list.
- The second element should be the sum of all odd numbers in the list.
Examples:
sum_odd_and_even([1, 2, 3, 4, 5, 6]) ➞ [12, 9]
sum_odd_and_even([-1, -2, -3, -4, -5, -6]) ➞ [-12, -9])
sum_odd_and_even([0, 0]) ➞ [0, 0])
Notes:
Count 0 as an even number.
Hard:
Create a function that returns the majority vote in a list. A majority vote is an element that occurs > N/2 times in a list (where N is the length of the list).
Examples:
majority_vote(["A", "A", "B"]) ➞ "A"
majority_vote(["A", "A", "A", "B", "C", "A"]) ➞ "A"
majority_vote(["A", "B", "B", "A", "C", "C"]) ➞ None
Notes
• The frequency of the majority element must be strictly greater than 1/2.
• If there is no majority element, return None.
• If the list is empty, return None.
Simply post your code and a screenshot of your results.
Please format your Python code and blur it or place it in a hidden section.
This workout will be released on Monday April 24, 2023, and the author’s solution will be posted on Wednesday April 30, 2023.