diff options
Diffstat (limited to 'challenge-330/sgreen/python')
| -rwxr-xr-x | challenge-330/sgreen/python/ch-1.py | 34 | ||||
| -rwxr-xr-x | challenge-330/sgreen/python/ch-2.py | 33 | ||||
| -rwxr-xr-x | challenge-330/sgreen/python/test.py | 21 |
3 files changed, 88 insertions, 0 deletions
diff --git a/challenge-330/sgreen/python/ch-1.py b/challenge-330/sgreen/python/ch-1.py new file mode 100755 index 0000000000..0f8f6ddc2e --- /dev/null +++ b/challenge-330/sgreen/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def clear_digits(input_string) -> str: + """ + Remove all letter/digit pairs from the input string until no more pairs + can be removed. + + :param input_string: The string from which to remove letter/digit pairs. + :return: The modified string with all letter/digit pairs removed. + """ + solution = input_string + + while True: + # See if there are any letter / digit pairs to remove + new_string = re.sub(r'[a-z][0-9]', '', solution) + if new_string == solution: + # No more pairs to remove, rerturn the result + return solution + + # Update the solution with the new string and run the loop again + solution = new_string + + +def main(): + result = clear_digits(sys.argv[1]) + print('"'+result+'"') + + +if __name__ == '__main__': + main() diff --git a/challenge-330/sgreen/python/ch-2.py b/challenge-330/sgreen/python/ch-2.py new file mode 100755 index 0000000000..68050246b8 --- /dev/null +++ b/challenge-330/sgreen/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +import sys + + +def title_capital(input_string: str) -> str: + """ + Convert the input string to title case, capitalizing the first letter of + each word longer than 2 characters, and converting the rest to lowercase. + + :param input_string: The string to convert to title case. + :return: The modified string in title case. + """ + # Convert the input string to lowercase, split it into words + words = input_string.lower().split() + + # Capitalize the first letter of each word longer than 2 characters + new_words = [ + word.capitalize() if len(word) > 2 else word + for word in words + ] + + # Join the words back into a single string with spaces + return ' '.join(new_words) + + +def main(): + result = title_capital(sys.argv[1]) + print('"' + result+'"') + + +if __name__ == '__main__': + main() diff --git a/challenge-330/sgreen/python/test.py b/challenge-330/sgreen/python/test.py new file mode 100755 index 0000000000..89980aa8ca --- /dev/null +++ b/challenge-330/sgreen/python/test.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.clear_digits("cab12"), "c") + self.assertEqual(ch_1.clear_digits("xy99"), "") + self.assertEqual(ch_1.clear_digits("pa1erl"), "perl") + + def test_ch_2(self): + self.assertEqual(ch_2.title_capital("PERL IS gREAT"), "Perl is Great") + self.assertEqual(ch_2.title_capital("THE weekly challenge"), "The Weekly Challenge") + self.assertEqual(ch_2.title_capital("YoU ARE A stAR"), "You Are a Star") + + +if __name__ == '__main__': + unittest.main() |
