Count amount of "holes" in the letters of a text

Asked

Viewed 804 times

-4

I need to write a program in Python that counts the amount of "holes" in a string. Imagine, for example, that the letters "A", "D", "O", "P", "R" have only one hole. Similarly, 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 provide in the input two information, in two lines: The first line contains a single integer T <= 40 which indicates the number of test cases. Then follow-up T test cases. Each line of the test case contains a non-empty text composed only of uppercase characters of the English alphabet. The size of each text is less than 100. For each test case, the output consists of a line containing the number of holes found in the test case. If the entrees are:

3
LARANJA
UVA
PERA

The exit would be:

4
1
3

I haven’t tried any promising code yet. I need a hint.

  • Have you thought about how to start developing? At least the initial steps?

  • @Diegof was right, I had not seen it. But, if I were him, I would remove both the beginning of the question and the tag, since it is a question about algorithm. It would be a beginning to ask the question to be better received.

  • I thought of assigning a value to each relevant letter, that is, those that have "holes", according to the number of "holes" that it has. The provided string would be read by the program, passing through each letter and counting the values of each one and adding up at the end. My difficulty is in managing the functions in the right way.

  • @Diegof In this type of question, in which the AP wants a "north" (as it was written in the original question), marked with the algorithm tag, I don’t see how the language is relevant. It restricts and decreases the possibility of him finding an answer. Who has the hidden Python tag does not see this question, even being able to answer.

  • It’s not that I see the problem. I only see room for improvement, both to increase the visibility of the question and for future readers who have the same doubt. But OK. :)

1 answer

2


I created a dictionary with letter and value type Dict = {'A':1,'B':2} then iterated by its fruits summarizing values found in Dict.

Ps. I would like to see this with list comprehensions or something more codigo simplifier (itertools for example) or something similar. If any more advanced Coder is out there accept the challenge :-)

ps . attention if your fruits are uppercase or minuscule , 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()

outworking:

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
  • The code should be written to receive only fully uppercase words, but thank you for calling attention. As for the list of words, it must be provided by the user, and not by whoever wrote the code, so I need to use raw_input so that the program receives as many entries as the user wants to provide. I will base myself on your answer and seek the solution. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.