diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-14 00:21:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-14 00:21:48 +0100 |
| commit | 062e6e3d6c270ec0aff32155db45e90f0824edba (patch) | |
| tree | 8e9aa4787b2a02313d8d1eddc6a55ffe3ba64f77 | |
| parent | aa1a1b092e8a2607da7d13a9b8d3d398e0d083e8 (diff) | |
| parent | 864c708aecefc7d2acd14f07a4f86b4a9c2e8082 (diff) | |
| download | perlweeklychallenge-club-062e6e3d6c270ec0aff32155db45e90f0824edba.tar.gz perlweeklychallenge-club-062e6e3d6c270ec0aff32155db45e90f0824edba.tar.bz2 perlweeklychallenge-club-062e6e3d6c270ec0aff32155db45e90f0824edba.zip | |
Merge pull request #12330 from simongreen-net/master
sgreen solutions to challenge 329
| -rw-r--r-- | challenge-329/sgreen/README.md | 4 | ||||
| -rwxr-xr-x | challenge-329/sgreen/python/ch-1.py | 28 | ||||
| -rwxr-xr-x | challenge-329/sgreen/python/ch-2.py | 38 | ||||
| -rwxr-xr-x | challenge-329/sgreen/python/test.py | 21 |
4 files changed, 89 insertions, 2 deletions
diff --git a/challenge-329/sgreen/README.md b/challenge-329/sgreen/README.md index 57261d9d9b..f06345033b 100644 --- a/challenge-329/sgreen/README.md +++ b/challenge-329/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 328 +# The Weekly Challenge 329 -Blog: [A good question](https://dev.to/simongreennet/weekly-challenge-a-good-question-3e3p) +No blog this week, sorry. diff --git a/challenge-329/sgreen/python/ch-1.py b/challenge-329/sgreen/python/ch-1.py new file mode 100755 index 0000000000..2ea1973cdb --- /dev/null +++ b/challenge-329/sgreen/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def counter_integers(input_string: str) -> list[int]: + # Find all integers in string + m = re.findall(r"\d+", input_string) + + # Convert to integers + solution = [] + + for i in map(int, m): + # Add them if they don't already appear + if i not in solution: + solution.append(i) + + return solution + + +def main(): + result = counter_integers(sys.argv[1]) + print(', '.join(map(str, result))) + + +if __name__ == '__main__': + main() diff --git a/challenge-329/sgreen/python/ch-2.py b/challenge-329/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a06379efe2 --- /dev/null +++ b/challenge-329/sgreen/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import sys + +def is_nice_string(s: str) -> bool: + # Check that every letter has a matching letter in the opposite case + for letter in s: + if letter.islower() and letter.upper() not in s: + return False + if letter.isupper() and letter.lower() not in s: + return False + return True + + +def nice_string(input_string: str) -> str|None: + solution = None + # Calculate every combination of starting and ending positions + for start in range(len(input_string)-1): + for end in range(start+2, len(input_string)+1): + # See if this is a nice string + extract = input_string[start:end] + if is_nice_string(extract): + # If it is longer than the current nice string, update the solution variable + if solution is None or len(solution) < len(extract): + solution = extract + + return solution + + +def main(): + result = nice_string(sys.argv[1]) + if result is None: + result = '' + print('"'+result+'"') + + +if __name__ == '__main__': + main() diff --git a/challenge-329/sgreen/python/test.py b/challenge-329/sgreen/python/test.py new file mode 100755 index 0000000000..4fa937d456 --- /dev/null +++ b/challenge-329/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.counter_integers("the1weekly2challenge2"), [1, 2]) + self.assertEqual(ch_1.counter_integers("go21od1lu5c7k"), [21, 1, 5, 7]) + self.assertEqual(ch_1.counter_integers("4p3e2r1l"), [4, 3, 2, 1]) + + def test_ch_2(self): + self.assertEqual(ch_2.nice_string("YaaAho"), "aaA") + self.assertEqual(ch_2.nice_string("cC"), "cC") + self.assertEqual(ch_2.nice_string("A"), None) + + +if __name__ == "__main__": + unittest.main() |
