HackerRankInterview QuestionsPythonPython InterviewRandom

HackerRank DefaultDict Tutorial solution in Python | python problem solution


 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

This prints:

('python', ['awesome', 'language'])
('something-else', ['not relevant'])

In this challenge, you will be given  integers,  and . There are  words, which might repeat, in word group . There are  words belonging to word group . For each  words, check whether the word has appeared in group  or not. Print the indices of each occurrence of  in group . If it does not appear, print .

Example

Group A contains ‘a’, ‘b’, ‘a’ Group B contains ‘a’, ‘c’

For the first word in group B, ‘a’, it appears at positions  and  in group A. The second word, ‘c’, does not appear in group A, so print .

Expected output:

1 3
-1

Input Format

The first line contains integers,  and  separated by a space.
The next  lines contains the words belonging to group .
The next  lines contains the words belonging to group .

Constraints



Output Format

Output  lines.
The  line should contain the -indexed positions of the occurrences of the  word separated by spaces.

Sample Input

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

Sample Output

1 2 4
3 5

Explanation

‘a’ appeared  times in positions  and .
‘b’ appeared  times in positions  and .
In the sample problem, if ‘c’ also appeared in word group , you would print .

Problem solution in Python 2 programming.

from collections import defaultdict
d = defaultdict(list)
n,m = map(int, raw_input().split())
for i in range(1,n+1):
d[raw_input()].append(str(i))

for _ in range(m):
w = raw_input()
if w in d:
print " ".join(d[w])
else:
print -1

Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import defaultdict
d = defaultdict(list)
list1=[]
n, m = map(int,input().split())
for i in range(1, n+1):
d[input()].append(str(i))


for i in range(m):
b = input()
if b in d: print(' '.join(d[b]))
else: print(-1)

Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import defaultdict
d, n = defaultdict(list), list(map(int, raw_input().split()))

for i in xrange(n[0]):
d[raw_input()].append(i + 1)

for i in xrange(n[1]):
print ' '.join(map(str, d[raw_input()])) or -1

Problem solution in pypy3 programming.

from collections import defaultdict
d=defaultdict(list)
n,m=map(int,input().split())
x=[]
for i in range(1,n+m+1):
c=input()
if(i>n):
if(d[c]==[]):
d[c].append(-1)
x.append(d[c])
else:
x.append(d[c])
else:
d[c].append(i)

for i in range(m):
print(" ".join(list(map(str,(x[i])))))

Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science