Part 1
Checking whether string conform to some arbitrary rules seems like a job for regexes.re.search("[aeiou].*[aeiou].*[aeiou].*", string_to_check)
The second rule is to check for a character to appear twice in a row. For this, we use the regex capture group (.) to get a single character and check if it appears right after again with \\1 . So together:
re.search("(.)\\1", string_to_check)
The third rule excludes strings with specific substrings, so similar to the first rule we just manually check for the simple existance of these substring:
re.search("(ab)|(cd)|(pq)|(xy)", string_to_check)
Once we have these three regexes, I wrote simple functions just to check if the string containst this regex. The examples provided were used a sanity check. Afterwards we iterate over all lines in the input and count the string fulfilling all rules.
Part 2
re.search("(..).*\\1", string_to_check)
re.search("(.).\\1", string_to_check)
For the input we again iterate over all lines and count the number of strings fulfilling both rules.
No comments:
Post a Comment