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'