Programming Problems Note: Please note that the doctest file uses random.seed. This guarantees that any numbers generated by the random module will be exactly the same each time. If you use the minimum (and correct) calls to the random function, your output should match the doctest exactly. If you make extra calls to random, then they will not match. interleaved Write a function interleaved that accepts two sorted sequences of numbers and returns a sorted sequence of all numbers obtained by interleaving the two sequences. Guidelines: • each argument is a list assume that each argument is sorted in non-decreasing order (goes up or stays the same, never goes down) you are not allowed to use sort or sorted (or any other form of sorting) in your solution • you must interleave by iterating over the two sequences simultaneously, choosing the smallest current number from each sequence. Sample usage: 1 >>> interleaved( [-7, -2, -1], [-4, 0, 4, 8]) 2 [-7, -4, -2, -1, 0, 4, 8] 234 >>> interleaved( [-4, 0, 4, 8], [-7, -2, -1]) [-7, -4, -2, -1, 0, 4, 8] 5 >>> interleaved( [-8, 4, 4, 5, 6, 6, 6, 9, 9], [-6, -2, 3, 4, 4, 5, 6, 7, 8]) 678 [-8, -6, -2, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 9, 9] >>> interleaved( [-3, -2, 0, 2, 2, 2, 3, 3, 3], [-3, -2, 2, 3]) [-3, -3, -2, -2, 0, 2, 2, 2, 2, 3, 3, 3, 3] 9 >>> interleaved( [-3, -2, 2, 3], [-3, -2, 0, 2, 2, 2, 3, 3, 3]) 10 [-3, -3, -2, -2, 0, 2, 2, 2, 2, 3, 3, 3, 3] 11 >>> interleaved ([1,2,2], []) 12 [1, 2, 2] 13 >>> interleaved ([],[1,2,2]) 14 [1, 2, 2]

icon
Related questions
Question
Programming Problems
Note: Please note that the doctest file uses random.seed. This guarantees that any numbers
generated by the random module will be exactly the same each time. If you use the minimum
(and correct) calls to the random function, your output should match the doctest exactly. If you
make extra calls to random, then they will not match.
interleaved
Write a function interleaved that accepts two sorted sequences of numbers and returns a
sorted sequence of all numbers obtained by interleaving the two sequences. Guidelines:
• each argument is a list
assume that each argument is sorted in non-decreasing order (goes up or stays the same,
never goes down)
you are not allowed to use sort or sorted (or any other form of sorting) in your solution
• you must interleave by iterating over the two sequences simultaneously, choosing the
smallest current number from each sequence.
Sample usage:
1
>>> interleaved( [-7, -2, -1], [-4, 0, 4, 8])
2 [-7, -4, -2, -1, 0, 4, 8]
234
>>> interleaved( [-4, 0, 4, 8], [-7, -2, -1])
[-7, -4, -2, -1, 0, 4, 8]
5 >>> interleaved( [-8, 4, 4, 5, 6, 6, 6, 9, 9], [-6, -2, 3, 4, 4, 5, 6, 7,
8])
678
[-8, -6, -2, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 9, 9]
>>> interleaved( [-3, -2, 0, 2, 2, 2, 3, 3, 3], [-3, -2, 2, 3])
[-3, -3, -2, -2, 0, 2, 2, 2, 2, 3, 3, 3, 3]
9 >>> interleaved( [-3, -2, 2, 3], [-3, -2, 0, 2, 2, 2, 3, 3, 3])
10 [-3, -3, -2, -2, 0, 2, 2, 2, 2, 3, 3, 3, 3]
11 >>> interleaved ([1,2,2], [])
12
[1, 2, 2]
13 >>> interleaved ([],[1,2,2])
14 [1, 2, 2]
Transcribed Image Text:Programming Problems Note: Please note that the doctest file uses random.seed. This guarantees that any numbers generated by the random module will be exactly the same each time. If you use the minimum (and correct) calls to the random function, your output should match the doctest exactly. If you make extra calls to random, then they will not match. interleaved Write a function interleaved that accepts two sorted sequences of numbers and returns a sorted sequence of all numbers obtained by interleaving the two sequences. Guidelines: • each argument is a list assume that each argument is sorted in non-decreasing order (goes up or stays the same, never goes down) you are not allowed to use sort or sorted (or any other form of sorting) in your solution • you must interleave by iterating over the two sequences simultaneously, choosing the smallest current number from each sequence. Sample usage: 1 >>> interleaved( [-7, -2, -1], [-4, 0, 4, 8]) 2 [-7, -4, -2, -1, 0, 4, 8] 234 >>> interleaved( [-4, 0, 4, 8], [-7, -2, -1]) [-7, -4, -2, -1, 0, 4, 8] 5 >>> interleaved( [-8, 4, 4, 5, 6, 6, 6, 9, 9], [-6, -2, 3, 4, 4, 5, 6, 7, 8]) 678 [-8, -6, -2, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 9, 9] >>> interleaved( [-3, -2, 0, 2, 2, 2, 3, 3, 3], [-3, -2, 2, 3]) [-3, -3, -2, -2, 0, 2, 2, 2, 2, 3, 3, 3, 3] 9 >>> interleaved( [-3, -2, 2, 3], [-3, -2, 0, 2, 2, 2, 3, 3, 3]) 10 [-3, -3, -2, -2, 0, 2, 2, 2, 2, 3, 3, 3, 3] 11 >>> interleaved ([1,2,2], []) 12 [1, 2, 2] 13 >>> interleaved ([],[1,2,2]) 14 [1, 2, 2]
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer