PROG0119 - Babel Fish

no tags 

After being forced to leave the earth because of an attack of a hostile extraterrestrial race, you have arrived on another planet. Of course, the aliens that live on this planet — the Gia'Duk — speak a completely different language. This complicates your integration in your new environment. 

Luckily, the extraterrestrials possess an advanced nanotechnology, allowing you to obtain a powerful hearing-aid. The only thing that is missing is a program that translates Gia'Duk words to English. The list of words is already at hand, only the implementation that provides a translation is lacking.

Assignment

Write a function addTranslation that takes three arguments: the first one is a string ( the word to translate ), the second one is a string as well ( the translation of the first argument ) and the third one the dictionary in which you add the translation to ( the first argument becomes a key in the dictionary, the second argument the value ).

Write a function translation that takes two arguments: the first one is a string ( a word you want to translate ) and the second is the translation dictionary. If there is a translation for the word in the dictionary ( it is a key of the dictionary ), the function returns the translation. If the translation is not available, it instead returns '???'.

Example

>>> dictionary = {}
>>> addTranslation('plerzs', 'woman', dictionary)
>>> addTranslation('nirtu', 'flower', dictionary)
>>> addTranslation('klinzoj', 'thirst', dictionary)
>>> addTranslation('tilza', 'dog', dictionary)
>>> addTranslation('zraidi', 'time', dictionary)
>>> dictionary
{'klinzoj': 'thirst', 'zraidi': 'time', 'tilza': 'dog', 'plerzs': 'woman', 'nirtu': 'flower'}

>>> translation('tilza', dictionary)
'dog'
>>> translation('guoles', dictionary)
'???'

Nadat je noodgedwongen de aarde hebt verlaten wegens een aanval van een vijandig buitenaards ras, ben je terechtgekomen op een andere planeet. Uiteraard spreken de buitenaardse wezens die de planeet bewonen — de Gia'Duk — een compleet andere taal. Daardoor slaag je er maar met moeite in om te integreren in je nieuwe leefwereld.

Gelukkig beschikken de buitenaardse wezens over zeer geavanceerde nanotechnologie, wat je toelaat om een krachtig intern gehoorapparaat te bekomen. Het enige wat ontbreekt is een programma dat woorden in de Gia'Duk taal vertaalt naar het Nederlands. De lijst met woorden is reeds voorhanden, dus enkel de implementatie die zorgt voor de vertaling ontbreekt nog.

Opgave

Schrijf een functie vertalingToevoegen die 3 argumenten heeft: het eerste argument is een string ( het woord dat je wilt vertalen ), het tweede is ook een string ( de vertaling van het eerste argument ) en het derde argument is de dictionary waarin je de vertaling wilt aan toevoegen ( dit wil zeggen, voeg het te vertalen woord toe als een sleutel aan de dictionary met als overeenkomende waarde de vertaling van het woord ).

Schrijf een functie vertaling die 2 argumenten heeft: het eerste argument is een string ( het woord dat je wilt vertalen ) en het tweede argument is het woordenboek dat is opgebouwd aan de hand van de functie vertalingToevoegen. Als er een vertaling bestaat in het woordenboek geeft de functie de vertaling terug, anders geeft het de string "???" terug.

Voorbeeld

>>> woordenboek = {}
>>> vertalingToevoegen('plerzs', 'vrouw', woordenboek)
>>> vertalingToevoegen('nirtu', 'bloem', woordenboek)
>>> vertalingToevoegen('klinzoj', 'dorst', woordenboek)
>>> vertalingToevoegen('tilza', 'hond', woordenboek)
>>> vertalingToevoegen('zraidi', 'tijd', woordenboek)
>>> woordenboek
{'klinzoj': 'dorst', 'zraidi': 'tijd', 'tilza': 'hond', 'plerzs': 'vrouw', 'nirtu': 'bloem'}

>>> vertaling('tilza', woordenboek)
'hond'
>>> vertaling('guoles', woordenboek)
'???'


Added by:Peter Dawyndt
Date:2011-08-06
Time limit:15s-20s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None