PY-16

Merge Matching Pairs from Left to Right

  • Medium
  • Text and Sequences
  • Python

Task

Write merge_pairs(values, pair, replacement). pair is a two-item tuple (first, second). Read values from left to right and replace every non-overlapping adjacent pair whose two values equal first and second.

When a pair matches, append one replacement value to the result and advance past both matched input values. When it does not match, append the current value and advance by one. Continue until all input values have been considered.

This is one pass over the original list: do not inspect a replacement again for another match. Return a new list and do not change values or pair.

Example

Overlapping matches do not share an input value:

The final unmatched value remains in the result. A replacement that happens to look like pair is still appended once and is not rescanned.

Your implementation

Edit solution.py and keep this function name and signature:

Return the required list; do not print it or ask for input. You may assume that pair contains exactly two values.