Python Collections: Hackerrank Question on Counter

The Question

https://www.hackerrank.com/challenges/word-order/problem

You are given n 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 …


This content originally appeared on DEV Community and was authored by Kathan Vakharia

The Question

https://www.hackerrank.com/challenges/word-order/problem

You are given n 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.

The Approach

  1. store the words as they come in the Counter.
  2. print them.

? I hope you remember, Counter after python 3.7 internally maintains insertion order.

Code

from collections import Counter

words = list()

#n -> no of words
n = int(input())
for _ in range(n):
    words.append(input())

#
c =Counter(words)

print(len(c))
print(*c.values())


This content originally appeared on DEV Community and was authored by Kathan Vakharia


Print Share Comment Cite Upload Translate Updates
APA

Kathan Vakharia | Sciencx (2021-08-26T05:49:09+00:00) Python Collections: Hackerrank Question on Counter. Retrieved from https://www.scien.cx/2021/08/26/python-collections-hackerrank-question-on-counter/

MLA
" » Python Collections: Hackerrank Question on Counter." Kathan Vakharia | Sciencx - Thursday August 26, 2021, https://www.scien.cx/2021/08/26/python-collections-hackerrank-question-on-counter/
HARVARD
Kathan Vakharia | Sciencx Thursday August 26, 2021 » Python Collections: Hackerrank Question on Counter., viewed ,<https://www.scien.cx/2021/08/26/python-collections-hackerrank-question-on-counter/>
VANCOUVER
Kathan Vakharia | Sciencx - » Python Collections: Hackerrank Question on Counter. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/26/python-collections-hackerrank-question-on-counter/
CHICAGO
" » Python Collections: Hackerrank Question on Counter." Kathan Vakharia | Sciencx - Accessed . https://www.scien.cx/2021/08/26/python-collections-hackerrank-question-on-counter/
IEEE
" » Python Collections: Hackerrank Question on Counter." Kathan Vakharia | Sciencx [Online]. Available: https://www.scien.cx/2021/08/26/python-collections-hackerrank-question-on-counter/. [Accessed: ]
rf:citation
» Python Collections: Hackerrank Question on Counter | Kathan Vakharia | Sciencx | https://www.scien.cx/2021/08/26/python-collections-hackerrank-question-on-counter/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.