diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-16 03:10:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-16 03:10:45 +0100 |
| commit | b91d56518d12647f8227fb8313b13b8544efaebd (patch) | |
| tree | 57d1441f309501ee2690f1313e5d3fdee39ec040 | |
| parent | 33e769177db792d141f888c0d79a884292bc9bd9 (diff) | |
| parent | 03b3de5ea0837a70ff825bc272a8e4ecec93d2ee (diff) | |
| download | perlweeklychallenge-club-b91d56518d12647f8227fb8313b13b8544efaebd.tar.gz perlweeklychallenge-club-b91d56518d12647f8227fb8313b13b8544efaebd.tar.bz2 perlweeklychallenge-club-b91d56518d12647f8227fb8313b13b8544efaebd.zip | |
Merge pull request #7894 from manfredi/challenge-212
Python Solution for Task #1
| -rwxr-xr-x | challenge-212/manfredi/python/ch-1.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-212/manfredi/python/ch-1.py b/challenge-212/manfredi/python/ch-1.py new file mode 100755 index 0000000000..2491158c2a --- /dev/null +++ b/challenge-212/manfredi/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# Python 3.9.2 on Debian GNU/Linux 11 (bullseye) + +print('challenge-212-task1') + +# Task 1: Jumping Letters +# You are given a word having alphabetic characters only, and a list of positive integers of the same length +# Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. +# The given list would have exactly the number as the total alphabets in the given word. + +def jumping_letters(word: str, jump: list[int]) -> None: + lower = [ c.islower() for c in word ] + ascii = [ ord(c.upper()) for c in word ] + ascii_ord = [ (o + jump[i]) if (o + jump[i]) < ord('Z') else ((o + jump[i]) + ord('A') - 1 ) % (ord('Z')) for i, o in enumerate(ascii) ] + res = ''.join([ str(chr(c)).lower() if lower[i] else str(chr(c)) for i, c in enumerate(ascii_ord)]) + print(f"{word}, '{jump}': {res}") + +def main(): + word, jump = 'Perl', [2, 22, 19, 9] + jumping_letters(word, jump) + word, jump = 'Raku', [24, 4, 7, 17] + jumping_letters(word, jump) + +if __name__ == '__main__': + main() |
