diff options
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() |
