Asked at Valkyrie Trading
Theory: Parsing and Data HygieneRead the problem, hints and solution here. The editor needs a bigger screen: open this page on a laptop to write and run your code.
The coding task on a junior derivatives trader online assessment, as two candidates reported it: "the regex for an email address which might contain an underscore". That was the whole instruction. No function name, no signature, no list of what counts as valid, and one of the two never reached it because the earlier questions took the time. This version states the rule, so the work is the pattern and not the guessing.
Implement is_valid_email(s) returning True when s is a valid address
and False otherwise. A valid address is:
@, with a local part before it and a domain after it.., _ and
+. It starts and ends with a letter or digit, and never has two dots in
a row.-, and does not start or end
with -.Letter case does not matter: [email protected] is valid.
is_valid_email("[email protected]")
# True
is_valid_email("jane [email protected]")
# False (a space)
(is_valid_email("[email protected]"), is_valid_email("trader@example"))
# (True, False)
# an underscore, a plus and four labels are fine; one label is not a domain
s is a str of length at most 320.@, and an accented
letter. There is also a run of 200,000 generated addresses, which is
worth compiling the pattern once for.