Last active
December 10, 2015 23:08
-
-
Save joaodubas/4507062 to your computer and use it in GitHub Desktop.
# Desafio Se você pensar em um papel como um plano e uma letra como uma marcação neste plano, então estas letras dividem o plano em regiões. Por exemplo, as letras A, D e O dividem o plano em 2 pois possuem um espaço confinado em seu desenho, ou um “buraco”. Outras letras como B possuem 2 buracos e letras como C e E não possuem buracos. Portanto…
This file contains 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2013 João Paulo Dubas <[email protected]> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains 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 python | |
# encoding: utf-8 | |
import string | |
MAX_NUMBER_OF_TEST = 40 | |
MAX_WORD_LENGTH = 100 | |
def translate_table(): | |
"""translate_table() -- Return a translation table.""" | |
letters = string.digits + string.letters + ' ' | |
values_digit = '0000101021' | |
values_lower = '11011010000000111000000000' | |
values_upper = '12010000000000111100000000' | |
values_space = '0' | |
values = 'values_digit + values_lower + values_upper + values_space | |
table = string.maketrans(letters, values) | |
return table | |
def count_holes(word): | |
"""count_holes(word) -- Return the number of holes contained in a given | |
word. | |
-- word: a string. | |
Usage: | |
>>> count_holes('paralelepipedo') | |
10 | |
>>> count_holes('hello') | |
2 | |
>>> count_holes('this is spartaaa') | |
5 | |
>>> count_holes('B' * 101) | |
Traceback (most recent call last): | |
... | |
ValueError: Word must not be greater than 100 letters. | |
""" | |
if len(word) > MAX_WORD_LENGTH: | |
raise ValueError('Word must not be greater than 100 letters.') | |
translation = string.translate(word, translate_table()) | |
count = sum(map(int, translation)) | |
return count | |
def process_line(line): | |
"""process_line(line) -- Process a line of test cases and return a string | |
with the count of holes for each word. | |
-- line: a command line | |
Usage: | |
>>> process_line('3:paralelepipedo;hello;this is spartaaa;') | |
'10;2;5' | |
>>> process_line('41:test') | |
Traceback (most recent call last): | |
... | |
ValueError: Only 40 tests are allowed. | |
>>> process_line('2:test;another test;wrong test;') | |
Traceback (most recent call last): | |
... | |
ValueError: Wrong number of tests. | |
""" | |
values = line.split(':') | |
number_of_tests = int(values[0]) | |
if number_of_tests > MAX_NUMBER_OF_TEST: | |
raise ValueError('Only 40 tests are allowed.') | |
texts = values[-1].split(';')[:-1] | |
if number_of_tests != len(texts): | |
raise ValueError('Wrong number of tests.') | |
result = [count_holes(word) for word in texts] | |
return ';'.join(map(str, result)) | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() | |
import argparse | |
import sys | |
parser = argparse.ArgumentParser(description='Process a line of tests') | |
parser.add_argument('line', type=str, | |
help='''A test configuration. | |
In the form of: <number_of_tests>:<phrases separated by ';'>''' | |
) | |
args = parser.parse_args() | |
try: | |
sys.stdout.write('{0}\n'.format(process_line(args.line))) | |
except ValueError as e: | |
sys.stderr.write('An error occured: {0}\n'.format(e.message)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment