Word Order in Python

Word Order in Python

Hello Friends, How are you? Today I am going to solve the HackerRank Word Order Problem in Python with a very easy explanation. In this article, you will get one or more approaches to solving this problem. So let’s start- {tocify} $title={Table of Contents} 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. The first line contains the integer, n. The next n lines each contain a word. The sum of the lengths of all the words does not exceed 10^6 All the words are composed of lowercase English letters only. 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.

4 bcdef abcdefg bcde bcdef {codeBox}

3 2 1 1 {codeBox}

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. Approach I: Word Order HackerRank Python Solution

# ========================
# Information
# ======================== # Name: Word Order in Python HackerRank
# Direct Link: https://www.hackerrank.com/challenges/word-order/problem
# Difficulty: Medium
# Max Score: 50
# Language: Pypy 3 # ========================
# Solution Start
# ======================== #Word Order in Python - Hacker Rank Solution # Enter your code here. Read input from STDIN. Print output to STDOUT import collections; N = int(input())
d = collections.OrderedDict() for i in range(N): word = input() if word in d: d[word] +=1 else: d[word] = 1 print(len(d)); for k,v in d.items(): print(v,end = " "); #Word Order in Python - Hacker Rank Solution END
# MyEduWaves
No Comments

Sorry, the comment form is closed at this time.