Problem 4 - Any/all, filtering, counting (5x5 points) For this problem, you should define all functions within the even
Posted: Sun May 15, 2022 8:06 am
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 argumenta 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 (e, the onder 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 - aven.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 by 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 problems 7 33:136-388- Spring 2022 Homework Due date: 5/9 the negation of all even" is at least one not even" Problem 4.5 - even count(): Should retum an integer that represents how many of the numbers in the input list are even
00 wN 3 4 5 class Even: 6 def keep(self,l): #Problem 4.1 solution 7 e-[] 8 for i in l: 9 if i%2--0: 10 e.append(i) 11 return e 12 13 def drop(self,l): #Problem 4.2 solution 14 e-[] 15 for i in 1: 16 if ix2!-e: 17 e.append(i) 18 return e 19 20 def all(self,l):#Problem 4.3 solution 21 ne 22 for i in 1: if ix2!-e: 24 n-1 25 if(n==0): 26 | return True 27 else: 28 return False 29 30 def any(self,l): #Problem 4.4 solution 31 ne 32 for i in 1: 33 if 1X2--: 34 n1 35 if(new): 36 return false 37 else: 38 return True 23
se: return false @ 51 32 33 34 def any(self,l): #Proble n= for i in 1: if i%2==0: n=1 if(n==0): | return false else: return True 35 36 37 38 39 40 41 42 43 44 45 even=Even() 1=[1,2,3,4,5,6,7,8,9] print(even.keep(1)) print(even.drop(1)) print(even.all(1)) print(even.any(1))
E. A O O ® O O OO 9 10 11 12 13 14 15 16 17 18 00 test_evency AllTests AnyTests test_not_builtin test_empty test_singleton test_simple test_ranges 100 test_import test_is_library CountTests test_empty test_singleton O test simple test_ranges 100 test_import test_is_library v Drop Tests test_empty test singleton test ranges 100 test_user_defined si test_import test_is_library Keep Tests test empty test singleton test ranges 100 test_user_defined_s.. test import test_is_library 19 28 21 22 23 24 25 26 27 28 def 8 OOO 29 30 31 32 33 34 35 36 37 38
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 - aven.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 by 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 problems 7 33:136-388- Spring 2022 Homework Due date: 5/9 the negation of all even" is at least one not even" Problem 4.5 - even count(): Should retum an integer that represents how many of the numbers in the input list are even
00 wN 3 4 5 class Even: 6 def keep(self,l): #Problem 4.1 solution 7 e-[] 8 for i in l: 9 if i%2--0: 10 e.append(i) 11 return e 12 13 def drop(self,l): #Problem 4.2 solution 14 e-[] 15 for i in 1: 16 if ix2!-e: 17 e.append(i) 18 return e 19 20 def all(self,l):#Problem 4.3 solution 21 ne 22 for i in 1: if ix2!-e: 24 n-1 25 if(n==0): 26 | return True 27 else: 28 return False 29 30 def any(self,l): #Problem 4.4 solution 31 ne 32 for i in 1: 33 if 1X2--: 34 n1 35 if(new): 36 return false 37 else: 38 return True 23
se: return false @ 51 32 33 34 def any(self,l): #Proble n= for i in 1: if i%2==0: n=1 if(n==0): | return false else: return True 35 36 37 38 39 40 41 42 43 44 45 even=Even() 1=[1,2,3,4,5,6,7,8,9] print(even.keep(1)) print(even.drop(1)) print(even.all(1)) print(even.any(1))
E. A O O ® O O OO 9 10 11 12 13 14 15 16 17 18 00 test_evency AllTests AnyTests test_not_builtin test_empty test_singleton test_simple test_ranges 100 test_import test_is_library CountTests test_empty test_singleton O test simple test_ranges 100 test_import test_is_library v Drop Tests test_empty test singleton test ranges 100 test_user_defined si test_import test_is_library Keep Tests test empty test singleton test ranges 100 test_user_defined_s.. test import test_is_library 19 28 21 22 23 24 25 26 27 28 def 8 OOO 29 30 31 32 33 34 35 36 37 38