Posts: 13
Threads: 5
Joined: Nov 2018
(Nov-24-2018, 09:30 PM)j.crater Wrote: Hello, in case of "cei" you use "is" instead of "in", as is the case in other elifs elif letter is "cei": This cannot work, because for loop iterates over characters, so no character will equal "cei". How do I get rid of the "e" and "и" after that then?
Is there any way something similar to this code could work?:
elif letter in "Cc" and translation[+2:] == "ei":
translation = translation[:+2] + "си"
Posts: 1,150
Threads: 42
Joined: Sep 2016
I can't think of a robust solution right off the bat. But you might be able to work something out with string's replace method.
Posts: 4,220
Threads: 97
Joined: Sep 2016
First of all, stop making new threads about this problem. I'm tired of you trying to get different answers.
I showed you how to fix the cei problem by looking backwards in the translation. You are trying to do that by looking forward in the translation. That's like looking forward in time. You can't do that. You could look forward in the original string, but your loop is not set up for that.
Posts: 13
Threads: 5
Joined: Nov 2018
(Nov-24-2018, 10:04 PM)ichabod801 Wrote: First of all, stop making new threads about this problem. I'm tired of you trying to get different answers. I showed you how to fix the cei problem by looking backwards in the translation. You are trying to do that by looking forward in the translation. That's like looking forward in time. You can't do that. You could look forward in the original string, but your loop is not set up for that. Your solution did not work. In case you don't see, I copied your exact text and put it into the code. I had to comment it out because it didn't work and I didn't want it getting in the way.
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Nov-24-2018, 10:18 PM)skrivver99 Wrote: Your solution did not work.
Then the correct thing to do would have been to post that my solution didn't work and exactly how it didn't work in the previous thread. Did you use the correction that I posted after finding two different e's in your code? Because I just ran that and it works.
Posts: 13
Threads: 5
Joined: Nov 2018
(Nov-24-2018, 11:15 PM)ichabod801 Wrote: (Nov-24-2018, 10:18 PM)skrivver99 Wrote: Your solution did not work. Then the correct thing to do would have been to post that my solution didn't work and exactly how it didn't work in the previous thread. Did you use the correction that I posted after finding two different e's in your code? Because I just ran that and it works.
I posted the exact code including your correction. This is the code that didn't work. Here it is again:
def translate(phrase):
translation = ""
for letter in phrase:
if letter in "Aa":
translation = translation + "a"
elif letter.lower() in "b":
if letter.isupper():
translation = translation + "Б"
else:
translation = translation + "б"
elif letter in "Ll":
translation = translation + "л"
elif letter in 'Ii' and translation[-2:] == 'ке':
translation = translation[:-2] + "cи"
elif letter in "Cc" and translation[+2:] == "ei":
translation = translation[:+2] + "си"
elif letter in 'Ii':
translation = translation + "и"
elif letter in 'Ii':
translation = translation + "и"
elif letter in "Ii":
translation = translation + "и"
elif letter in "Ee":
translation = translation + "е"
elif letter in "Ff":
translation = translation + "ф"
elif letter in "cei":
translation = translation + "си"
elif letter in "Dd":
translation = translation + "д"
elif letter in "Gg":
translation = translation + "г"
else:
translation = translation + letter
'''
This list
Needs to be completed
'''
return translation
Posts: 4,787
Threads: 76
Joined: Jan 2018
Nov-25-2018, 10:04 PM
(This post was last modified: Nov-25-2018, 10:05 PM by Gribouillis.)
I would simply use a regex for this task. It will be much faster at run time than a loop over the letters and a huge sequence of ifs
import re
regex = re.compile(
r"cei"
r"|[Aa]"
r"|[Bb]"
r"|[Cc]"
r"|[Ll]"
r"|[Ii]"
r"|[Ee]"
r"|[Dd]"
r"|[Gg]"
r"|[Ff]"
)
mapping = {}
def register(*s):
def decorate(func):
for k in s:
mapping[k] = func
return func
return decorate
@register("cei")
def _():
return "си"
@register("A", "a")
def _():
return "a"
@register("B", "b")
def _():
return "б"
@register("C", "c")
def _():
return "к"
@register("L", "l")
def _():
return "л"
@register("I", "i")
def _():
return "и"
@register("E", "e")
def _():
return "е"
@register("D", "d")
def _():
return "д"
@register("G", "g")
def _():
return "г"
@register("F", "f")
def _():
return "ф"
def action(match):
return mapping[match.group(0)]()
def translate(phrase):
return regex.sub(action, phrase)
if __name__ == '__main__':
result = translate('ceiling')
print(result) The fundamental theorem of software engineering Wrote:We can solve any problem by introducing an extra level of indirection.
Posts: 4,220
Threads: 97
Joined: Sep 2016
That's not the code with my correction. This is:
def translate(phrase):
translation = ""
for letter in phrase:
if letter in "Aa":
translation = translation + "a"
elif letter in "Bb":
translation = translation + "б"
elif letter in "Cc":
translation = translation + "к"
elif letter in "Ll":
translation = translation + "л"
elif letter in 'Ii' and translation[-2:] == 'ке':
translation = translation[:-2] + "cи"
elif letter in 'Ii':
translation = translation + "и"
elif letter in "Ee":
translation = translation + "е"
elif letter in "cei":
translation = translation + "си"
else:
translation = translation + letter
return translation
print(translate(input("Enter word: ")))
|