Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Python. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for DefaultDict Tutorial in Python-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Python
Python is a widely-used, interpreted, object-oriented, and high-level programming language with dynamic semantics, used for general-purpose programming. It was created by Guido van Rossum, and first released on February 20, 1991.
Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. It is also used to create various machine learning algorithm, and helps in Artificial Intelligence. Python is a general purpose language, meaning it can be used to create a variety of different programs and isn’t specialized for any specific problems. This versatility, along with its beginner-friendliness, has made it one of the most-used programming languages today. A survey conducted by industry analyst firm RedMonk found that it was the most popular programming language among developers in 2020.
Link for the Problem – DefaultDict Tutorial in Python – HackerRank Solution
DefaultDict Tutorial in Python – HackerRank Solution
Problem:
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 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 words, 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.
Constraints :
- 1 <= n <= 10000
- 1 <= m <= 100
- 1 <= length of each word in the input <= 100
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.
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 :
5 2 a a b a b a b
Sample Output :
1 2 4 3 5
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.
DefaultDict Tutorial in Python – HackerRank Solution
# DefaultDict Tutorial in Python - Hacker Rank Solution # Python 3 # Enter your code here. Read input from STDIN. Print output to STDOUT # DefaultDict Tutorial in Python - Hacker Rank Solution START 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)
Hey! Do you use Twitter? I’d like to follow you if that would be okay. I’m undoubtedly enjoying your blog and look forward to new posts.