1
I’m trying to solve an exercise, but I’m not getting any results yet. I’m new to language.
'''Define a merge function that does the following: Given two ordered lists L1 and L2, returns an ordered list of all L1 and L2 numbers.
The function should NOT use python’s Sort method.
Instead, do the following: * Create an I1 index for the L1 list and an I2 for L2 * Initialize I1 and I2 with 0 * Compare L1[I1] to L2[I2] and put the smaller of the two on the response list. If the lowest was L1[I1], increase I1. Otherwise, increase I2 * Thus, L1[I1] and L2[I2] are always the smallest elements of L1 and L2, and one of them is always the next to enter in response * Keep doing this until you add all the elements of L1 and L2 in response'''
I tried to do something like this below
def merge(lista1, lista2):
i1 = 0
i2 = 0
resposta = []
lista = lista1 + lista2
for num in lista:
if num < lista2[i2]:
resposta.append(lista1[i1])
i1 += 1
else:
resposta.append(lista2[i2])
i2 += 1
return resposta
https://answall.com/questions/252400/python-order-lists-sem-o-sort See if this helps...
– White
The name of this is balance line, join two already ordered lists.
– epx