aboutsummaryrefslogtreecommitdiff
path: root/challenge-275/sgreen/python/ch-2.py
blob: ec4fb291d8d1b9be412d27e62839b5b7a1d4f4c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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()