I'm trying to work on a list union algorithm now, with the following specifications: if an element in L1 occurs in L1 more than it occurs in L2, the union should return the maximum number of occurrences, i.e the amount it occurs in L1, with the roles of L1 and L2 switched if an element occurs in L2 more than it occurs in L1. If L1 and L2 are disjoint, the union just returns the regular set union. So far my thought process has been:
- Iterate through L1.
- Check if any element in L1 is also in L2.
- If an element in L1 is also in L2, check which list has the greater
countof the element. - If L1 and L2 are disjoint, return regular set union.
- Repeat step 3 with L2 and L1 reversed.
- Return the union.
I was thinking about using the max function to kind of tell Python to return the list where the multiplicity of each element in the union is the maximum number of occurrences of the element in both L1 and L2. Ideas?
collections.Counter...collections.Counterimplementation. Transforming this into a dictionary will make this much easier. Good idea.