Working with a python dictionary

Asked

Viewed 66 times

-1

I’m trying to resolve an issue in the URI, I’m going to put the description of the issue, and the link, whatever you prefer.

Link: https://www.urionlinejudge.com.br/judge/pt/problems/view/1215

Andy only 8 years old has a dream - he wants to create his own dictionary. This is not an easy task for him as he knows few words. Well, instead of thinking the words you know, he had a brilliant idea. From his favorite storybook, he will create a dictionary with all the distinct words that exist in it. By ordering these words in alphabetical order, the work will be done. Of course, this is a task that takes a certain amount of time and therefore the help of a computer programmer like you is very welcome.

You have been asked to write a program that lists all the different words that exist in a text. In this case, a word is defined as a string of letters, uppercase or lowercase. Words with only one letter should also be considered. Therefore, your program should be "Case Insensitive". For example, words like "Apple", "apple" or "APPLE" should be considered the same word.

Entree

The input contains a maximum of 10000 lines of text, each of which has a maximum of 200 characters. The end of input is determined by EOF.

Exit

You should print a list of different words that appear in the text, one word per line. All words should be printed with lower case letters in alphabetical order. There should be a maximum of 5000 different words.

Input example:

Ex(*$a#.mpl.e:

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the road. The sign read: "Disneyland LEFT."

So they went home.

Output from this example:

a
adventures
blondes
came
disneyland
e
ex
fork
going
home
in
left
mpl
read
road
sign
so
the
they
to
two
went
were
when

That’s what I got so far:

#Recebo a entrada, e já a converto para minúsculo
entrada = input()
entrada = entrada.lower()

#Tento remover todos os caracteres que não sejam letras. Aí está o PRINCIPAL erro do meu código, pois desta forma, sei que não consigo remover todos os caracteres desejados da string
S = entrada
for x in '0123456789+-*/[]{}().,?":~´`ªº!@#$%¨&_':
    S = S.replace(x, ' ')

#Armazeno cada "palavra" na lista, separando pelo o espaço vazio (' '), e ordeno a lista
L = S.split(' ')
L = sorted(set(L))

#Removo índices que estão vazios ('')
i = 0
while i < len(L):
    if L[i] == '':
        L.pop(i)
    i += 1

#Exibo o conteúdo de cada índice da lista, um por linha
j = 0
while j < len(L):
    print(L[j])
    j += 1

I believe the error can be resolved in this part of the second comment.

  • 1

    Where is the error? ...

  • 2

    I think the problem with your program is that it doesn’t read the right input, it needs an EOF treatment, but I have no idea how to do that. Modify your question to something like: "what is EOF in python?" or "how to read EOF from a Pyhton input". The more your program seems to work, I would only trade this whim of the first for the function isalpha() python, which returns true if the string has only alphanumeric characters and false otherwise

  • Male, was that one isalpha() that I needed, I just found resolution with what removes if it is numerical, I think this will solve my problem

1 answer

0

I found the function that checks whether the character is or is not string, so I used the isalpha(), my departure was identical to the required exit from the URI, but the question remains with Wrong Answer (100%):

entrada = input()
entrada = entrada.lower()

S = entrada
for x in S:
    if not(x.isalpha()):
        S = S.replace(x, ' ')

L = S.split(' ')
L = sorted(set(L))

i = 0
while i < len(L):
    if L[i] == '':
        L.pop(i)
    i += 1

j = 0
while j < len(L):
    print(L[j])
    j += 1

My output using the same URI input output:

a
adventures
blondes
came
disneyland
e
ex
fork
going
home
in
left
mpl
read
road
sign
so
the
they
to
two
went
were
when

As you can see, my exit is identical to the URI.

  • Davi Monteiro, I think the entrance should be done in a loop of repetition only stopping when finding EOF.

Browser other questions tagged

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