Exercice 7 #9
DominiqueMakowski
started this conversation in
Show and tell
Replies: 6 comments 1 reply
-
def transform_string(string, encode):
""" Transform each letter into the next or previous letter in the alphabet, dpending on if encode is true or false. """
if encode == True:
trans = 1
end = 0
else:
trans = -1
end = -1
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
new_string = ''
for l in string:
if l.lower() in alphabet:
try:
i = alphabet.index(l.lower())
new_string += alphabet[i + trans]
except IndexError:
new_string += alphabet[end]
else:
new_string += l
return new_string |
Beta Was this translation helpful? Give feedback.
1 reply
-
def decoder(s, encode=False):
s = s.lower()
alphabet = ["a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
"r", "s", "t", "u", "v", "w", "x", "y", "z"]
sentence = ""
if encode==False:
for letter in s:
if letter == " ":
sentence += " "
elif letter == "a":
sentence += "z"
else:
sentence += alphabet[alphabet.index(letter)-1]
return sentence
else:
return s |
Beta Was this translation helpful? Give feedback.
0 replies
-
import string
def shift_word_byone(word, encode=True):
alphabet = list(string.ascii_lowercase)
output = ""
if encode:
shift = -1
else:
shift = 1
for iword, vword in enumerate(word.lower()):
# Edge case
if iword == 0 & shift == -1:
iword = len(alphabet) + 1
elif iword == len(alphabet) & shift == 1:
iword = -1
previous_ndx = alphabet.index(vword) + shift
output += alphabet[previous_ndx]
return output
shift_word_byone("ifmmp", encode=True) |
Beta Was this translation helpful? Give feedback.
0 replies
-
def enc_3 (word, dec):
final = ''
alphabet = ["a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
"r", "s", "t", "u", "v", "w", "x", "y", "z"]
for j in word:
if j == 'z':
let = 'a'
final += let
else:
ind = alphabet.index(j)
if dec == True:
letter = ind + 1
else:
letter = ind - 1
final += alphabet[letter]
return final |
Beta Was this translation helpful? Give feedback.
0 replies
-
def decoder(x, encode=False):
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
x = x.lower()
sentence = ""
if encode is True:
for letter in x:
if letter == "z":
sentence += "a"
elif letter == " ":
sentence += " "
else:
sentence += alphabet[alphabet.index(letter)+1]
if encode is False:
for letter in x:
if letter == "a":
sentence += "z"
elif letter == " ":
sentence += " "
else:
sentence += alphabet[alphabet.index(letter)-1]
return sentence
decoder("hppe npsojoh", encode=False) |
Beta Was this translation helpful? Give feedback.
0 replies
-
alph= ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
def decoder(x, encode=True):
x = x.lower()
word = ""
if encode==False:
for l in x:
if l == "a":
word += "z"
else:
word += alph[alph.index(l)-1]
return word
else:
return x
decoder("hello") |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Add a decoder feature
Post your solution as a comment to the discussion
Expected:
Beta Was this translation helpful? Give feedback.
All reactions