You are viewing a single comment's thread. Return to all comments →
Actually I am new to Python, and it is difficult to understand what is your logic in this one line of code. Can you please elaboratoly explain it, including of course the usage of the built-ins you have used in python.
Okay. As such, the only built-in used here, is "set". set() function forms a mathematical type of set and removes all the duplicate elements. Similarly, set.intersection() takes intersection of all the sets (as in mathematical context). Please have a look at the docs and tutorial for better understanding.
The logic is to form a set corresponding to each "rock" by removing the duplicate "gems". So, each set will have every gem present in rock, exactly once. Now, we just need to find the common gems in each set, which is basically equivalent to finding intersection of all sets mathematically. Thus, set.intersection gives the list of the "gems" present in each rock. The result just requires the count of gems, hence, length of above result is printed as answer.
Thanks. Now I got it finally.
Its pretty elegant even in a 6 line Python solution, you should have no problem reading this:
n = int(input())
c = set(input())
for i in range(n-1):
a = set(input())
c = c.intersection(a)
You don't need to convert each input into a set, since the intersection method takes any iterable as an argument.
TypeError: descriptor 'intersection' requires a 'set' object but received a 'str'