Find common values in their original order
Two measurement lists can contain repeated numbers. Design an algorithm that receives first and second and returns a new list of the numbers present in both inputs. Use sets to check membership.
- Each common number must appear once, in the order of its first occurrence in
first. - Both inputs contain finite integers or decimal numbers. Compare numbers by their value;
1and1.0count as the same number. - Leave both input lists unchanged. Return
[]when either list is empty or when the lists have no common numbers.
For the inputs in Listing 1, the required result is [8, 3, 1]. The order of the second list does not determine the output order.
- Present your algorithm in pseudocode, a flowchart, or Python. If you use Python, name the function
ordered_common_values(first, second). - Give checks with inputs and expected results for repeated numbers, empty input, and lists with no common numbers.