aboutsummaryrefslogtreecommitdiff
path: root/challenge-275/sgreen/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-275/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-275/sgreen/python/ch-2.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-275/sgreen/python/ch-2.py b/challenge-275/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..ec4fb291d8
--- /dev/null
+++ b/challenge-275/sgreen/python/ch-2.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+
+import string
+import sys
+
+
+def replace_digits(s: str) -> str:
+ """
+ Replaces digits in a string with corresponding letters from the alphabet.
+
+ Args:
+ s (str): The input string.
+
+ Returns:
+ str: The modified string with digits replaced by letters.
+ """
+
+ current_letter = None
+ solution = ''
+ alphabet = string.ascii_lowercase
+
+ for char in s:
+ if char.isdigit():
+ if current_letter is None:
+ raise ValueError('The first number must follow a letter.')
+ solution += alphabet[(alphabet.index(current_letter) +
+ int(char)) % 26]
+ else:
+ solution += char
+ current_letter = char
+
+ return solution
+
+
+def main():
+ result = replace_digits(sys.argv[1])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()