From 9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 16 Nov 2025 23:13:00 +1000 Subject: sgreen solutions to challenge 347 --- challenge-347/sgreen/python/ch-2.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 challenge-347/sgreen/python/ch-2.py (limited to 'challenge-347/sgreen/python/ch-2.py') diff --git a/challenge-347/sgreen/python/ch-2.py b/challenge-347/sgreen/python/ch-2.py new file mode 100755 index 0000000000..776a289e1d --- /dev/null +++ b/challenge-347/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def format_phone(input_string: str) -> str: + # Strip all non-digit characters + input_string = re.sub(r'\D', '', input_string) + + parts = [] + while input_string: + # Decide length of next part + l = 2 if len(input_string) == 4 else min(3, len(input_string)) + parts.append(input_string[:l]) + input_string = input_string[l:] + + return '-'.join(parts) + + +def main(): + result = format_phone(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() -- cgit