Hackerrank - Defaultdict Solution

Hackerrank - Defaultdict 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 .

Constraints


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 .

Output Format

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

Sample Input

5 2
a
a
b
a
b
a
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 .

Solution in Python

from collections import defaultdict

n,m = list(map(int,input().split()))
d = defaultdict(list)
for i in range(n):
    d[input()].append(i+1)
for i in range(m):
    print(*d[input()] or [-1])

Additional Information

Understanding the following line

print(*d[input()] or [-1])

What does * in Python mean?

The * in python is used to unpack the iterable.

print(*[1,2,3]) 
#1 2 3

print(*(1,2,3))
#1 2 3

print(*"123")
#1 2 3

The or condition returns the first true value

print(0 or 1 or 2)
#1

print(1 or 0 or 2)
#1

print("" or 0 or "Good")
#Good

print([] or [-1])
#[-1]

When d[input()] is empty its value will be []

print(*d[input()] or [-1])

So our above code evaluates to

print(*[] or [-1])

The * is evaluated at last. First it will evaluate [] or [-1]. So our code becomes

print(*[-1])
#-1

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe