Created
January 29, 2011 15:41
-
-
Save tuxes/801927 to your computer and use it in GitHub Desktop.
[SOLVED] https://br.spoj.pl/problems/ENCOTEL/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# -*- encoding:utf-8 -*- | |
import string | |
class Encotel(object): | |
def get_number(self, x): | |
"Retorna os números equivalentes aos caracteres" | |
saida = '' | |
mapping = self.get_mapping() | |
for s in x: | |
if s >= 'A' and s <= 'Z': | |
saida += str(mapping.get(s)) | |
elif s =='0' or s =='1': | |
saida += s | |
else: | |
saida += s | |
return saida | |
def get_mapping(self): | |
"Retorna o mapeamento dos caracteres x números" | |
mapping = {} | |
for i in string.ascii_uppercase: | |
count = string.ascii_uppercase.index(i) | |
if count >= 25: #z | |
count -= 2 | |
elif count >= 18: | |
count -= 1 | |
count /= 3 | |
count += 2 | |
mapping[i] = count | |
return mapping |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding:utf-8 -*- | |
import string | |
""" | |
versão para o spoj-br | |
""" | |
class Encotel(object): | |
def get_number(self, x): | |
"Retorna os números equivalentes aos caracteres" | |
saida = '' | |
mapping = self.get_mapping() | |
for s in x: | |
if s >= 'A' and s <= 'Z': | |
saida += str(mapping.get(s)) | |
elif s =='0' or s =='1': | |
saida += s | |
else: | |
saida += s | |
return saida | |
def get_mapping(self): | |
"Retorna o mapeamento dos caracteres x números" | |
mapping = {} | |
for i in string.ascii_uppercase: | |
count = string.ascii_uppercase.index(i) | |
if count >= 25: #z | |
count -= 2 | |
elif count >= 18: | |
count -= 1 | |
count /= 3 | |
count += 2 | |
mapping[i] = count | |
return mapping | |
while True: | |
solver = Encotel() | |
try: | |
linha = raw_input() | |
print solver.get_number(linha) | |
except: | |
break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import unittest | |
from nose.tools import * | |
from Encotel import * | |
class EncotelTestCase(unittest.TestCase): | |
encotel = Encotel() | |
def testa_se_abc_retorna_222(self): | |
assert_equals(self.encotel.get_number('ABC'), '222') | |
def teste_se_ajm_retorna_256(self): | |
assert_equals(self.encotel.get_number('AJM'), '256') | |
def teste_se_entrada1_retorna_saida1(self): | |
assert_equals(self.encotel.get_number('1-HOME-SWEET-HOME'), '1-4663-79338-4663') | |
def teste_se_entrada2_retorna_saida2(self): | |
assert_equals(self.encotel.get_number('MY-MISERABLE-JOB'), '69-647372253-562') | |
def teste_se_teste1_retorna_teste1(self): | |
assert_equals(self.encotel.get_number('01010101-WXYZ'), '01010101-9999') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment