06 Feb DefaultDict Tutorial in
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} 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.
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}
(‘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. 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. 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. 1
Sorry, the comment form is closed at this time.