Word Order Simple.

Rodrick Brown rodrick.brown at gmail.com
Fri Mar 11 19:27:10 EST 2016


You are given nn words. Some words may repeat. For each word, output its
number of occurrences. The output order should correspond with the input
order of appearance of the word. See the sample input/output for
clarification.

*Note:* Each input line ends with a *"\n"* character.

*Constraints:*
1≤n≤1051≤n≤105
The sum of the lengths of all the words do not exceed 106106
All the words are composed of lowercase English letters only.

*Input Format*

The first line contains the integer, nn.
The next nn lines each contain a word.

*Output Format*

Output 22 lines.
On the first line, output the number of distinct words from the input.
On the second line, output the number of occurrences for each distinct word
according to their appearance in the input.

*Sample Input*

4
bcdef
abcdefg
bcde
bcdef

*Sample Output*

3
2 1 1

*Explanation*

There are 3 distinct words. Here, *"bcdef"* appears twice in the input at
the first and last positions. The other words appear once each. The order
of the first appearances are *"bcdef"*,*"abcdefg"* and *"bcde"* which
corresponds to the output.

Here is my attempt I can't seem to past all test cases and not sure why?
The explanation for line how to get 1 1 seems weird maybe I'm not reading
it correctly.

#!/usr/bin/env python3

from collections import defaultdict
from collections import Counter

if __name__ == '__main__':

  words = defaultdict(list)
  for i,word in enumerate(input() for x in range(int(input()))):
    words[word].append([i+1])

  count = Counter()
  print(len(words.keys()))

  for k in words:
    if len(words[k]) > 1:
      print(len(words[k]),end=' ')
    else:
      count[k] += 1

  for c in count.values(): print(c,end=' ')

$  cat words.txt | ./wordcount.py

3
2 1 1 ⏎



More information about the Python-list mailing list