diff --git a/exercises/ex07/ex07b.py b/exercises/ex07/ex07b.py new file mode 100644 index 0000000..eb27349 --- /dev/null +++ b/exercises/ex07/ex07b.py @@ -0,0 +1,39 @@ +""" +Project: Date Detection +Page 186 +""" + +import re +import datetime + +tests = """ + Sí 1/1/1911 + Sí 02/1/2000 + Sí 12/12/2012 + No 123/1/1911 + No 1/123/1911 + No 1/1/11 + Inválido 32/12/1212 + Inválido 3/13/1212 + Inválido 3/12/3000 + Inválido 3/12/00 + Inválido 29/02/2023 +""" + + +def find_date(string): + rgx = re.compile(r"\b(\d{1,2})/(\d{1,2})/(\d{4})") + finds = rgx.findall(string) + dates = [] + for find in finds: + d, m, y = int(find[0]), int(find[1]), int(find[2]) + if y > 999 and y < 3000: + try: + dates.append(datetime.date(y, m, d).isoformat()) + except Exception: + pass + return dates + + +dates = find_date(tests) +print(dates) diff --git a/exercises/ex07/ex07c.py b/exercises/ex07/ex07c.py new file mode 100644 index 0000000..afc1045 --- /dev/null +++ b/exercises/ex07/ex07c.py @@ -0,0 +1,32 @@ +""" +Project: Strong Password Detection +Page 186 +""" + +import re + +tests = [ + "", + "a", + "aaaaaaaa", + "AAAAAAAA", + "11111111", + "aAaAaAaA", + "a1a1a1a1", + "A1A1A1A1", + "aA1aA1aA1", +] + + +def check_pass(string): + withupper = True if re.match(r".*[A-Z]", string) else False + withlower = True if re.match(r".*[a-z]", string) else False + withdigit = True if re.match(r".*\d", string) else False + if len(string) >= 8 and withupper and withlower and withdigit: + print(f"'{string}' is a strong pass") + else: + print(f"'{string}' is NOT a strong pass") + + +for test in tests: + check_pass(test) diff --git a/exercises/ex07/ex07d.py b/exercises/ex07/ex07d.py new file mode 100644 index 0000000..ae7f723 --- /dev/null +++ b/exercises/ex07/ex07d.py @@ -0,0 +1,25 @@ +""" +Project: Regex Version of the strip() Method +Page 186 +""" + +import re + +tests = { + "Hola": None, + " Hola ": None, + "bHola": "b", + "\tHola,\tmundo\t": "\t", +} + + +def rgx_strip(string, char=" "): + string = re.sub(r"^" + char + r"+", "", string) + return re.sub(char + r"+$", "", string) + + +for key, val in tests.items(): + if val is None: + print(f"'{key}' -> '{rgx_strip(key)}'") + else: + print(f"'{key}' -> '{rgx_strip(key, val)}'")