mardi 5 janvier 2021

function tests haven't gone as expected(part of AoC day4)

I wrote a function that checks if data is correct. Requirements are as follows:

byr-(Birth Year) - four digits; at least 1920 and at most 2002.

iyr (Issue Year) - four digits; at least 2010 and at most 2020.

eyr (Expiration Year) - four digits; at least 2020 and at most 2030.

def check_byr_iyr_eyr(line):
    statement = True
    if line[:3] == "byr":
        if (len(line[line.index(':')+1:]) != 4 or
        1920 > int(line[line.index(':')+1:]) > 2002 ):
            statement = False
    elif line[:3] == "iyr":
        if (len(line[line.index(':')+1:]) != 4 or
        2010 > int(line[line.index(':')+1:]) > 2020 ):
            statement = False
    elif line[:3] == "eyr":
        if (len(line[line.index(':')+1:]) != 4 or
        2020 > int(line[line.index(':')+1:]) > 2030 ):
            statement = False
    return statement


list = ['byr:1919', 'iyr:2010', 'eyr:2021', 'iyr:2019', 'iyr:1933',
        'byr:1946', 'iyr:1919', 'eyr:2005']

for i in list:
    print(check_byr_iyr_eyr(i))

'''
    expected result:
        False
        True
        True
        True
        False
        True
        False
        False
'''

and results of checking provided samples should be like in that multi-line comment "expected results", but unfortunately a result is always True.

I don't know what I'am doing wrong - conditions seems good to me...

Please help, thanks from advance.

Aucun commentaire:

Enregistrer un commentaire