0

My task here is to write a function called fix_yz. This function takes a single argument which will be a string. My function returns this string with all of the ys and zs swapped, and all of the Ys and Zs swapped. What i have done:

replace = {"Y":"Z","y":"z","Z":"Y","z":"y"}
def fix_yz(Input):
   replaced=[]
   for yzYZ in Input:
     if yzYZ in replace:
        yzYZ = replace[replaced]
     replaced.append(yzYZ)
  return ''.join(replaced)

although i get a error and i dont understand which line to work on.

Utsav
  • 145
  • 1
  • 13

4 Answers4

3

This does exactly what you need:

translation_table = str.maketrans({"Y":"Z","y":"z","Z":"Y","z":"y"})

def fix_yz(input_):
    return input_.translate(translation_table)

You create a translation table, and then translate everything using it. It is VERY fast regarding execution time, and a very simple solution.

Bharel
  • 23,672
  • 5
  • 40
  • 80
  • 1
    This does not work for me. The keys of the the table dictionary have to be ordinals, as mentioned in the [documentation](https://docs.python.org/3/library/stdtypes.html#str.translate) and here: https://stackoverflow.com/questions/17020661/why-doesnt-str-translate-work-in-python-3 – dron22 Mar 23 '16 at 23:59
  • The OP wanted the code for Python 3. Maketrans+translate does exactly that. (I updated the question to add maketrans) – Bharel Mar 24 '16 at 00:06
1

Your code has two errors: an indentation error on the return statement, and a wrong variable. You try to index the dictionary by a list.

Fixed version:

replace = {"Y":"Z","y":"z","Z":"Y","z":"y"}
def fix_yz(Input):
  replaced=[]
  for yzYZ in Input:
    if yzYZ in replace:
      yzYZ = replace[yzYZ]
    replaced.append(yzYZ)
  return ''.join(replaced)
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
0

As already pointed out you simply used the wrong variable in your code, you are trying to lookup using [replaced] which is a list not yzYZ which is each character in your string:

replace = {"Y":"Z","y":"z","Z":"Y","z":"y"}
def fix_yz(Input):
   replaced = []
   for yzYZ in Input:
     if yzYZ in replace:
        yzYZ = replace[yzYZ] # replace[yzYZ]
     replaced.append(yzYZ)
   return ''.join(replaced)

You can also simplify your own logic using dict.get using each character as the default value if the char is not in your replace dict:

def fix_yz(inp):
   return ''.join([replace.get(ch, ch) for ch in inp])

If you were to use str.translate you need to use the ord of each char for your dict mapping:

replace = {ord("Y"): "Z", ord("y"): "z", ord("Z"): "Y", ord("z"): "y"}

def fix_yz(inp):
    return inp.translate(replace)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

In Python3 you can do

def fix_yz(s):
    return s.translate(str.maketrans("yYzY", "zZyY"))

or

table = {ord("Y"):"Z",ord("y"):"z",ord("Z"):"Y",ord("z"):"y"}
def fix_yz(s):
    return s.translate(table)

in < Python3 it would be

from string import maketrans

table = maketrans("yYzY", "zZyY")

def fix_yz(s):
    return s.translate(table)
dron22
  • 1,235
  • 10
  • 20