Translation of the string into "robbery language"

I am trying to practice for my upcoming practice test on programming, so he looked online for examples and came across this.

"Write a translate () function that translates the text into" rövarspråket "(Swedish for" predatory language "), ie doubles each consonant and puts an" o "between them. For example, translate (" it's fun ") should return a string "tothohisos isos fofunon".

I don’t know why, but I struggle a lot with this. So simple, but still hard for me. Here is what I tried

def translate(n):
    consonant="bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
    for letters in n:
        if letters in consonant:
            return (letters+"O"+letters)
        else:
            return("Vowel")

Sorry if my coding is super-amateur. Just trying to find out: /

+4
source share
6
consonants = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")
original_string = "this is fun"
translated = ''.join(map(lambda x: x+"0"+x if x in consonants else x,original_string)
print translated

- , ... + 0 + , ,

+4
    mytext = 'thisisfun'
consonants = 'bcdfghjklmnpqrstuvwxyz'
newtext = []
def translate(mytext):
    for i in mytext:
        if i not in consonants:
            newtext.append(i)
        else:
            newtext.append(i)
            newtext.append('o')
            newtext.append(i)
    print(newtext)

translate(mytext)

, , ... , - , ... , - - 10 !

+1

return - . . . , return . , .

- , , :

def translate(n):
    consonants = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")
    for letter in n:
        if letter in consonants:
            yield '{}o{}'.format(letter,letter)
        else:
            yield letter

''.join(translate('this is fun'))
Out[44]: 'tothohisos isos fofunon'

, .

... . , , . , , , :

from string import ascii_letters
def translate(s):
    consonants = set(ascii_letters) - set('aeiouAEIOU')
    def _mapper(c):
        return '{}o{}'.format(c,c) if c in consonants else c
    return ''.join(map(_mapper, s))
+1

, return , - .

, return :

def translate(n):
    consonants = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")
    ret_str = ""
    for letter in n:
        if letter in consonants:
            ret_str += letter + ("o" if letter.islower() else "O") + letter
        else:
            ret_str += letter
    return ret_str

str.islower(), , "o" .

set , , , . , , , , ...

"o", , :

''.join([ s + "o" + s if s in consonant else s for s in n ])

[ ] - , . str.join() .

, :

''.join([ s+"o"+s if s.islower() else s+"O"+s if s in consonant else s for s in n ])
+1

my knowledge of python is very short, but I think that the author forgot to write an empty string, and add this to the letters, after the loop ...

def traslate():
  a = raw_input("Please give a sentence: ")
  b = ""
  for i in a:
     b += i
     if i in "bcdfghjklmnpqrstvwxyz":
           b += "o" + i
  print "You answer translate into 'robber' language: " + b

traslate(n)

Maybe you like it ....

def translate(n):
    b = ""
    consonant = "bcdfghjklmnpqrstvwxyz"
    for letters in n: 
          b += letters           # you add first time the consonant
          if letters in consonant:
                b += "0" + letters  #you add 0 and seconds consonant
          else:
              return ("Vowel")

print translate ("this is funk")
+1
source
consonants = 'bcdfghjklmnpqrstuvwxyz'
def translate(mytext):
    newtext = ""
    for i in mytext:
        if i in consonants:
            newtext+=i+'o'+i                
        else:
            newtext+=i
    return newtext
#Enter any text by using mytext variable
mytext = input("Enter Text")
translatedText=translate(mytext)
print("After translation the text is:",translatedText)
-1
source

Source: https://habr.com/ru/post/1540422/


All Articles