diff options
| author | dcw <d.white@imperial.ac.uk> | 2022-11-20 20:38:56 +0000 |
|---|---|---|
| committer | dcw <d.white@imperial.ac.uk> | 2022-11-20 20:38:56 +0000 |
| commit | 3956a95b61015a8abb5aab29015ea85e594877e0 (patch) | |
| tree | 699b2a0c2181d7c50510c33954598e640187e54d /challenge-190/sgreen/python/ch-2.py | |
| parent | e2232acaf7deca62a2551afe032cdef96055cae5 (diff) | |
| parent | bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56 (diff) | |
| download | perlweeklychallenge-club-3956a95b61015a8abb5aab29015ea85e594877e0.tar.gz perlweeklychallenge-club-3956a95b61015a8abb5aab29015ea85e594877e0.tar.bz2 perlweeklychallenge-club-3956a95b61015a8abb5aab29015ea85e594877e0.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-190/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-190/sgreen/python/ch-2.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-190/sgreen/python/ch-2.py b/challenge-190/sgreen/python/ch-2.py new file mode 100755 index 0000000000..02835441b3 --- /dev/null +++ b/challenge-190/sgreen/python/ch-2.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import string +import sys + + +def main(s): + '''Main function''' + + # Loop through the alphabet + letter_map = {} + for k, v in enumerate(string.ascii_uppercase): + letter_map[str(k+1)] = v + + # Record completed solutions + solutions = [] + + # Start with the input string + stack = [(s, '')] + + while stack: + (numbers, letters) = stack.pop(0) + + if numbers == '': + # If we have no numbers for this stack, it is a solution + solutions.append(letters) + else: + # Consider either the first single or two numbers + for l in (1, 2): + if len(numbers) >= l and numbers[:l] in letter_map: + # If we have a mapping, add to the stack list with the + # remaining numbers, and appending this letter to the + # letters + stack.append( + (numbers[l:], letters + letter_map[numbers[:l]])) + + # Print the solutions + if solutions: + print(*sorted(solutions), sep=', ') + else: + print('No solution possible') + + +if __name__ == '__main__': + main(sys.argv[1]) |
