DefaultDict Tutorial - Python HackerRank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank DefaultDict Tutorial 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}
HackerRank Python DefaultDict Tutorial Solution - Problem Statement
The defaultdict tool is a container in the collections class of Python. It's similar to the usual dictionary (dict) container, but the only difference is that a defaultdict will have a default value if that key has not been set yet. If you didn't use a defaultdict you'd have to check to see if that key exists, and if it doesn't, set it to what you want.
For example:
from collections import defaultdict d = defaultdict(list) d['python'].append("awesome") d['something-else'].append("not relevant") d['python'].append("language") for i in d.items(): print i {codeBox}
This prints
('python', ['awesome', 'language']) ('something-else', ['not relevant']) {codeBox}
In this challenge, you will be given 2 integers, n and m. There are n words, which might repeat, in word group A. There are m words belonging to word group B. For each m word, check whether the word has appeared in group A or not. Print the indices of each occurrence of m in group A. If it does not appear, print -1.
Example
Group A contains 'a', 'b', 'a' Group B contains 'a', 'c'
For the first word in group B, 'a', it appears at positions 1 and 3 in group A. The second word, 'c', does not appear in group A, so print -1.
Expected output:
1 3 -1 {codeBox}
Input Format
The first line contains integers, n and m separated by a space.
The next n lines contains the words belonging to group A.
The next m lines contains the words belonging to group B.
Constraints
1 <= n <= 10000
1 <= m <= 100
1 <= length of each word in the input <= 100
Output Format:
Output m lines.
The ith line should contain the 1-indexed positions of the occurrences of the ith word separated by spaces.
Sample Input 0
STDIN Function ----- -------- 5 2 group A size n = 5, group B size m = 2 a group A contains 'a', 'a', 'b', 'a', 'b' a b a b a group B contains 'a', 'b' b {codeBox}
Sample Output 0
1 2 4 3 5 {codeBox}
Explanation:
'a' appeared 3 times in positions 1, 2 and 4.
'b' appeared 2 times in positions 3 and 5.
In the sample problem, if 'c' also appeared in word group B, you would print -1.
Python DefaultDict Tutorial - Hacker Rank Solution
Approach I: DefaultDict Tutorial HackerRank Python Solution
# ========================
# Information
# ========================
# Name: DefaultDict Tutorial in Python HackerRank
# Direct Link: https://www.hackerrank.com/challenges/defaultdict-tutorial/problem
# Difficulty: Easy
# Max Score: 20
# Language: Pypy 3
# ========================
# Solution Start
# ========================
#DefaultDict Tutorial in Python - Hacker Rank Solution
from collections import defaultdict
n, m = map(int,input().split())
a = defaultdict(list)
for i in range(1, n + 1):
a[input()].append(i)
for i in range(1, m + 1):
key = input()
if len(a[key]) > 0:
print(" ".join(str(c) for c in a[key]))
else:
print(-1)
#DefaultDict Tutorial in Python - Hacker Rank Solution END
# MyEduWaves
Disclaimer: The above Problem ( Python DefaultDict Tutorial ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.
I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python DefaultDict Tutorial Hackerrank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Python