# HackerRank Solution: Python Word Order [3 Methods]

Author: Bashir Alam
Reviewer: Deepak Prasad

## Question: Python Word Order (Collections)

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 the appearance of the word. See the sample input/output for clarification.

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

Input Format

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

Constraints

1 <= n <= 10^5
The sum of the lengths of all the words does not exceed 10^6
All the words are composed of lowercase English letters only.

Output Format:

Output 2 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 is "`bcdef`", "`abcdefg`" and "`bcde`" which corresponds to the output.

## Possible solutions:

In this section, we will try to solve the given problem using various methods:

### Solution-1: Using collection module

In this solution, we will reads a list of words from the user, counts the frequency of each word in the list, and prints the number of unique words and the frequency of each word.

``````# importing module
from collections import Counter

integer1 = int(input())
lst = [list(map(str, input().split())) for _ in range(integer1)]

words = [x[0] for x in lst]

print(len(Counter(words).keys()))
val = list(Counter(words).values())
listToStr = ' '.join([str(elem) for elem in val])
print(listToStr) ``````

The input consists of an integer `integer1` followed by `integer1` lines, each containing a word. The code uses a list comprehension to read the input and create a list of lists `lst`, where each sublist contains a single word.

The list `words` is created by extracting the first element (the word) from each sublist in `lst`.

The `Counter` class from the `collections` module is then used to count the frequency of each word in the list. The `Counter` class takes an iterable (in this case, the list `words`) as input and returns a dictionary with the elements of the iterable as keys and their frequency as values.

The code then prints the number of unique words by calling the `keys` method on the `Counter` object and taking the length of the resulting list.

Finally, the code creates a list of the values (frequencies) in the `Counter` object, converts this list to a string with each value separated by a space, and prints the string.

### Solution-2: Using for loop

Now instead of iterators from collection module, we will use for loops as shown below:

``````n=int(input())
words=[input() for i in range(n)]
words_occurences={}
for word in words:
words_occurences[word]=0
for word in words:
words_occurences[word]+=1
print(len(words_occurences))
occurences=words_occurences.values()
for i in occurences:
print(i,end=" ")
``````

This code reads a list of words from the user, counts the frequency of each word in the list, and prints the number of unique words and the frequency of each word.

The input consists of an integer `n` followed by `n` lines, each containing a word. The code uses a list comprehension to create a list `words` containing the input words.

The code then initializes an empty dictionary `words_occurrences`, where the keys will be the words and the values will be the frequency of each word. It iterates through the list `words` and adds each word to the dictionary with a value of 0.

The code then iterates through `words` again and increases the value (frequency) of each word in the dictionary by 1.

Finally, the code prints the number of unique words by taking the length of the dictionary `words_occurrences`, and prints the values (frequencies) in the dictionary by iterating through the dictionary's `values` method and printing each value with a space separator.

### Solution-3: Using classes in Python

Now let us use the Python class to solve the given question:

``````# Total Words
n = int(input())

# Create a Ordered Counter by merging Counter and Ordered Dict
from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
pass

# Read and count using Counter
word_counter = OrderedCounter(
[input() for _ in range(n)]
)

# print results
print(len(word_counter))
print(*[item[1] for item in word_counter.items()])``````

This code reads a list of words from the user, counts the frequency of each word in the list, and prints the number of unique words and the frequency of each word.

The input consists of an integer `n` followed by `n` lines, each containing a word. The code uses a list comprehension to create a list of the input words.

The code then creates a custom class `OrderedCounter` by subclassing the `Counter` class from the `collections` module and the `OrderedDict` class. The `Counter` class is used to count the frequency of elements in an iterable, and the `OrderedDict` class is a dictionary subclass that preserves the order that elements are added.

The `OrderedCounter` class is then used to count the frequency of the words in the list. It takes the list as input and returns an `OrderedCounter` object with the words as keys and their frequency as values.

Finally, the code prints the number of unique words by taking the length of the `OrderedCounter` object, and prints the values (frequencies) in the object by iterating through the object's `items` method, extracting the values, and printing each value with a space separator.

## Summary

In this article, we learned how we can solve the Words Order question from HackerRank. We solved the problem using three different solutions.

## Further Reading

Question on Hacker Rank: Python Word Order

Views: 344

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.