DefaultDict Tutorial : HackerRank Solution in Python

Solution in Python 3

Link:- DefaultDict (github.com)

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

Explanation

First we will import defaultdict from collections module of Python. Then we will accept n and m from the input stream.

n, m = map(int, input().split())

The above map function applies the int to the items of input().split() and stores the respective values in variables n and m.

Subsequently, we declare a defaultdict and pass a list as its parameter which will make all the values of the defaultdict to be stored in the form of a Python list.

d = defaultdict(list)

Now we will use a for loop to iterate over the range of n through variable i

for i in range(n):

In the for loop we will, we will append i+1 to the defaultdict d with input() as a key

for i in range(n):
d[input()].append(i+1)

i+1 represents the positions of words in group A which are accepted from the input stream. We append i+1 because in the Python’s range function starts iteration from 0.

Thus in the above for loop we store index positions of words that occur in word group A.

Then we will declare another for loop to iterate over range of m, through a variable j.

for j in range(m):

In the for loop we will accept the word whose positions are to be outputted in a variable called s.

s= input()

Then we will check if s exists as a key in the default dictionary d,

if s in d:

If s exists as a key in d, then the index occurrences of the word exists in group A and thus we print the values of the word in the defaultdict d.

for j in range(m):
s = input()

if s in d:
print(*d[s])

The * in the print functions enables us to print the value(s) of a list in form of a string, that is, without commas and square brackets.

If s doesn’t exist as a key then we print -1.

else:
print(-1)

Thus the second for loop accepts the words belonging to word group B and checks whether the word has appeared in group A, if it has displays the indices of each occurrences else it prints -1.

--

--

--

pretending to teach is one of the best way to learn.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Oz: a language being way too academic ?

The best way to store secrets in your app is not to store secrets in your app

Don’t Just Write a Code —  Write a Clean Code

4 Tips to Successfully Kick Off a Pen Test

Inducing MVVM to Existing Kotlin Code

What’s New in SAP ABAP

[Colawork Active User Interview]#2 NEXTLab

Cloud Analytics: You Can’t Control What You Can’t See

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shounak Lohokare

Shounak Lohokare

pretending to teach is one of the best way to learn.

More from Medium

How to Hide Messages in Pictures with Python: Steganography — StackZero

VS Code(1.66.2) Setting Up python interpreter path

How to process and plot Audio data with Python

Working with Migen(Python) & LiteX | 1 . Setting up Migen