简体   繁体   中英

IQ test function in Python not working as intended

I'm having trouble with this code below.

My task is to create a function, that among the given numbers finds one that is different in evenness, and returns a position of that number. The numbers are given as a string. So far I have managed to convert the string into an integer list, then using for loop to iterate through each number.

The problem I'm encountering is that I've managed to return only the position of an odd number among the even numbers, and I can't continue on with the code for vice versa action, because it only returns the position of the odd number.

Here is the code:

def iq_test(numbers):
    # Splitting the "numbers" string
    num_split = numbers.split()
    # converting the splitted strings into int
    num_map = map(int, num_split)
    # converting the object into list
    list_num = list(num_map)
    for n in list_num:
        if not n%2 == 0:
            return list_num.index(n) + 1

Your problem is, that you are assuming, that you are searching for the first even number. What you have to do, is to first decide, what you are searching for. You could for example simply first count the number of even numbers. If it is one, then you are looking for an even number, otherwise, you are looking for an odd. As you don't care for the actual numbers, I would map all of them to their value mod 2 as so:

num_map = list(map(lambda x: int(x) % 2, num_split))

Then, the rest is simple. For example like this:

def iq_test(numbers):
    # Splitting the "numbers" string
    num_split = numbers.split()
    # converting the splitted strings into even (0) or odd (1)
    num_map = list(map(lambda x: int(x) % 2, num_split))
    # return the correct position based on if even or odd is in search
    evens = num_map.count(0)
    if evens == 1:
        return num_map.index(0) + 1
    else:
        return num_map.index(1) + 1

I came up with a similar and a little bit shorter solution

def iq_test(numbers):
    # first check what im looking for "even" or "odd",  map a lambda function that basically does it for me, using the numbers argument as a list type and afterwards cast it into a list so i can iterate later on
    num_map = list(map(lambda x: 'e' if int(x) % 2 == 0 else 'o', numbers.split()))
# search for even numbers  numbers
if num_map.count('e') == 1:
    return num_map.index('e') + 1
# search for odd numbers numbers
return num_map.index('o') + 1
def iq_test(numbers):
    # Splitting the "numbers" string
    num_split = numbers.split()
    # converting the splitted strings into int
    num_map = map(int, num_split)
    # converting the object into list
    list_num = list(num_map)
    for n in list_num:
        if not n%2 == 0:
            return list_num.index(n) + 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM