I am having trouble with my code. I'm still new to Python, but I appreciate any push in the right direction. I am trying to sort a sequence of numbers and returns sorted. I'm not asking for the answer, but for guidance to help me correct my own code.
I'm not sure how to fix the out of range. Would I need to add -1 somewhere?
def interleaved(seq1,seq2):
seq1= [-7, -2, -1]
seq2= [-4, 0, 4, 8]
i = 0
j = 0
res = []
while i <len(seq1) and j <len(seq2):
print(i,j,seq1[i],seq2[j])
i+=1
j+=1
if seq1[i] <seq2[j]:
print(res)
i+=1
res.append(seq1[i])
if seq1[i]> seq2[j]:
print(res)
j+=1
res.append(j)
return res
In here i'm taking *argv aguments so you can pass how many lists you want. Then am creating a main list with all lists joining together. Then i'm looping through elements and swapping if it's not in correct order
def interleaved(*seq):
res=[]
for x in seq:
res.extend(x)
for i in range(len(res)-1):
if res[i] > res[i+1]:
res[i],res[i+1]=res[i+1],res[i]
return res
seq1= [-7, -2, -1]
seq2= [-4, 0, 4, 8]
print(interleaved(seq1,seq2))