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; 1 and 1.0 count 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.

Listing 1: Two measurement lists with repeated numbers and different orders.
first = [8, 3, 8, 5, 1, 3]
second = [1, 8, 8, 3, 9]
  1. Present your algorithm in pseudocode, a flowchart, or Python. If you use Python, name the function ordered_common_values(first, second).
  2. Give checks with inputs and expected results for repeated numbers, empty input, and lists with no common numbers.