We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
https://docs.python.org/3/library/stdtypes.html#truth-value-testing
now u need to understand: if i not in input() then d[i] will return [] right? So an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object. 1 Here are most of the built-in objects considered false:
constants defined to be false: None and False.
zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
empty sequences and collections: '', (), [], {}, set(), range(0)
so first in for loop range(n), it gets all the index of a and b, which is [1,2,4], [3,5]
then in for loop range(m) it checks: where is a's index ? where is b's index
if a's index or b's index == None, that means d[input()] in for loop range(m) == [] right?(it's None). So in this python doc:https://docs.python.org/3/library/stdtypes.html#truth-value-testing
it shows that: "empty sequences and collections: '', (), [], {}, set(), range(0)"
are considered False. So if it False, with False and -1, it's a boolean operator, simple, right ? It prints -1. Hope u can understand
if we don't want to store the input() value then we can do like this. Here input() will execute and asks for users input() and then it will check the particular key in defaultdict and return a list.So know we wanna concatenate the list values but .join() always receives string instance so we need to type cast each element of returned list into string so we are using map function. map() will retrieve element of list one by one and type cast it to string...
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
DefaultDict Tutorial
You are viewing a single comment's thread. Return to all comments →
I cannot understand the use of or inside the print statement. Could you elaborate it a bit.
https://docs.python.org/3/library/stdtypes.html#truth-value-testing now u need to understand: if i not in input() then d[i] will return [] right? So an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object. 1 Here are most of the built-in objects considered false:
constants defined to be false: None and False.
zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
empty sequences and collections: '', (), [], {}, set(), range(0)
and like i said, [] or -1, so -1 is True, so it prints out -1
print(' '.join(map(str,** d[input()]**)) or -1)
Can you explain me how it works--> ("d[input()")
so first in for loop range(n), it gets all the index of a and b, which is [1,2,4], [3,5] then in for loop range(m) it checks: where is a's index ? where is b's index if a's index or b's index == None, that means d[input()] in for loop range(m) == [] right?(it's None). So in this python doc:https://docs.python.org/3/library/stdtypes.html#truth-value-testing it shows that: "empty sequences and collections: '', (), [], {}, set(), range(0)" are considered False. So if it False, with False and -1, it's a boolean operator, simple, right ? It prints -1. Hope u can understand
if we don't want to store the input() value then we can do like this. Here input() will execute and asks for users input() and then it will check the particular key in defaultdict and return a list.So know we wanna concatenate the list values but .join() always receives string instance so we need to type cast each element of returned list into string so we are using map function. map() will retrieve element of list one by one and type cast it to string...