Complete a function called normalize() that replaces specific non-ASCII characters in a…
Private research annotation
Sign in to add a private research label or note.
- User
Complete a function called normalize() that replaces specific non-ASCII characters in a given string with their ASCII equivalents.
Here are the lists of the letters and the dictionary with the replacements:
characters = ["é", "ë", "á", "å", "œ", "æ"] replacements = ["e", "e", "a", "aa", "oe", "ae"] map_char_to_rep = {"é": "e", "ë": "e", "á": "a", "å": "aa", "œ": "oe", "æ": "ae"}
Complete the code below so that the function normalize() takes a string with non-ASCII characters and returns a string where all of them have been replaced with the equivalents.
Note that variables characters, replacements, and map_char_to_rep are not included in the code, and should be copied manually to be used in the solution.
Sample Input 1:
Charlotte Brontë
Sample Output 1:
Charlotte Bronte
Hint by avatar Jose A Lobato If you create a dictionary with all substitution then you can iterate over the dictionary. Was this hint helpful? Write a program in Python 3 Report a typo AI Code Editor IDE100 ⌄ 1 2 3 4 5 6 7 8 9 name = input()
def normalize(passed_name):
code# put your code here return new_nameprint(normalize(name))
- Assistant
To complete the
normalizefunction, you can use the predefined lists and dictionary to systematically replace each non-ASCII character in the input string with its ASCII equivalent. Here's how you can implement the function:python - User
Error: Traceback (most recent call last): File "/sandbox/main.py", line 17, in print(normalize(name)) ^^^^^^^^^^^^^^^ File "/sandbox/main.py", line 10, in normalize if char in map_char_to_rep: ^^^^^^^^^^^^^^^ NameError: name 'map_char_to_rep' is not defined
- Assistant
The error message indicates that the
map_char_to_repdictionary is not defined within the scope of your code. To fix this, ensure that the definitions forcharacters,replacements, andmap_char_to_repare included in your code before you use them in thenormalizefunction.Here's the corrected code including all necessary definitions:
python