I know set()python has no order since it is implemented as a hash table. However, I was a little surprised to resolve the issue of order using set.intersection().
So, I am assigned two lists with order, for example, indicating a certain rating or sequence of occurrence. I have to find an element that is common to both lists, and has the highest order (first) in the two lists. For instance,
List1 = ['j','s','l','p','t']
List2 = ['b','d','q','s','y','j']
must infer 's', since he is the second best in List1and occurs first in List2.
If you convert each of the lists into sets and take an intersection Set1.intersection(Set2), you will get a set set(['s', 'j']). In my case, I could convert this to a list and spit out the first element, and that was approximately O (n1 + n2).
I was happy to solve this interview question (all tests passed), but I am surprised how I can solve such a problem using python set.
Does anyone know how this works? What is the possible case that this can lead to breakage?
EDIT: looks like this is a case of luck, so if you have a good solution for this problem, it will also be appreciated