PROG0167 - Mobile phone

no tags 

The alphabet consists of 26 characters, while the keyboard of a mobile phone or a GPS usually has a lot less keys. In order to easily type words on such appliances, a so-called MultiTap technique was originally used. The principle is simple: on each key, three or more letters are portrayed (2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PQRS, 8=TUV, 9=WXYZ). To obtain the right letter, you have to press the key on which that letter occurs once or more, depending of the position of that letter on the key.

multitab_keyboard
multitab_keyboard

Suppose for example you want to type the letter A. You have to press key 2 one time (portrayed as "2"). However, if you wish to type a B, you must press that key twice shortly after eachother (portrayed as "22"). To type two letters that are on the same key after eachother, the user must insert a short pause after typing the first of both letters. We will use a space to represent such a pause. This way "2 2" represents AA, while "22" gives B.

We will stick to simple messages that only consist from the letters (a-z) and spaces. Spaces are obtained by pressing the key 0. "the" is coded as "84433", "it" is coded as "4448", the text "foo  bar" (with two spaces) is coded as "333666 6660022 2777", and the text "introduction informatics" is coded as "4446687776663882228444666 660444663336667776284442227777". Note that a pause does not need to be inserted for two consecutive spaces, in other words: no space is needed between two consecutive zeroes (see underlined fragment).

Assignment

  1. Declare a global variable keys as follows:

    keys = {0:' ', 2:'ABC', 3:'DEF', 4:'GHI', 5:'JKL', 
               6:'MNO', 7:'PQRS', 8:'TUV', 9:'WXYZ'}
    
  2. Use the variable keys optimally to write a function letter2digits, to which a letter or a space must be given as an argument. This function must print the MultiTap sequence as a result that corresponds with the given letter. For example, the function should print the string 22 for the letter B, and for a given space, the function should print a string that consists solely of the digit 0. The function should be able to work with both uppercase and lowercase letters. Try to minimize the amount of conditions that the function must test. 

  3. Us the variable keys optimally to write a function digits2letter, to which a MultiTap sequence must be given as a string argument. This function must print the corresponding uppercase letter or space. For example, the function must print capital F for the sequence 333, and a space for the digit 0. Try to minimize the number of conditions that need to be tested for this function.

  4. Use the function letter2digits to write a function codeMultiTap, that converts a given text to the corresponding MultiTap sequence. For example, the function must print "4446687776663882228444666 660444663336667776284442227777" for the given text "introduction informatics".

  5. Use the function digits2letter to write a function decodeMultiTap, that converts a given MultiTap sequence to the corresponding text, in which all letters are uppercase letters. For example, the function must print "INTRODUCTION INFORMATICS" for the given string "4446687776663882228444666 660444663336667776284442227777".

Voorbeeld

>>> letter2digits('B')
'22'
>>> digits2letter('333')
'F'
>>> codeMultiTap('introduction informatics')
'4446687776663882228444666 660444663336667776284442227777'
>>> decodeMultiTap('4446687776663882228444666 660444663336667776284442227777')
'INTRODUCTION INFORMATICS'

Het alfabet bestaat uit 26 karakters, terwijl het toetsenbord van een mobiele telefoon of een GPS doorgaans veel minder toetsen bevat. Om op dergelijke toestellen toch makkelijk woorden te kunnen intoetsen, werd oorspronkelijk gebruik gemaakt van de zogenaamde MultiTap techniek. Het principe is eenvoudig: op elke toets worden drie of meer letters afgebeeld (2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PQRS, 8=TUV, 9=WXYZ). Om de gewenste letter te krijgen, moet je de toets waarop de letter wordt afgebeeld één of meer keer indrukken, afhankelijk van de positie van de letter op de toets.

multitab_keyboard
multitab_keyboard

Stel dat je bijvoorbeeld de letter A wilt typen. Dan moet je één keer op toets 2 drukken (voorgesteld als "2"). Wil je echter een B typen, dan moet je twee keer kort na elkaar op toets 2 drukken (voorgesteld als "22"). Om na elkaar twee letters te typen die op dezelfde toets worden afgebeeld, moet de gebruiker een korte pauze inlassen alvorens dezelfde toets opnieuw in te drukken. We zullen een spatie gebruiken om een dergelijke pauze voor te stellen. Op die manier staat "2 2" voor AA, terwijl "22" staat voor B.

We houden het bij eenvoudige boodschappen die enkel bestaan uit letters (a-z) en spaties. Spaties worden ingegeven door op toets 0 te drukken. Het woord "de" wordt dan gecodeerd als "3 33", het woord "het" wordt gecodeerd als "44338", de tekst "foo  bar" (met twee spaties) wordt gecodeerd als "333666 6660022 2777", en de tekst "inleiding informatica" wordt gecodeerd als "44466555334443444664044466333666777628444222 2". Merk dus op dat tussen twee opeenvolgende spaties geen pauze moet ingelast worden, of met andere woorden dat tussen twee opeenvolgende nullen geen spatie moet geplaatst worden (zie onderlijnd fragment).

Opgave

  1. Declareer als volgt een globale variabele toetsen

    toetsen = {0:' ', 2:'ABC', 3:'DEF', 4:'GHI', 5:'JKL', 
               6:'MNO', 7:'PQRS', 8:'TUV', 9:'WXYZ'}
    
  2. Maak optimaal gebruik van de variabele toetsen om een functie letter2cijfers te schrijven, waaraan een letter of een spatie als argument kan doorgegeven worden. Deze functie moet als resultaat de MultiTap cijferreeks teruggeven die correspondeert met de gegeven letter. Zo moet de functie voor de letter B de string 22 teruggeven, en moet de functie voor een gegeven spatie een string die enkel bestaat uit het cijfer 0 teruggeven. De functie moet zowel met hoofdletters als kleine letters kunnen werken. Probeer het aantal voorwaarden dat deze functie moet testen zo klein mogelijk te houden.

  3. Maak optimaal gebruik van de variabele toetsen om een functie cijfers2letter te schrijven, waaraan een MultiTap cijferreeks als stringargument moet doorgegeven worden. Deze functie moet als resultaat de corresponderende hoofdletter of een spatie teruggeven. Zo moet de functie voor de cijferreeks 333 de hoofdletter F teruggeven, en moet de functie voor het cijfer 0 een spatie teruggeven. Probeer het aantal voorwaarden dat deze functie moet testen zo klein mogelijk te houden.

  4. Gebruik de functie letter2cijfers om een functie codeerMultiTap te schrijven, die een gegeven tekst omzet naar de corresponderende MultiTap cijferreeks. Zo moet de functie als resultaat de string  "44466555334443444664044466333666777628444222 2" teruggeven voor de gegeven tekst "inleiding informatica".

  5. Gebruik de functie cijfers2letter om een functie decodeerMultiTap te schrijven, die een gegeven MultiTap cijferreeks omzet naar de corresponderende tekst, waarin alle letters hoofdletters zijn. Zo moet de functie als resultaat de tekst "INLEIDING INFORMATICA" teruggeven voor de gegeven string "44466555334443444664044466333666777628444222 2".

Voorbeeld

>>> letter2cijfers('B')
'22'
>>> cijfers2letter('333')
'F'
>>> codeerMultiTap('inleiding informatica')
'44466555334443444664044466333666777628444222 2'
>>> decodeerMultiTap('44466555334443444664044466333666777628444222 2')
'INLEIDING INFORMATICA'


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