Page 1 of 1

In Python

Posted: Fri Apr 29, 2022 7:11 am
by answerhappygod
In Python
In Python 1
In Python 1 (316.29 KiB) Viewed 32 times
Problem 4 - Any/all, filtering, counting [5x5 points] For this problem, you should define all functions within the even library module. All functions in this problem should accept the same kind of argument: a list of integers. Furthermore, all functions that we ask you to define perform the same condition test over each of the list elements (specifically, test if it's even). However, each returns a different kind of result (as described below). Finally, once again none of the functions should modify their input list in any way. Remark: Although we do not require you to re-use functions in a specific way, you might want to consider doing so, to simplify your overall effort. You may define the functions in any order you wish (i.e., the order does not necessarily have to correspond to the sub-problem number), as long as you define all of them correctly. Problem 4.1 - even. keep(): Should return a new list, which contains only those numbers from the input list that are even. Problem 4.2- even.drop(): Should return a new list, which contains only those numbers from the input list that are not even. Problem 4.3 - even.all(): Should return True if all numbers in the input list are even, and False otherwise. Just to be clear, although you should not be confusing data types this point, the returned value should be boolean. Problem 4.4 - even. any(): Should return True if at least one number in the input list is even, and False otherwise. As a reminder, what we ask you here is not the opposite of the previous problem: 6 the negation of "all even" is "at least one not even”. Problem 4.5 - even.count(): Should return an integer that represents how many of the numbers in the input list are even. Hint: The keep function is very similar to the keep_positive function from the previous assignment. The all function is very similar to the second problem (i.e., the all_positive function) that you worked on during the lecture of Mar 29th; you can reuse almost all of your work from there. Finally, the remaining functions should be relatively simple variations of the same coding patterns.