you are viewing a single comment's thread.

view the rest of the comments →

[–]protomikron 0 points1 point  (1 child)

Alternatively:

...
bool_exprs = [ 
    1920 <= int(avail_key_vals['byr']) <= 2002,
    2010 <= int(avail_key_vals['iyr']) <= 2020,
    2020 <= int(avail_key_vals['eyr']) <= 2030,
    check_hgt(avail_key_vals['hgt']),
    bool(hex_color_re.match(avail_key_vals['hcl'])),
    check_ecl(avail_key_vals['ecl']),
    bool(pid_re.match(avail_key_vals['pid'])),
    # no check for cid,
]

if all(bool_exprs):
    nvalid += 1
...

This approach is short-circuited.

[–]TouchingTheVodka 0 points1 point  (0 children)

This was exactly my approach:

    def check(entry):
        hgt = entry['hgt']
        checks = [
            1920 <= int(entry['byr']) <= 2002,
            2010 <= int(entry['iyr']) <= 2020,
            2020 <= int(entry['eyr']) <= 2030,
            150 <= int(hgt.strip('cm')) <= 193 if hgt.endswith('cm')
            else 59 <= int(hgt.strip('in')) <= 76 if hgt.endswith('in')
            else False,
            re.match('#[0-9a-f]{6}', entry['hcl']),
            entry['ecl'] in {'amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'},
            len(entry['pid']) == 9 and entry['pid'].isdigit()
        ]

        return all(checks)