IQ Test

CodeWars上的IQ Test题目

Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number.

! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the elements start from 1 (not 0)

##Examples :

iq_test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even

iq_test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd


def iq_test(numbers):
    #find the different one among the numbers,even or odd
    od = 0
    ev = 0
    index = 0
    for i in range(0, len(numbers)):
        if (int(numbers[i]) % 2 == 0):
            ev = ev +1
        else:
            od = od + 1

    if (ev > od):
        for i in range(0, len(numbers)):
            if (int(numbers[i]) % 2 == 1):
                index = i + 1
            else:
                continue

    if(od > ev):
        for i in range(0, len(numbers)):
            if (int(numbers[i]) % 2 == 0):
                index = i + 1
            else:
                continue
    print("The index of the different number is {}".format(index))

num = [2, 2, 1, 2]
iq_test(num)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值