Exercice 6 #8
DominiqueMakowski
started this conversation in
Show and tell
Replies: 6 comments
-
x= "Hello World"
def encode(s):
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 = ""
for letter in s:
if letter == " ":
sentence += " "
elif letter == "z":
sentence += "a"
else:
sentence += alphabet[alphabet.index(letter)+1]
return sentence |
Beta Was this translation helpful? Give feedback.
0 replies
-
def transform_string(string):
""" Transform each letter into the next letter in the alphabet.
Other symbols/spaces remain the same, and the last letter of the alphabet becomes the first letter.
Updated to print as one string. """
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 + 1]
except IndexError:
new_string += alphabet[0]
else:
new_string += l
return new_string |
Beta Was this translation helpful? Give feedback.
0 replies
-
import string
def shift_word_byone(word):
alphabet = list(string.ascii_lowercase)
word = word.lower()
output = ""
for iword, vword in enumerate(word):
# Edge case
if iword == 0:
iword = len(alphabet) + 1
previous_ndx = alphabet.index(vword) - 1
output += alphabet[previous_ndx]
return output
shift_word_byone("Hello") |
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 encode(x):
x = x.lower()
word = ""
for letter in x:
if letter == "z":
word += "a"
else:
word += alph[alph.index(letter)+1]
return word
encode("hello") |
Beta Was this translation helpful? Give feedback.
0 replies
-
def enc (word):
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)
letter = ind + 1
final += alphabet[letter]
return final |
Beta Was this translation helpful? Give feedback.
0 replies
-
def encode(x):
"""Function that takes a number as an input, and prints the
Fibonacci sequence (0, 1, 1, 2, 3, 5, …) up until that number."""
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 = ""
for letter in x:
if letter == "z":
sentence += "a"
elif letter == " ":
sentence += " "
else:
sentence += alphabet[alphabet.index(letter)+1]
return sentence
encode("good morning") |
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.
-
Create an encoder that changes each letter of a sentence into the next letter from alphabetical order
Post your solution as a comment to the discussion
Expected:
Hint: your function might include:
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"]Beta Was this translation helpful? Give feedback.
All reactions