F
F
fakename2014-11-29 10:40:18
Python
fakename, 2014-11-29 10:40:18

How to find an error in Python code?

I solved the problem, but the code does not pass the test (stepic.org). What can be improved?
task :
The program should read one line from the standard input and output for each unique word in this line the number of its repetitions (case insensitive) in the format "word count" (see output example).
The order of output of words can be arbitrary.
Sample Input 1:
a a abC aa ac abc bcd a
Sample Output 1:
a 2
aa 2
ac 1
abc 2
bcd 1
Sample Input 2:
a A a
Sample Output 2:
a 3
Memory Limit: 256 MB
Time Limit: 5 seconds
mine solution :

def f():
    key = [i.lower() for i in input().split()]
    d={}
    for i in key:
       if i in d:
           d[i]+=1
       elif i not in d:
           d[i]=1
    for i in d:
       print(i, d[i])

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Va1ery, 2014-11-30
@fakename

"The order of output of words can be arbitrary."
Your program works correctly, and in order not to fit into 256 megabytes, there must be a very large input string.
Maybe all the same, they did not consider all possible output options for some test, this happens. Try, for the sake of interest, to output by ordering by key, or by the length of the key, and inside the length alphabetically, in the examples like this.
It is also possible that the verification server requires the main function as an entry point, try adding
if __name__ == '__main__':
main()
and put what you wrote into a separate function

S
Sergey Lerg, 2014-11-29
@Lerg

Most likely, you don’t fit in memory, don’t use word splitting in list, go along the line, isolate words, increment the dictionary with words.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question