This Python program is designed to solve a problem of counting the number of distinct words in a list and their occurrences. It uses the collections module, specifically the Counter and OrderedDict classes, to keep track of the words and their counts in the order they were first encountered.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution works by creating a custom class, OrderedCounter, that inherits from both Counter and OrderedDict. This allows it to count the occurrences of each word while maintaining the order of their first appearance. The program then reads a number of words from the user, stores them in a list, and creates an OrderedCounter from this list. It prints the number of distinct words and the count of each word in the order they were first encountered.
from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
pass
word_ar = []
n = int(input())
for i in range(n):
word_ar.append(input().strip())
word_counter = OrderedCounter(word_ar)
print(len(word_counter))
for word in word_counter:
print(word_counter[word], end=" ")
If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.