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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
In Python IDLE: How would I write a function for the problem in the attached image?
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]
3
>>> interleaved( [-4, 0, 4, 8], [-7, -2, -1])
4
[-7, -4, -2, -1, 0, 4, 8]
>>> interleaved( [-8, 4, 4, 5, 6, 6, 6, 9, 9], [-6, -2, 3, 4, 4, 5, 6, 7,
8])
[-8, -6, -2, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 9, 9]
7
>>> interleaved( [-3, -2, 0, 2, 2, 2, 3, 3, 3], [-3, -2, 2, 3])
8
[-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]
15
>>> interleaved ([],[])
16
[]
17
>>> interleaved( list(range(-2,12,3)), list(range (20,50,5)) )
18
[-2, 1, 4, 7, 10, 20, 25, 30, 35, 40, 45]
19
>>> interleaved( list(range (20,50,5)), list(range(-2,12,3)) )==[-2, 1, 4,
7, 10, 20, 25, 30, 35, 40, 45]
20
True
Transcribed Image Text: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] 3 >>> interleaved( [-4, 0, 4, 8], [-7, -2, -1]) 4 [-7, -4, -2, -1, 0, 4, 8] >>> interleaved( [-8, 4, 4, 5, 6, 6, 6, 9, 9], [-6, -2, 3, 4, 4, 5, 6, 7, 8]) [-8, -6, -2, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 9, 9] 7 >>> interleaved( [-3, -2, 0, 2, 2, 2, 3, 3, 3], [-3, -2, 2, 3]) 8 [-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] 15 >>> interleaved ([],[]) 16 [] 17 >>> interleaved( list(range(-2,12,3)), list(range (20,50,5)) ) 18 [-2, 1, 4, 7, 10, 20, 25, 30, 35, 40, 45] 19 >>> interleaved( list(range (20,50,5)), list(range(-2,12,3)) )==[-2, 1, 4, 7, 10, 20, 25, 30, 35, 40, 45] 20 True
Expert Solution
code:

#function interleaved takes two sorted lists, then returns a sorted sequence of all numbers by interleaving the two sequences
def interleaved(l1,l2):
    i=j=0#i is used to traverse l1, j is used to traverse l2
    l3=[]#new empty list to store sorted sequence of all numbers in both arrays
    while i<len(l1) and j<len(l2):#runs until one of the index counter reaches its arrays length
        if l1[i]<=l2[j]:#if current element of l1 is smaller than l2's element
            l3=l3+[l1[i]]#then adding current element of l1 and leaving element of l2
            i+=1            #moving index of l1 
        elif l2[j]<l1[i]:#if current element of l2 is smaller than l1's element
            l3=l3+[l2[j]]#then adding current element of l2 and leaving element of l1
            j+=1            #moving index of l2
    #since one of the counter will reach the size of its array
    #we must add all elements remaining in the other array
    while i<len(l1):#adding all remaining elements of l1 to l3
        l3=l3+[l1[i]]
        i+=1
    while j<len(l2):#adding all remaining elements of l2 to l3
        l3=l3+[l2[j]]
        j+=1
    #finally returning sorted l3
    return l3
#testing above method
print(interleaved([-7,-2,-1],[-4,0,4,8]))
print(interleaved([-4,0,4,8],[-7,-2,-1]))
    

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY