Statistics offices sometimes start a new price index (a new basket, a new base year) and stop the old one. To get one long series they splice the two at a link year that both cover: the old series is multiplied by one factor so that it agrees with the new series in the link year,
\text{spliced}_y = \text{old}_y \cdot \frac{\text{new}_L}{\text{old}_L}
old and new map calendar years to index values. The years of each are consecutive (no gaps), and the two share at least one year. The link year L is the latest year that appears in both.
Write splice(old, new) that returns a dict over every year that appears in either series: a year of new keeps its new value; a year only in old gets its scaled old value.
Examples
Input: old = {1995: 100.0, 1996: 104.0, 1997: 109.2, 1998: 113.568}
new = {1997: 90.0, 1998: 93.0, 1999: 96.72, 2000: 100.0}
Output: {1995: 81.88926458157228, 1996: 85.16483516483517, 1997: 90.0,
1998: 93.0, 1999: 96.72, 2000: 100.0}
Explanation: the series overlap in 1997 and 1998; the link is 1998, with factor
93 / 113.568. 1997 is in the new series, so it keeps 90.0 (the scaled old value
would be 89.42: two indexes rarely agree exactly in every shared year).
Input: old = {2010: 80.0, 2011: 88.0}, new = {2011: 100.0, 2012: 103.0}
Output: {2010: 90.9090909090909, 2011: 100.0, 2012: 103.0}
Constraints
- floats are compared with a tolerance of
1e-6
Goals
- Find the link year where two index series overlap
- Scale an old series onto a new series' base with one ratio
- Merge two dicts with a clear rule for the years they share