diff options
| author | Simon Green <mail@simon.green> | 2024-11-17 20:40:19 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-11-17 20:40:19 +1100 |
| commit | fe00ecfb7362652b3d450000cd099f4044775cfd (patch) | |
| tree | ce353e375954d457e1fa26069104aaf436b57a5a /challenge-295/sgreen/python | |
| parent | 7ce82fc7f6821082ec19358e597a3e4843c7ab51 (diff) | |
| download | perlweeklychallenge-club-fe00ecfb7362652b3d450000cd099f4044775cfd.tar.gz perlweeklychallenge-club-fe00ecfb7362652b3d450000cd099f4044775cfd.tar.bz2 perlweeklychallenge-club-fe00ecfb7362652b3d450000cd099f4044775cfd.zip | |
sgreen solutions to challenge 295
Diffstat (limited to 'challenge-295/sgreen/python')
| -rwxr-xr-x | challenge-295/sgreen/python/ch-1.py | 22 | ||||
| -rwxr-xr-x | challenge-295/sgreen/python/ch-2.py | 34 | ||||
| -rwxr-xr-x | challenge-295/sgreen/python/test.py | 23 |
3 files changed, 79 insertions, 0 deletions
diff --git a/challenge-295/sgreen/python/ch-1.py b/challenge-295/sgreen/python/ch-1.py new file mode 100755 index 0000000000..bed823f512 --- /dev/null +++ b/challenge-295/sgreen/python/ch-1.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def word_break(s: str, words: list) -> bool: + # Turn the words into a regular expression + pattern = '^(' + '|'.join(map(re.escape, words)) + ')+$' + + # Check if the string matches the pattern + return True if re.search(pattern, s) else False + + +def main(): + # The first value is the string, the rest are the words + result = word_break(sys.argv[1], sys.argv[2:]) + print('true' if result else 'false') + + +if __name__ == '__main__': + main() diff --git a/challenge-295/sgreen/python/ch-2.py b/challenge-295/sgreen/python/ch-2.py new file mode 100755 index 0000000000..5aa6999eb9 --- /dev/null +++ b/challenge-295/sgreen/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import sys + + +def jump_game(ints: list, moves=1) -> int: + # Calculate the minimum number of moves from this point + min_moves = None + for i in range(ints[0], 0, -1): + # We can complete the jump with this move + if i >= len(ints)-1: + return moves + + # Skip if we can't move forward + if ints[i] == 0: + continue + + # Call the function again jumping 'i' positions + m = jump_game(ints[i:], moves+1) + if m is not None and (min_moves is None or min_moves > m): + min_moves = m + + return min_moves + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = jump_game(array) + print(-1 if result is None else result) + + +if __name__ == '__main__': + main() diff --git a/challenge-295/sgreen/python/test.py b/challenge-295/sgreen/python/test.py new file mode 100755 index 0000000000..33117a6d85 --- /dev/null +++ b/challenge-295/sgreen/python/test.py @@ -0,0 +1,23 @@ +#!/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.assertTrue(ch_1.word_break( + 'weeklychallenge', ["challenge", "weekly"])) + self.assertTrue(ch_1.word_break('perlrakuperl', ["raku", "perl"])) + self.assertFalse(ch_1.word_break( + 'sonsanddaughters', ["sons", "sand", "daughters"])) + + def test_ch_2(self): + self.assertEqual(ch_2.jump_game([2, 3, 1, 1, 4]), 2) + self.assertEqual(ch_2.jump_game([2, 3, 0, 4]), 2) + self.assertEqual(ch_2.jump_game([2, 0, 0, 4]), None) + + +if __name__ == '__main__': + unittest.main() |
