In traffic engineering, the late merge or zipper method is a convention for merging traffic into a reduced number of lanes. Drivers in merging lanes are expected to use both lanes to advance to the lane reduction point and merge at that location, alternating turns. The late merge method has not been found to increase throughput (the number of vehicles that pass through a point in a given period of time). However, it considerably reduces queue length because drivers use the ending lane until its end and reduces speed differences between the two lanes, increasing safety.
Governments hold campaigns to promote the late merge method because irritation, aggression and feelings of insecurity easily occur while zipping. Often drivers who change lanes too early do not like to see other drivers continue until the end of the drop-away lane, even though this late merging is encouraged by the authorities. In Belgium and Germany, a driver can be penalized for not using the late merge method. In Austria only where a traffic sign so indicates.
Most states in the United States require merging traffic to yield to through traffic which already exists in the lane they wish to enter. This further complicates the common understanding of proper merging protocol, as even though zipper merging is widely encouraged, those doing so are still legally required to yield, and those who choose not to let them merge are not technically doing anything wrong legally.
We'll implement three different ways to merge the elements of two given sequences (list or tuple) into a new list (list).
In all three cases, merging happens alternately: we first append the first element of the first sequence to the new list, then the first element of the second sequence, the second element of the first sequence, the second element of the second sequence, and so on.
In all three cases, merging also happens pairwise: if the $$i$$-th element of the first sequence is appended, the $$i$$-th element of the second sequence must be appended as well. The three implementations behave exactly the same if the two given sequences have the same length. But they differ in handling sequences of different lengths.
Your task:
Write a function merge that takes two sequences (list or tuple) and returns a new list (list) containing the elements of the two given sequences that have been merged in an alternate and pairwise manner. Merging stops as soon as all elements of the shortest sequence have been added to the new list.
Write a function weave that takes two sequences (list or tuple) and returns a new list (list) containing the elements of the two given sequences that have been merged in an alternate and pairwise manner. Merging stops as soon as all elements of the longest sequence have been added to the new list. After appending the last element of the shortest sequence, the function must continue appending elements at the start of the sequence, starting with the first one.
Write a function zipper that takes two sequences (list or tuple) and returns a new list (list) containing all elements of the two given sequences. The function starts merging the elements of both sequences in an alternate and pairwise manner, until all elements of the shortest sequence have been added. Then the function also appends the remaining elements of the longest sequence to the new list.
>>> merge(('A', 'B', 'C'), [1, 2, 3])
['A', 1, 'B', 2, 'C', 3]
>>> merge(['A'], [1, 2, 3, 4])
['A', 1]
>>> merge(('A', 'B'), (1, 2, 3, 4))
['A', 1, 'B', 2]
>>> merge(('A', 'B', 'C'), [1, 2])
['A', 1, 'B', 2]
>>> weave(('A', 'B', 'C'), [1, 2, 3])
['A', 1, 'B', 2, 'C', 3]
>>> weave(['A'], [1, 2, 3, 4])
['A', 1, 'A', 2, 'A', 3, 'A', 4]
>>> weave(('A', 'B'), (1, 2, 3, 4))
['A', 1, 'B', 2, 'A', 3, 'B', 4]
>>> weave(('A', 'B', 'C'), [1, 2])
['A', 1, 'B', 2, 'C', 1]
>>> zipper(('A', 'B', 'C'), [1, 2, 3])
['A', 1, 'B', 2, 'C', 3]
>>> zipper(['A'], [1, 2, 3, 4])
['A', 1, 2, 3, 4]
>>> zipper(('A', 'B'), (1, 2, 3, 4))
['A', 1, 'B', 2, 3, 4]
>>> zipper(('A', 'B', 'C'), [1, 2])
['A', 1, 'B', 2, 'C']
In 2008, physicist Yuki Sugiyama1 of the University of Nagoya demonstrated why traffic jams sometimes form in the absence of a bottleneck. He spaced 22 drivers around a 230-meter track and asked them to proceed as steadily as possible at 30 km/h, each maintaining a safe distance from the car ahead of it.
Because the cars were packed quite densely, irregularities began to appear within a couple of laps. When drivers were forced to brake, they would sometimes overcompensate slightly, forcing the drivers behind them to overcompensate as well. A stop-and-go wave developed: cars arriving at the back of the jam were forced to slow down, and cars reaching the front could accelerate again to normal speed, producing a living wave that crept backward around the track.
Interestingly, Sugiyama found that this phenomenon arises predictably in the real world. Measurements on various motorways in Germany and Japan have shown that free-flowing traffic becomes congested when the density of cars reaches 40 vehicles per mile. Beyond that point, the flow becomes unstable and stop-and-go waves appear. Because it's founded in human reaction times, this happens regardless of the country or the speed limit. And as long as the total number of cars on the motorway doesn't change, the wave rolls backward at a predictable 12 km/h.
Sugiyama told the following to Gavin Pretor-Pinney for his book The wavewatcher's Companion2:
Understanding things like traffic jams from a physical point of view is a totally new, emerging field of physics. While the phenomenon of a jam is so familiar to us, it is still too difficult to truly understand why it happens.
Sugiyama Y, Fukui M, Kikuchi M, Hasabe K, Nakayama A, Nishinari K, Tadaki S, Yukawa S (2008). Traffic jams without bottlenecks — experimental evidence for the physical mechanism of the formation of a jam. New Journal of Physics 10(3), 033001. 3
Vanderbilt T (2009). Traffic: why we drive the way we do (and what it says about us). Vintage. 4