Unit 2 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the problem asking for?
signals1
not in signals2
, and one with distinct integers from signals2
not in signals1
.Q: What are the inputs?
signals1
and signals2
.Q: What are the outputs?
signals1
not in signals2
, and the second element is a list of integers from signals2
not in signals1
.Q: Are there any constraints on the values in the arrays?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use sets to find the differences between the two lists and convert the results back to lists.
1. Convert `signals1` and `signals2` to sets `set1` and `set2`.
2. Find the difference between `set1` and `set2` and store it in `diff1`.
3. Find the difference between `set2` and `set1` and store it in `diff2`.
4. Convert `diff1` and `diff2` to lists.
5. Return the list `[diff1, diff2]`.
def find_difference(signals1, signals2):
# Step 1: Convert the lists to sets
set1 = set(signals1)
set2 = set(signals2)
# Step 2: Find the difference between the sets
diff1 = list(set1 - set2)
diff2 = list(set2 - set1)
# Step 3: Return the differences as lists
return [diff1, diff2]