diff options
| author | Simon Green <mail@simon.green> | 2025-11-16 23:13:00 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-11-16 23:13:00 +1000 |
| commit | 9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7 (patch) | |
| tree | 5e34e38b2e0eedd4a615f76fe9a1fa5151a98d41 /challenge-347/sgreen/python/ch-2.py | |
| parent | d945cc3d48b1284d49aba765341514707f7bee50 (diff) | |
| download | perlweeklychallenge-club-9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7.tar.gz perlweeklychallenge-club-9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7.tar.bz2 perlweeklychallenge-club-9e07c2fbb9f9eae0a3ea4a904281372e8cd996b7.zip | |
sgreen solutions to challenge 347
Diffstat (limited to 'challenge-347/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-347/sgreen/python/ch-2.py | 27 |
1 files changed, 27 insertions, 0 deletions
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() |
