Created
April 15, 2020 15:45
-
-
Save robertlugg/49e2ab7ba1b95fc9e514987a8e4b479c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fixed version. | |
# Minimal changes to get it working | |
my_string = "This is a bunch of words!" | |
print(f'{my_string}') | |
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
outputStr = '' | |
for i in my_string: | |
for idx, j in enumerate(alphabet): | |
if i == j: | |
outputStr += alphabet[(idx + 13) % len(alphabet)] | |
break | |
else: | |
outputStr += i | |
print(f'{outputStr}') | |
# My attempt at the best way: | |
import string | |
all_letters = string.ascii_letters | |
output_list = list() | |
for this_letter in my_string: | |
if this_letter in all_letters: | |
idx = all_letters.find(this_letter) | |
idx = (idx + 13) % len(all_letters) | |
output_list.append(all_letters[idx]) | |
else: | |
output_list.append(this_letter) | |
output_string = ''.join(output_list) | |
print(f'{output_string}') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment