Number of "holes" in the letters of a text

-4

I need to write a program in Python that counts the number of "holes" in a string. Imagine, for example, that the letters "A", "D", "O", "P", "R" have only one hole. Likewise, the letter "B" has two holes. The letters "C", "E", "F", "K" have no holes. The program should consider that the number of holes in a text is equal to the total number of holes in the letters of the text. The user must supply two information in the input, in two lines: The first line contains a simple integer T < = 40 that indicates the number of test cases. Then, there are T test cases. Each line of the test case contains non-empty text composed only of uppercase English alphabet characters. The size of each text is less than 100. There are no spaces in the input. For each test case, the output consists of a line containing the number of holes found in the test case. If the entries are:

3
LARANJA
UVA
PERA

The output would be:

4
1
3

I have not tried any promising code yet. I need a tip.

    
asked by anonymous 01.04.2016 / 13:12

1 answer

2

I created a dictionary with letter and value type dict = {'A': 1, 'B': 2} then iterava for its fruits adding values found in the dict.

Ps. I would like to see this with list comprehensions or something more simpler than the code (itertools for example) or something like that. If any more advanced coders are out there accept the challenge: -)

ps. if your fruits are case-sensitive, it will vary. See the case of B and b.

burakonta = {'a':1,'A':1, 'b':1,'B':2, 'e':1}
salada = ['uva','laranja','pera','BANANA','bananinha']

def qual_fruta():
    for fruta in salada:
        buracos(fruta)

def buracos(fruta, buracos=0):
    for letra in fruta:
        if letra in burakonta:
            buracos = buracos + burakonta[letra]
    print 'fruta %s tem %s buracos' %(fruta, buracos)


if __name__ == "__main__":
    qual_fruta()

result:

fruta uva tem 1 buracos
fruta laranja tem 3 buracos
fruta pera tem 2 buracos
fruta BANANA tem 5 buracos
fruta bananinha tem 4 buracos
    
01.04.2016 / 15:02