diff options
Diffstat (limited to 'challenge-287/lubos-kolouch/python')
| -rw-r--r-- | challenge-287/lubos-kolouch/python/ch-1.py | 59 | ||||
| -rw-r--r-- | challenge-287/lubos-kolouch/python/ch-2.py | 75 |
2 files changed, 134 insertions, 0 deletions
diff --git a/challenge-287/lubos-kolouch/python/ch-1.py b/challenge-287/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..9593c70beb --- /dev/null +++ b/challenge-287/lubos-kolouch/python/ch-1.py @@ -0,0 +1,59 @@ +import unittest + + +def strong_password_steps(s: str) -> int: + """ + Calculates the minimum number of steps required to make the given string a strong password. + + A strong password must: + - Be at least 6 characters long. + - Contain at least one lowercase letter, one uppercase letter, and one digit. + - Not contain three repeating characters in a row. + + Args: + s (str): The input password string. + + Returns: + int: The minimum number of steps required to make the password strong. + """ + import re + missing_types = 3 + if re.search(r'[a-z]', s): + missing_types -= 1 + if re.search(r'[A-Z]', s): + missing_types -= 1 + if re.search(r'\d', s): + missing_types -= 1 + + repeats_to_fix = 0 + i = 2 + n = len(s) + while i < n: + if s[i] == s[i - 1] == s[i - 2]: + repeat_len = 3 + while i + 1 < n and s[i + 1] == s[i]: + repeat_len += 1 + i += 1 + repeats_to_fix += repeat_len // 3 + i += 1 + + if n >= 6: + total_steps = max(missing_types, repeats_to_fix) + else: + total_steps = max(missing_types + repeats_to_fix, 6 - n) + return total_steps + + +# Unit Tests +class TestStrongPassword(unittest.TestCase): + + def test_examples(self): + self.assertEqual(strong_password_steps("a"), 5, 'Example 1') + self.assertEqual(strong_password_steps("aB2"), 3, 'Example 2') + self.assertEqual(strong_password_steps("PaaSW0rd"), 0, 'Example 3') + self.assertEqual(strong_password_steps("Paaasw0rd"), 1, 'Example 4') + self.assertEqual(strong_password_steps("aaaaa"), 3, 'Example 5') + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-287/lubos-kolouch/python/ch-2.py b/challenge-287/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..f302b7360a --- /dev/null +++ b/challenge-287/lubos-kolouch/python/ch-2.py @@ -0,0 +1,75 @@ +import re +from typing import Any +import unittest + + +def is_valid_number(s: str) -> bool: + """ + Determines if the given string is a valid number based on specific rules. + + A valid number is defined as: + - An integer number followed by an optional exponent. + - A decimal number followed by an optional exponent. + + An integer number is defined with an optional sign '-' or '+' followed by digits. + + Decimal Number: + - An optional sign '-' or '+' followed by one of the following: + - Digits followed by a dot '.'. + - Digits followed by a dot '.' followed by digits. + - A dot '.' followed by digits. + + Exponent: + - An exponent is defined with an exponent notation 'e' or 'E' followed by an integer number. + + Args: + s (str): The input string. + + Returns: + bool: True if the string is a valid number, False otherwise. + """ + pattern = r""" + ^ # Start of string + [+-]? # Optional sign + ( + ( # Decimal numbers + \d+\.\d* # Digits followed by dot and optional digits + | + \.\d+ # Dot followed by digits + | + \d+\. # Digits followed by dot + ) + | + \d+ # Integer numbers + ) + ([eE][+-]?\d+)? # Optional exponent + $ # End of string + """ + regex = re.compile(pattern, re.VERBOSE) + return bool(regex.match(s)) + + +# Unit Tests +class TestValidNumber(unittest.TestCase): + + def test_examples(self): + self.assertEqual(is_valid_number("1"), True, 'Example 1') + self.assertEqual(is_valid_number("a"), False, 'Example 2') + self.assertEqual(is_valid_number("."), False, 'Example 3') + self.assertEqual(is_valid_number("1.2e4.2"), False, 'Example 4') + self.assertEqual(is_valid_number("-1."), True, 'Example 5') + self.assertEqual(is_valid_number("+1E-8"), True, 'Example 6') + self.assertEqual(is_valid_number(".44"), True, 'Example 7') + + def test_additional(self): + self.assertEqual(is_valid_number("6e6.5"), False, + 'Exponent with decimal') + self.assertEqual(is_valid_number(""), False, 'Empty string') + self.assertEqual(is_valid_number("123"), True, 'Integer number') + self.assertEqual(is_valid_number("-."), False, + 'Negative dot without digits') + self.assertEqual(is_valid_number("1e"), False, 'Incomplete exponent') + + +if __name__ == "__main__": + unittest.main() |
