-1

i recently made a test in university and the question was like this-"ask a name,and remove the accents from it and print it"(there was more to the question but this was the main problem for me). My teacher didn´t allow us to use string or list methods, just the basic things literally(if, while loop, for loop, arrays),the only methods that we where allowed to use were the find() , index() and len().

So for example the name José needed to be printed has Jose, or José Magalhães should be Jose Magalhaes

If you could say a way to to this with the basic things i would be appreciated. thank you.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Miguel TV
  • 3
  • 1
  • 2

2 Answers2

1

The translate function is usually the fastest for this given that it's a one to one character mapping:

normalMap = {'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A',
             'à': 'a', 'á': 'a', 'â': 'a', 'ã': 'a', 'ä': 'a', 'ª': 'A',
             'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E',
             'è': 'e', 'é': 'e', 'ê': 'e', 'ë': 'e',
             'Í': 'I', 'Ì': 'I', 'Î': 'I', 'Ï': 'I',
             'í': 'i', 'ì': 'i', 'î': 'i', 'ï': 'i',
             'Ò': 'O', 'Ó': 'O', 'Ô': 'O', 'Õ': 'O', 'Ö': 'O',
             'ò': 'o', 'ó': 'o', 'ô': 'o', 'õ': 'o', 'ö': 'o', 'º': 'O',
             'Ù': 'U', 'Ú': 'U', 'Û': 'U', 'Ü': 'U',
             'ù': 'u', 'ú': 'u', 'û': 'u', 'ü': 'u',
             'Ñ': 'N', 'ñ': 'n',
             'Ç': 'C', 'ç': 'c',
             '§': 'S',  '³': '3', '²': '2', '¹': '1'}
normalize = str.maketrans(normalMap)

output:

print("José Magalhães ".translate(normalize))
# Jose Magalhaes 

If you're not allowed to use methods of str, you can still use the dictionary:

S = "José Magalhães "
print(*(normalMap.get(c,c) for c in S),sep="")
# Jose Magalhaes

If you're not allowed to use a dictionary, you can use two strings to map the characters and loop through the string:

fromChar = "ÀÁÂÃÄàáâãäªÈÉÊËèéêëÍÌÎÏíìîïÒÓÔÕÖòóôõöºÙÚÛÜùúûüÑñÇç§³²¹"
toChar   = "AAAAAaaaaaAEEEEeeeeIIIIiiiiOOOOOoooooOUUUUuuuuNnCcS321"

S = "José Magalhães "
R = ""                                 # start result with an empty string
for c in S:                            # go through characters
    p = fromChar.find(c)               # find position of accented character
    R += toChar[p] if p>=0 else c      # use replacement or original character

print(R)
# Jose Magalhaes
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0
text_normalizing_map = {'À': 'A',
        'Á': 'A',
        'Â': 'A',
        'Ã': 'A',
        'Ä': 'A',
        'È': 'E',
        'É': 'E',
        'Ê': 'E',
        'Ë': 'E',
        'Í': 'I',
        'Ì': 'I',
        'Î': 'I',
        'Ï': 'I',
        'Ù': 'U',
        'Ú': 'U',
        'Û': 'U',
        'Ü': 'U',
        'Ò': 'O',
        'Ó': 'O',
        'Ô': 'O',
        'Õ': 'O',
        'Ö': 'O',
        'Ñ': 'N',
        'Ç': 'C',
        'ª': 'A',
        'º': 'O',
        '§': 'S',
        '³': '3',
        '²': '2',
        '¹': '1',
        'à': 'a',
        'á': 'a',
        'â': 'a',
        'ã': 'a',
        'ä': 'a',
        'è': 'e',
        'é': 'e',
        'ê': 'e',
        'ë': 'e',
        'í': 'i',
        'ì': 'i',
        'î': 'i',
        'ï': 'i',
        'ù': 'u',
        'ú': 'u',
        'û': 'u',
        'ü': 'u',
        'ò': 'o',
        'ó': 'o',
        'ô': 'o',
        'õ': 'o',
        'ö': 'o',
        'ñ': 'n',
        'ç': 'c'}

def normalize(text):
    list_text = list(text)
    for index, i in enumerate(list_text):
        val = text_normalizing_map.get(i)
        if(val):
            list_text[index] = val
    return "".join(list_text)

print(normalize("José Magalhães "))

output

Jose Magalhaes 
Epsi95
  • 8,832
  • 1
  • 16
  • 34