Reputation:
I am learning python and I don't expect an answer. I just need assistance really.
I'm given multiple lists that from the lists I need to check to make sure that:
a) It is not an empty list
b) It has more than one integer within the list
c) Check if the integers within the list has an even product value or an odd product value by checking for distinct pairs. For example list1 = [1,2,3]
. This would return False because the product is even. list2 = [3,2,3]
would return True because the product of the two odd pairs is odd.
These are some of my ideas:
a) To check if it is an empty list you would type:
if not myList:
return(False)
or
if myList != []:
return(True)
b)
if myList != 1:
return(True)
or
if int in myList < 0 and if int in myList > 2:
return(False)
c)
if len(myList) % 2 == 0:
return(False)
Because if there are two even doubles then it would return even either way. I just want to find the odd product.
if len(myList) % 2 != 0:
for i in myList:
if i % i == 1:
return(True)
else:
return(False)
I should test this out but I actually just came up with it writing this. Finding the pairs is pretty difficult.
I figured that if the end result is 1 then they are the same number- right? First time user of this site so I am not familiar with the standard protocol of questions (I did read the rules though).
Sorry if this is long and thanks to whoever helps me out!
Upvotes: 1
Views: 455
Reputation: 41872
a) It is not an empty list
Your empty container check seems fine (just drop the parentheses)
if not myList:
return False
b) It has more than one integer within the list
This doesn't work, it's a number check not a list length check:
if myList != 1:
return(True)
you may instead want (which also takes care of case 'a'):
if len(myList) < 2:
return False
c) Check if the integers within the list has an even product value or an odd product value by checking for distinct pairs.
This one is tricky as your explanation seems inconsistent with your examples:
For example list1 = [1,2,3]. This would return False because the product is even. list2 = [3,2,3] would return True because the product of the two odd pairs is odd.
Both lists have an even product but you return True
for one and False
for the other.
Your subsequent explanation seems to imply you want to determine parity without multiplying by noting that all odd elements = odd product (True) and any even element = even product (False) which leads to two possible predicates:
def are_all_odd(myList): # odd product True; even product False
return all(element % 2 for element in myList)
def is_any_even(myList): # even product True; odd product False
return any(element % 2 == 0 for element in myList)
But the modulus operator %
is division which is as expensive as multiplication. To avoid that we can do this bit-wise:
def are_all_odd(myList): # odd product True; even product False
return all(element & 1 for element in myList)
def is_any_even(myList): # even product True; odd product False
return any(~element & 1 for element in myList)
Using generators as input to any()
and all()
means these will stop as soon as the answer can be determined. However, the data may not be in the optimal order to minimize the number of tests.
Upvotes: 1