From fe00ecfb7362652b3d450000cd099f4044775cfd Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 17 Nov 2024 20:40:19 +1100 Subject: sgreen solutions to challenge 295 --- challenge-295/sgreen/python/ch-1.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 challenge-295/sgreen/python/ch-1.py (limited to 'challenge-295/sgreen/python/ch-1.py') 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() -- cgit