Problem:
Extraordinary Girl (II) She loves e-mail so much! She sends e-mails by her cellular phone to
her friends when she has breakfast, she talks with other friends, and even
when she works in the library! Her cellular phone has somewhat simple
layout (Figure 1). Pushing button 1 once displays a character (’), pushing   it twice in series displays a character (,), and so on, and pushing it 6 times
displays (’) again. Button 2 corresponds to charaters (abcABC), and, for
example, pushing it four times displays (A). Button 3-9 have is similar to
button 1. Button 0 is a special button: pushing it once make her possible
to input characters in the same button in series. For example, she has to
push “20202” to display “aaa” and “660666” to display “no”. In addition,
pushing button 0 n times in series (n > 1) displays n − 1 spaces. She never
pushes button 0 at the very beginning of her input. Here are some examples
of her input and output: 666660666 --> No
44444416003334446633111 --> I’m fine.
20202202000333003330333 --> aaba f ff One day, the chief librarian of the library got very angry with her and
hacked her cellular phone when she went to the second floor of the library
to return books in shelves. Now her cellular phone can only display button
numbers she pushes. Your task is to write a program to convert the sequence
of button numbers into correct characters and help her continue her e-mails! Input Input consists of several lines. Each line contains the sequence of button
numbers without any spaces. You may assume one line contains no more
than 10000 numbers. Input terminates with EOF. Output For each line of input, output the corresponding sequence of characters in
one line. Sample Input 666660666
44444416003334446633111
20202202000333003330333 Output for the Sample Input No
I'm fine.
aaba  f ff

Solution (Python):
alpha = ["","',.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]
while 1:
    try:
        ans = ""
        sers = ""
        c = 0
        for key in raw_input():
            if key == "0":
                if len(sers) > 0:
                    n,l = int(sers[0]),len(sers)
                    l = (l-1)%len(alpha[n])
                    ans += alpha[n][l]
                    sers = ""
                else:
                    ans += " "
            elif len(sers) == 0 or sers[-1] == key:
                sers += key
            else:
                n,l = int(sers[0]),len(sers)
                l = (l-1)%len(alpha[n])
                ans += alpha[n][l]
                sers = key
        if len(sers) > 0:
            if sers[0] == 0:
                ans += " "*len(sers)
            else:
                n,l = int(sers[0]),len(sers)
                l = (l-1)%len(alpha[n])
                ans += alpha[n][l]
        print ans
    except:
        break

Solution (Python):
ref = ["",
       "',.!?",
       "abcABC",
       "defDEF",
       "ghiGHI",
       "jklJKL",
       "mnoMNO",
       "pqrsPQRS",
       "tuvTUV",
       "wxyzWXYZ"]

def inp(s):
    n = int(s[0])
    p = (len(s)-1)%len(ref[n])
    return ref[n][p]

while 1:
    try:
        pushes = raw_input()
    except:
        break
    ans = ""
    s = ""
    for push in pushes:
        l = len(s)
        if push == "0":
            if l > 0: ans += inp(s)
            else: ans += " "
            s = ""
        elif l == 0 or s[-1] == push:
            s += push
        else:
            ans += inp(s)
            s = push
    if l > 0:
        ans += " "*l if s[0] == "0" else inp(s)
    print ans

Solution (Python):
ref = ["",
       "',.!?",
       "abcABC",
       "defDEF",
       "ghiGHI",
       "jklJKL",
       "mnoMNO",
       "pqrsPQRS",
       "tuvTUV",
       "wxyzWXYZ"]

def inp(s):
    n = int(s[0])
    p = (len(s)-1)%len(ref[n])
    return ref[n][p]

while 1:
    try:
        pushes = raw_input()
    except:
        break
    ans = ""
    s = ""
    for push in pushes:
        l = len(s)
        if push == "0":
            if l > 0: ans += inp(s)
            else: ans += " "
            s = ""
        elif l == 0 or s[-1] == push:
            s += push
        else:
            ans += inp(s)
            s = push
    if l > 0:
        ans += " "*l if s[0] == "0" else inp(s)
    print ans

Solution (Python):
def getchar(button,num):
    if button=='0':
        return ' '*(num-1)
    elif button=='1':
        num=(num-1)%5
        return "',.!?"[num]
    elif button=='2':
        num=(num-1)%6
        return "abcABC"[num]
    elif button=='3':
        num=(num-1)%6
        return "defDEF"[num]
    elif button=='4':
        num=(num-1)%6
        return "ghiGHI"[num]
    elif button=='5':
        num=(num-1)%6
        return "jklJKL"[num]
    elif button=='6':
        num=(num-1)%6
        return "mnoMNO"[num]
    elif button=='7':
        num=(num-1)%8
        return "pqrsPQRS"[num]
    elif button=='8':
        num=(num-1)%6
        return "tuvTUV"[num]
    elif button=='9':
        num=(num-1)%8
        return "wxyzWXYZ"[num]



while(1):
    try:
        keyin=raw_input()
    except:
        break
    oldb='0'
    num=1
    ans=''
    for k in keyin:
        if oldb!=k:
            ans=ans+getchar(oldb,num)
            num=1
        else:
            num=num+1
        oldb=k
    ans=ans+getchar(oldb,num)
    print ans

Solution (Python):
a=[['\'',',','.','!','?'],
   ['a','b','c','A','B','C'],
   ['d','e','f','D','E','F'],
   ['g','h','i','G','H','I'],
   ['j','k','l','J','K','L'],
   ['m','n','o','M','N','O'],
   ['p','q','r','s','P','Q','R','S'],
   ['t','u','v','T','U','V'],
   ['w','x','y','z','W','X','Y','Z']]
while 1:
    try:b=input()+'@'
    except:break
    c=0
    ans=''
    for i in range(len(b)-1):
        if b[i]!=b[i+1]:
            if b[i]!='0':
                d=int(b[i])-1
                ans+=a[d][c%len(a[d])]
            c=0
        else: c+=1
        if b[i]==b[i+1]=='0':ans+=' '
    print(ans)

Solution (Python):
import fileinput

chr_master = [
    ' ', '\',.!?', 'abcABC', 'defDEF', 'ghiGHI',
    'jklJKL', 'mnoMNO', 'pqrsPQRS', 'tuvTUV', 'wxyzWXYZ'
]

for s in (line.strip() for line in fileinput.input()):
    s += '!'
    prev_c = None
    cnt = 0
    chr_list = []
    for c in s:
        if c == prev_c:
            cnt += 1
            continue
        if prev_c == '0':
            if cnt > 1:
                chr_list.extend([(0, 0)] * (cnt - 1))
        else:
            if prev_c is not None:
                chr_list.append((int(prev_c), cnt - 1))

        prev_c = c
        cnt = 1
    print(''.join(chr_master[i][j] for i, j in chr_list))

Solution (Python):
import fileinput

chr_master = [
    ' ', '\',.!?', 'abcABC', 'defDEF', 'ghiGHI',
    'jklJKL', 'mnoMNO', 'pqrsPQRS', 'tuvTUV', 'wxyzWXYZ'
]

for s in (line.strip() for line in fileinput.input()):
    s += '!'
    prev_c = None
    cnt = 0
    ans = ''
    for c in s:
        if c == prev_c:
            cnt += 1
            continue
        if prev_c == '0':
            if cnt > 1:
                ans += ' ' * (cnt - 1)
        else:
            if prev_c is not None:
                ans += chr_master[int(prev_c)][cnt - 1]

        prev_c = c
        cnt = 1
    print(ans)

Solution (Python):
import fileinput

chr_master = [' ', '\',.!?', 'abcABC', 'defDEF', 'ghiGHI', 'jklJKL', 'mnoMNO', 'pqrsPQRS', 'tuvTUV', 'wxyzWXYZ']

for s in (line.strip() for line in fileinput.input()):
    s += '!'
    prev_c, cnt = 0, 1
    ans = ''
    for c in s:
        if c == prev_c:
            cnt += 1
            continue
        if prev_c == '0':
            if cnt > 1:
                ans += ' ' * (cnt - 1)
        else:
            ans += chr_master[int(prev_c)][cnt - 1]
        prev_c, cnt = c, 1
    print(ans[1:])

Solution (Python):



tbl = ["", "',.!?", "abcABC", "defDEF", "ghiGHI", "jklJKL", \
       "mnoMNO", "pqrsPQRS", "tuvTUV", "wxyzWXYZ"]

while True:
	try: s = input().strip()
	except: break
	ans, i = '', 0
	while i < len(s):
		c = s[i]
		w, d, i = 0, int(c), i+1
		while i < len(s) and s[i] == c: i, w = i+1, w+1
		if d == 0: ans += ' '*w
		else: ans += tbl[d][w%len(tbl[d])]
	print(ans)

Solution (Python):
Button={
"1":"',.!?",
"2":"abcABC",
"3":"defDEF",
"4":"ghiGHI",
"5":"jklJKL",
"6":"mnoMNO",
"7":"pqrsPQRS",
"8":"tuvTUV",
"9":"wxyz",
"0":" "}

while True:
    try:
        sent=raw_input()
    except EOFError:
        break
    init=sent[0]
    cnt=1
    ans=[]
    for c in sent[1:]:
        if c==init:
            cnt+=1
        elif init=="0" and c!="0":
            init=c
            ans.append(" "*(cnt-1))
            cnt=1
        else:
            ans.append(Button[init][cnt-1])
            init=c
            cnt=1
    else:
        ans.append(Button[init][cnt-1])
    print "".join(ans)

Solution (Python):

import sys


def decode_keitai(str):

    
    keitai_keys = [" ","',.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]

    
    results = []

    
    inputs = str.split("\n")

    
    for input in inputs:

        input = input.replace("\n","")
        result = "" 
        
        s_before = ""

        
        while len(input) > 0:

            
            s = input[0]
            input = input[1:]

            
            if s_before == "":
                s_before = s
                continue

            
            if not s in s_before:
                if s_before[0] == "0":
                    result += s_before[:-1].replace("0"," ")
                else:
                    id = int(s_before[0])
                    keylen = len(keitai_keys[id])
                    result += keitai_keys[id][(len(s_before)-1)%keylen]
                s_before = s
            else:
                s_before += s
            

        
        if len(s_before) > 0:
            if s_before[0] == "0":
                result += s_before[:-1].replace("0"," ")
            else:
                id = int(s_before[0])
                keylen = len(keitai_keys[id])
                result += keitai_keys[id][(len(s_before)-1)%keylen]
            s_before = ""

        results.append(result)

    return "\n".join(results)

if __name__=="__main__":
    lines = []
    for line in sys.stdin:
        lines.append(line.replace("\n",""))
    print decode_keitai("\n".join(lines))

Solution (Python):
T = [
    None,
    "',.!?",
    "abcABC",
    "defDEF",
    "ghiGHI",
    "jklJKL",
    "mnoMNO",
    "pqrsPQRS",
    "tuvTUV",
    "wxyzWXYZ",
]
try:
    while 1:
        ans = []
        *S, = map(int, input())
        S.append(0)
        prv = 0
        cur = -1
        for c in S:
            if c == prv:
                if c == 0:
                    ans.append(" ")
                    cur = 0
                else:
                    cur += 1
            else:
                if prv != 0:
                    t = T[int(prv)]
                    ans.append(t[cur%len(t)])
                cur = 0
            prv = c
        print(*ans, sep='')
except EOFError:
    ...

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

S = ["", "',.!?", "abcABC", "defDEF", "ghiGHI", 
"jklJKL", "mnoMNO", "pqrsPQRS", "tuvTUV", "wxyzWXYZ"]

for line in sys.stdin:
    ans = ''
    k = 0
    cnt = 0
    line = list(line)
    line[-1] = "-1"
    for c in line:
        if int(c) == k:
            cnt += 1
        else:
            if k == 0:
                ans += ' ' * cnt
            else:
                ans += S[k][cnt % len(S[k])]
            cnt = 0
            k = int(c)
    print ans