And different inputs may have different edges. I grabbed a posted solution just to check what number I was aiming for just to have it also give a wrong answer for my input.
If you wrap it in paranthesis, you can apply line breaks and indentation for better readability. E.g. your code slightly more readable:
if ( (len(check_passport[0])== 4 and int(check_passport[0])>1919 and int(check_passport[0])<2003)
and (len(check_passport[1]==4) and int(check_passport[1])>2009 and int(check_passport[1])<2021)
and (len(check_passport[2]==4) and int(check_passport[2])>2019 and int(check_passport[2])<2031)
and (check_passport[4][0]=="#" and len(check_passport[4]) == 7
and check_passport[4][1] in hair_couler
and check_passport[4][2] in hair_couler
and check_passport[4][3] in hair_couler
and check_passport[4][4] in hair_couler
and check_passport[4][5] in hair_couler)
and check_passport[6][1] in hair_couler
and check_passport[5] in eye_couler and len(check_passport[5]) == 3
and len(check_passport[6]) == 9 and int(check_passport[6]) < 1000000000
and (("cm" in check_passport[3] and 149 <= int(check_passport[3].strip('cm')) <= 194)
or ("in" in check_passport[3] and 149 <= int(check_passport[3].strip('cm')) <= 194))
):
result += 1
print(check_passport)
Small comment, but your use of 'strip' would allow incorrect inputs to go undetected. For instance, if the "hgt" field was '180cmmmmmmmm' all of the c's and m's would be stripped, giving you the number.
I don't think that any of the data were wrong in this way but I have been tripped up by 'strip' before.
Yes, in python 3.9 there is a new string method 'removesuffix', as well as 'removeprefix', for cases like this. With python <=3.8, you should identify that the suffix is what you intend and then slice the string.
Murphy's law tells me that if I were to type this, I would skip out a bracket somewhere and get a 'mismatched parenthesis' error... somewhere in line 28...
Did you know that in Python you can do 1919 < int(check_passport[0]) < 2003? It has special syntax for chained comparisons, so unlike most other languages, you don't end up comparing (True|False) < 2003.
Also <= instead of < and > so you can just use the numbers from the assignment instead of having to add/subtract 1.
var validation = new Dictionary<string, Func<string, bool>>
{
//...
["hgt"] = s =>
Regex.Match(s, @"^\s*(\d+)\s*(cm|in)\s*$") is { Success: true, Groups: var grp } &&
(int.Parse(grp[1].Value), grp[2].Value) is ( >= 150 and <= 193, "cm") or ( >= 59 and <= 76, "in"),
//...
};
I prefer to save the bools as variables with readable variable names. You lose the short-circuiting that would otherwise be applied, but it makes it just so much easier to understand what is going on. Here is my solution for reference:
def is_valid_passport_policy2(passport):
required_fields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
# Check that all required fields are there
if not all(field in passport for field in required_fields):
return False
hgt_match = re.match(r'^(\d+)(cm|in)$', passport['hgt'])
if hgt_match:
hgt, unit = hgt_match.groups()
hgt_min, hgt_max = {'cm': (150, 193), 'in': (59, 76)}[unit]
is_valid_hgt = hgt_match is not None and (hgt_min <= int(hgt) <= hgt_max)
is_valid_byr = 1920 <= int(passport['byr']) <= 2002
is_valid_iyr = 2010 <= int(passport['iyr']) <= 2020
is_valid_eyr = 2020 <= int(passport['eyr']) <= 2030
is_valid_hcl = bool(re.match(r'^#[0-9a-f]{6}$', passport['hcl']))
is_valid_ecl = bool(re.match(r'^(amb|blu|brn|gry|grn|hzl|oth)$', passport['ecl']))
is_valid_pid = bool(re.match(r'^\d{9}$', passport['pid']))
return all([
is_valid_hgt, is_valid_byr, is_valid_iyr, is_valid_eyr,
is_valid_hcl, is_valid_ecl, is_valid_pid
])
This is when you really appreciate Python doing the functions-are-objects paradigm.
I just created a dictionary (hashmap) from property name (byr, hcl, etc.) to functions that returns True if the conditions for this property is met. For (cid), I just return True.
7
u/Sourish17 Dec 04 '20
Ahhhh....
And I thought MY if statements were long ;)
Nice one, though!
2
u/[deleted] Dec 04 '20
But what if there was a passport number < 0? ;)
51
u/[deleted] Dec 04 '20
This is the worst part of it.
26
u/[deleted] Dec 04 '20
The virgin
if <super_long_statement>:vs the chadcond = <super_long_statement>; if cond:7
u/Tjaja Dec 04 '20
If you wrap it in paranthesis, you can apply line breaks and indentation for better readability. E.g. your code slightly more readable:
12
u/Weak_Pea_2878 Dec 04 '20
Please ask me next time you copy pasta my code ;) jk
1
u/KazeTheSpeedDemon Dec 04 '20
If it works it works!
1
u/fuckdominiccummings Dec 05 '20
Small comment, but your use of 'strip' would allow incorrect inputs to go undetected. For instance, if the "hgt" field was '180cmmmmmmmm' all of the c's and m's would be stripped, giving you the number.
I don't think that any of the data were wrong in this way but I have been tripped up by 'strip' before.
1
u/[deleted] Dec 06 '20
Excuse me! What's the name of the text font?(got you an award, you deserve it, I didn't know what to do with it anyway)
3
u/djavaman Dec 05 '20
Advent of Obfuscation
2
u/KlaireOverwood Dec 04 '20
Thanks, I hate it.
2
u/CCC_037 Dec 05 '20
Murphy's law tells me that if I were to type this, I would skip out a bracket somewhere and get a 'mismatched parenthesis' error... somewhere in line 28...
1
u/euidzero Dec 04 '20
https://www.reactiongifs.com/r/eyes.gif
1
u/Pat_The_Hat Dec 05 '20
My if statements were bad but not wrapping 8 times bad
1
u/MattieShoes Dec 05 '20
... that's basically what I did, other than height because I didn't want to figure out a regex for that.
1
u/thomastc Dec 05 '20
Did you know that in Python you can do
1919 < int(check_passport[0]) < 2003? It has special syntax for chained comparisons, so unlike most other languages, you don't end up comparing(True|False) < 2003.Also
<=instead of<and>so you can just use the numbers from the assignment instead of having to add/subtract 1.1
u/MizardX Dec 05 '20
I like the new C# patterns:
1
u/Mulvad4ever Dec 05 '20
I prefer to save the bools as variables with readable variable names. You lose the short-circuiting that would otherwise be applied, but it makes it just so much easier to understand what is going on. Here is my solution for reference:
1
u/qse81 Dec 05 '20
This is significantly worse than mine. Thank you
1
u/kuki126 Dec 05 '20
So that's why my prof insisted on using functions for my program
1
u/liangyiliang Dec 06 '20
This is when you really appreciate Python doing the functions-are-objects paradigm.
I just created a dictionary (hashmap) from property name (byr, hcl, etc.) to functions that returns True if the conditions for this property is met. For (cid), I just return True.
1
u/[deleted] Dec 09 '20
If it’s stupid but it works, it ain’t stupid
1
u/[deleted] Dec 04 '20
[removed] — view removed comment