merging 3 lists in ascending order
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm looking for a way to merge 3 sorted lists of ascending order without using any built-in functions or recursion.
For example:
merge([1,4],[1,5,6],[3,7,9]) produces [1,1,3,4,5,6,7,9]
I have the following done so far but it does not produce the expected above result.
def merge(list1, list2, list3):
results =
while len(list1) and len(list2) and len(list3):
if (list1[0] < list2[0]) and (list1[0] < list3[0]):
results.append(list1.pop(0))
elif (list2[0] < list1[0]) and (list2[0] < list3[0]):
results.append(list2.pop(0))
elif (list3[0] < list1[0]) and (list3[0] < list2[0]):
results.append(list3.pop(0))
results.extend(list1)
results.extend(list2)
results.extend(list3)
return results
python
add a comment |
I'm looking for a way to merge 3 sorted lists of ascending order without using any built-in functions or recursion.
For example:
merge([1,4],[1,5,6],[3,7,9]) produces [1,1,3,4,5,6,7,9]
I have the following done so far but it does not produce the expected above result.
def merge(list1, list2, list3):
results =
while len(list1) and len(list2) and len(list3):
if (list1[0] < list2[0]) and (list1[0] < list3[0]):
results.append(list1.pop(0))
elif (list2[0] < list1[0]) and (list2[0] < list3[0]):
results.append(list2.pop(0))
elif (list3[0] < list1[0]) and (list3[0] < list2[0]):
results.append(list3.pop(0))
results.extend(list1)
results.extend(list2)
results.extend(list3)
return results
python
see my simple solution
– Geeocode
Nov 14 '18 at 2:07
Cutie Pie in my last update eliminated the only builtinlen()
function, take a look.
– Geeocode
Nov 14 '18 at 2:29
add a comment |
I'm looking for a way to merge 3 sorted lists of ascending order without using any built-in functions or recursion.
For example:
merge([1,4],[1,5,6],[3,7,9]) produces [1,1,3,4,5,6,7,9]
I have the following done so far but it does not produce the expected above result.
def merge(list1, list2, list3):
results =
while len(list1) and len(list2) and len(list3):
if (list1[0] < list2[0]) and (list1[0] < list3[0]):
results.append(list1.pop(0))
elif (list2[0] < list1[0]) and (list2[0] < list3[0]):
results.append(list2.pop(0))
elif (list3[0] < list1[0]) and (list3[0] < list2[0]):
results.append(list3.pop(0))
results.extend(list1)
results.extend(list2)
results.extend(list3)
return results
python
I'm looking for a way to merge 3 sorted lists of ascending order without using any built-in functions or recursion.
For example:
merge([1,4],[1,5,6],[3,7,9]) produces [1,1,3,4,5,6,7,9]
I have the following done so far but it does not produce the expected above result.
def merge(list1, list2, list3):
results =
while len(list1) and len(list2) and len(list3):
if (list1[0] < list2[0]) and (list1[0] < list3[0]):
results.append(list1.pop(0))
elif (list2[0] < list1[0]) and (list2[0] < list3[0]):
results.append(list2.pop(0))
elif (list3[0] < list1[0]) and (list3[0] < list2[0]):
results.append(list3.pop(0))
results.extend(list1)
results.extend(list2)
results.extend(list3)
return results
python
python
edited Nov 14 '18 at 1:31
user3483203
32.1k82857
32.1k82857
asked Nov 14 '18 at 1:21
Cutie PieCutie Pie
144
144
see my simple solution
– Geeocode
Nov 14 '18 at 2:07
Cutie Pie in my last update eliminated the only builtinlen()
function, take a look.
– Geeocode
Nov 14 '18 at 2:29
add a comment |
see my simple solution
– Geeocode
Nov 14 '18 at 2:07
Cutie Pie in my last update eliminated the only builtinlen()
function, take a look.
– Geeocode
Nov 14 '18 at 2:29
see my simple solution
– Geeocode
Nov 14 '18 at 2:07
see my simple solution
– Geeocode
Nov 14 '18 at 2:07
Cutie Pie in my last update eliminated the only builtin
len()
function, take a look.– Geeocode
Nov 14 '18 at 2:29
Cutie Pie in my last update eliminated the only builtin
len()
function, take a look.– Geeocode
Nov 14 '18 at 2:29
add a comment |
3 Answers
3
active
oldest
votes
Your use of <
rather than <=
is causing you problems. In the case where you have identical data points, it can easily lead to none of the if
statements firing. Specifically, take your first data point from each list, 1/1/3
, and use them in the conditions for your three if
statements:
(1 < 1) and (1 < 3): no, fails first part
(1 < 1) and (1 < 3): no, fails first part
(3 < 1) and (3 < 1): no, fails both parts
That causes an infinite loop since no action is being taken to modify the lists.
In any case, I think you're overly complicating things with a three-way merge when you can just do it as a couple of two-way merges:
def merge2(list1, list2):
result =
idx1 = 0
idx2 = 0
# Get lowest while both lists are active.
while idx1 < len(list1) and idx2 < len(list2):
if list1[idx1] <= list2[idx2]:
result.append(list1[idx1])
idx1 += 1
else:
result.append(list2[idx2])
idx2 += 1
# Get remainder of each list (only one will be active here).
while idx1 < len(list1):
result.append(list1[idx1])
idx1 += 1
while idx2 < len(list2):
result.append(list2[idx2])
idx2 += 1
return result
def merge(list1, list2, list3):
# Three-way is two two-ways.
return merge2(merge2(list1, list2), list3)
print(merge([1,4],[1,5,6],[3,7,9]))
This is slightly less efficient than a three-way but won't really make a difference unless you use truly large data sets (and, in my opinion, doing it this way results in a much "cleaner" program).
Of course the smart way to do this would be to use the actual facilities of the language. Even though you've stated you don't want to do that (and I'm not sure why that would be the case, other than maybe an artificial restriction for classwork), the Pythonic way would be:
def merge(list1, list2, list3):
allitems = [item for sublist in [list1, list2, list3] for item in sublist]
allitems.sort()
return allitems
And, in fact, you could make it handle arbitrary list quantities by providing a list of lists in the call, rather than a fixed number of lists:
def merge(listOfLists):
allitems = [item for sublist in listOfLists for item in sublist]
allitems.sort()
return allitems
print(merge([[1,4],[1,5,6],[3,7,9]])) # Three lists, but any number will work.
is there a way of doing this without any helper function?
– Cutie Pie
Nov 14 '18 at 1:44
@CutiePie, yes, there is. I call it the "ugly and not very well structured" method :-) In any case, don't think of it as a helper function, it's actually a useful two-way merge in its own right. Refactoring that out means you can use it in other situations.
– paxdiablo
Nov 14 '18 at 1:45
add a comment |
Or use non-builtin sorting way:
def merge(*l):
l=[x for i in l for x in i]
newl =
while l:
mi = l[0]
for x in l:
if x < mi:
mi = x
newl.append(mi)
l.remove(mi)
return newl
Now:
print(merge([1,4],[1,5,6],[3,7,9]))
Is:
[1, 1, 3, 4, 5, 6, 7, 9]
add a comment |
Whithout any builtin function and so:
a quite simple form:
def merge(*lists_in):
list_in =
for l in lists_in:
list_in += l
i = 0
while True:
if i == list_in.__len__() -1:
break
if list_in[i] > list_in[i+1]:
temp = list_in[i]
list_in[i] = list_in[i+1]
list_in[i+1] = temp
i = 0
else:
i += 1
return list_in
Testing it:
list1 = [1,4]
list2 = [1,5,6]
list3 = [3,7,9]
print(merge(list1, list2, list3))
Out:
[1, 1, 3, 4, 5, 6, 7, 9]
1
There really is no need to do a sort when the whole point of a merge is to merge already-sorted data :-)
– paxdiablo
Nov 14 '18 at 2:16
1
@paxdiablo haha, yeah
– Geeocode
Nov 14 '18 at 2:19
@paxdiablo with this update now using the merge() name is legal, but somone downvoted... :)
– Geeocode
Nov 14 '18 at 2:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291851%2fmerging-3-lists-in-ascending-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your use of <
rather than <=
is causing you problems. In the case where you have identical data points, it can easily lead to none of the if
statements firing. Specifically, take your first data point from each list, 1/1/3
, and use them in the conditions for your three if
statements:
(1 < 1) and (1 < 3): no, fails first part
(1 < 1) and (1 < 3): no, fails first part
(3 < 1) and (3 < 1): no, fails both parts
That causes an infinite loop since no action is being taken to modify the lists.
In any case, I think you're overly complicating things with a three-way merge when you can just do it as a couple of two-way merges:
def merge2(list1, list2):
result =
idx1 = 0
idx2 = 0
# Get lowest while both lists are active.
while idx1 < len(list1) and idx2 < len(list2):
if list1[idx1] <= list2[idx2]:
result.append(list1[idx1])
idx1 += 1
else:
result.append(list2[idx2])
idx2 += 1
# Get remainder of each list (only one will be active here).
while idx1 < len(list1):
result.append(list1[idx1])
idx1 += 1
while idx2 < len(list2):
result.append(list2[idx2])
idx2 += 1
return result
def merge(list1, list2, list3):
# Three-way is two two-ways.
return merge2(merge2(list1, list2), list3)
print(merge([1,4],[1,5,6],[3,7,9]))
This is slightly less efficient than a three-way but won't really make a difference unless you use truly large data sets (and, in my opinion, doing it this way results in a much "cleaner" program).
Of course the smart way to do this would be to use the actual facilities of the language. Even though you've stated you don't want to do that (and I'm not sure why that would be the case, other than maybe an artificial restriction for classwork), the Pythonic way would be:
def merge(list1, list2, list3):
allitems = [item for sublist in [list1, list2, list3] for item in sublist]
allitems.sort()
return allitems
And, in fact, you could make it handle arbitrary list quantities by providing a list of lists in the call, rather than a fixed number of lists:
def merge(listOfLists):
allitems = [item for sublist in listOfLists for item in sublist]
allitems.sort()
return allitems
print(merge([[1,4],[1,5,6],[3,7,9]])) # Three lists, but any number will work.
is there a way of doing this without any helper function?
– Cutie Pie
Nov 14 '18 at 1:44
@CutiePie, yes, there is. I call it the "ugly and not very well structured" method :-) In any case, don't think of it as a helper function, it's actually a useful two-way merge in its own right. Refactoring that out means you can use it in other situations.
– paxdiablo
Nov 14 '18 at 1:45
add a comment |
Your use of <
rather than <=
is causing you problems. In the case where you have identical data points, it can easily lead to none of the if
statements firing. Specifically, take your first data point from each list, 1/1/3
, and use them in the conditions for your three if
statements:
(1 < 1) and (1 < 3): no, fails first part
(1 < 1) and (1 < 3): no, fails first part
(3 < 1) and (3 < 1): no, fails both parts
That causes an infinite loop since no action is being taken to modify the lists.
In any case, I think you're overly complicating things with a three-way merge when you can just do it as a couple of two-way merges:
def merge2(list1, list2):
result =
idx1 = 0
idx2 = 0
# Get lowest while both lists are active.
while idx1 < len(list1) and idx2 < len(list2):
if list1[idx1] <= list2[idx2]:
result.append(list1[idx1])
idx1 += 1
else:
result.append(list2[idx2])
idx2 += 1
# Get remainder of each list (only one will be active here).
while idx1 < len(list1):
result.append(list1[idx1])
idx1 += 1
while idx2 < len(list2):
result.append(list2[idx2])
idx2 += 1
return result
def merge(list1, list2, list3):
# Three-way is two two-ways.
return merge2(merge2(list1, list2), list3)
print(merge([1,4],[1,5,6],[3,7,9]))
This is slightly less efficient than a three-way but won't really make a difference unless you use truly large data sets (and, in my opinion, doing it this way results in a much "cleaner" program).
Of course the smart way to do this would be to use the actual facilities of the language. Even though you've stated you don't want to do that (and I'm not sure why that would be the case, other than maybe an artificial restriction for classwork), the Pythonic way would be:
def merge(list1, list2, list3):
allitems = [item for sublist in [list1, list2, list3] for item in sublist]
allitems.sort()
return allitems
And, in fact, you could make it handle arbitrary list quantities by providing a list of lists in the call, rather than a fixed number of lists:
def merge(listOfLists):
allitems = [item for sublist in listOfLists for item in sublist]
allitems.sort()
return allitems
print(merge([[1,4],[1,5,6],[3,7,9]])) # Three lists, but any number will work.
is there a way of doing this without any helper function?
– Cutie Pie
Nov 14 '18 at 1:44
@CutiePie, yes, there is. I call it the "ugly and not very well structured" method :-) In any case, don't think of it as a helper function, it's actually a useful two-way merge in its own right. Refactoring that out means you can use it in other situations.
– paxdiablo
Nov 14 '18 at 1:45
add a comment |
Your use of <
rather than <=
is causing you problems. In the case where you have identical data points, it can easily lead to none of the if
statements firing. Specifically, take your first data point from each list, 1/1/3
, and use them in the conditions for your three if
statements:
(1 < 1) and (1 < 3): no, fails first part
(1 < 1) and (1 < 3): no, fails first part
(3 < 1) and (3 < 1): no, fails both parts
That causes an infinite loop since no action is being taken to modify the lists.
In any case, I think you're overly complicating things with a three-way merge when you can just do it as a couple of two-way merges:
def merge2(list1, list2):
result =
idx1 = 0
idx2 = 0
# Get lowest while both lists are active.
while idx1 < len(list1) and idx2 < len(list2):
if list1[idx1] <= list2[idx2]:
result.append(list1[idx1])
idx1 += 1
else:
result.append(list2[idx2])
idx2 += 1
# Get remainder of each list (only one will be active here).
while idx1 < len(list1):
result.append(list1[idx1])
idx1 += 1
while idx2 < len(list2):
result.append(list2[idx2])
idx2 += 1
return result
def merge(list1, list2, list3):
# Three-way is two two-ways.
return merge2(merge2(list1, list2), list3)
print(merge([1,4],[1,5,6],[3,7,9]))
This is slightly less efficient than a three-way but won't really make a difference unless you use truly large data sets (and, in my opinion, doing it this way results in a much "cleaner" program).
Of course the smart way to do this would be to use the actual facilities of the language. Even though you've stated you don't want to do that (and I'm not sure why that would be the case, other than maybe an artificial restriction for classwork), the Pythonic way would be:
def merge(list1, list2, list3):
allitems = [item for sublist in [list1, list2, list3] for item in sublist]
allitems.sort()
return allitems
And, in fact, you could make it handle arbitrary list quantities by providing a list of lists in the call, rather than a fixed number of lists:
def merge(listOfLists):
allitems = [item for sublist in listOfLists for item in sublist]
allitems.sort()
return allitems
print(merge([[1,4],[1,5,6],[3,7,9]])) # Three lists, but any number will work.
Your use of <
rather than <=
is causing you problems. In the case where you have identical data points, it can easily lead to none of the if
statements firing. Specifically, take your first data point from each list, 1/1/3
, and use them in the conditions for your three if
statements:
(1 < 1) and (1 < 3): no, fails first part
(1 < 1) and (1 < 3): no, fails first part
(3 < 1) and (3 < 1): no, fails both parts
That causes an infinite loop since no action is being taken to modify the lists.
In any case, I think you're overly complicating things with a three-way merge when you can just do it as a couple of two-way merges:
def merge2(list1, list2):
result =
idx1 = 0
idx2 = 0
# Get lowest while both lists are active.
while idx1 < len(list1) and idx2 < len(list2):
if list1[idx1] <= list2[idx2]:
result.append(list1[idx1])
idx1 += 1
else:
result.append(list2[idx2])
idx2 += 1
# Get remainder of each list (only one will be active here).
while idx1 < len(list1):
result.append(list1[idx1])
idx1 += 1
while idx2 < len(list2):
result.append(list2[idx2])
idx2 += 1
return result
def merge(list1, list2, list3):
# Three-way is two two-ways.
return merge2(merge2(list1, list2), list3)
print(merge([1,4],[1,5,6],[3,7,9]))
This is slightly less efficient than a three-way but won't really make a difference unless you use truly large data sets (and, in my opinion, doing it this way results in a much "cleaner" program).
Of course the smart way to do this would be to use the actual facilities of the language. Even though you've stated you don't want to do that (and I'm not sure why that would be the case, other than maybe an artificial restriction for classwork), the Pythonic way would be:
def merge(list1, list2, list3):
allitems = [item for sublist in [list1, list2, list3] for item in sublist]
allitems.sort()
return allitems
And, in fact, you could make it handle arbitrary list quantities by providing a list of lists in the call, rather than a fixed number of lists:
def merge(listOfLists):
allitems = [item for sublist in listOfLists for item in sublist]
allitems.sort()
return allitems
print(merge([[1,4],[1,5,6],[3,7,9]])) # Three lists, but any number will work.
edited Nov 14 '18 at 2:45
answered Nov 14 '18 at 1:39
paxdiablopaxdiablo
644k17712691692
644k17712691692
is there a way of doing this without any helper function?
– Cutie Pie
Nov 14 '18 at 1:44
@CutiePie, yes, there is. I call it the "ugly and not very well structured" method :-) In any case, don't think of it as a helper function, it's actually a useful two-way merge in its own right. Refactoring that out means you can use it in other situations.
– paxdiablo
Nov 14 '18 at 1:45
add a comment |
is there a way of doing this without any helper function?
– Cutie Pie
Nov 14 '18 at 1:44
@CutiePie, yes, there is. I call it the "ugly and not very well structured" method :-) In any case, don't think of it as a helper function, it's actually a useful two-way merge in its own right. Refactoring that out means you can use it in other situations.
– paxdiablo
Nov 14 '18 at 1:45
is there a way of doing this without any helper function?
– Cutie Pie
Nov 14 '18 at 1:44
is there a way of doing this without any helper function?
– Cutie Pie
Nov 14 '18 at 1:44
@CutiePie, yes, there is. I call it the "ugly and not very well structured" method :-) In any case, don't think of it as a helper function, it's actually a useful two-way merge in its own right. Refactoring that out means you can use it in other situations.
– paxdiablo
Nov 14 '18 at 1:45
@CutiePie, yes, there is. I call it the "ugly and not very well structured" method :-) In any case, don't think of it as a helper function, it's actually a useful two-way merge in its own right. Refactoring that out means you can use it in other situations.
– paxdiablo
Nov 14 '18 at 1:45
add a comment |
Or use non-builtin sorting way:
def merge(*l):
l=[x for i in l for x in i]
newl =
while l:
mi = l[0]
for x in l:
if x < mi:
mi = x
newl.append(mi)
l.remove(mi)
return newl
Now:
print(merge([1,4],[1,5,6],[3,7,9]))
Is:
[1, 1, 3, 4, 5, 6, 7, 9]
add a comment |
Or use non-builtin sorting way:
def merge(*l):
l=[x for i in l for x in i]
newl =
while l:
mi = l[0]
for x in l:
if x < mi:
mi = x
newl.append(mi)
l.remove(mi)
return newl
Now:
print(merge([1,4],[1,5,6],[3,7,9]))
Is:
[1, 1, 3, 4, 5, 6, 7, 9]
add a comment |
Or use non-builtin sorting way:
def merge(*l):
l=[x for i in l for x in i]
newl =
while l:
mi = l[0]
for x in l:
if x < mi:
mi = x
newl.append(mi)
l.remove(mi)
return newl
Now:
print(merge([1,4],[1,5,6],[3,7,9]))
Is:
[1, 1, 3, 4, 5, 6, 7, 9]
Or use non-builtin sorting way:
def merge(*l):
l=[x for i in l for x in i]
newl =
while l:
mi = l[0]
for x in l:
if x < mi:
mi = x
newl.append(mi)
l.remove(mi)
return newl
Now:
print(merge([1,4],[1,5,6],[3,7,9]))
Is:
[1, 1, 3, 4, 5, 6, 7, 9]
answered Nov 14 '18 at 1:39
U9-ForwardU9-Forward
18.1k51744
18.1k51744
add a comment |
add a comment |
Whithout any builtin function and so:
a quite simple form:
def merge(*lists_in):
list_in =
for l in lists_in:
list_in += l
i = 0
while True:
if i == list_in.__len__() -1:
break
if list_in[i] > list_in[i+1]:
temp = list_in[i]
list_in[i] = list_in[i+1]
list_in[i+1] = temp
i = 0
else:
i += 1
return list_in
Testing it:
list1 = [1,4]
list2 = [1,5,6]
list3 = [3,7,9]
print(merge(list1, list2, list3))
Out:
[1, 1, 3, 4, 5, 6, 7, 9]
1
There really is no need to do a sort when the whole point of a merge is to merge already-sorted data :-)
– paxdiablo
Nov 14 '18 at 2:16
1
@paxdiablo haha, yeah
– Geeocode
Nov 14 '18 at 2:19
@paxdiablo with this update now using the merge() name is legal, but somone downvoted... :)
– Geeocode
Nov 14 '18 at 2:51
add a comment |
Whithout any builtin function and so:
a quite simple form:
def merge(*lists_in):
list_in =
for l in lists_in:
list_in += l
i = 0
while True:
if i == list_in.__len__() -1:
break
if list_in[i] > list_in[i+1]:
temp = list_in[i]
list_in[i] = list_in[i+1]
list_in[i+1] = temp
i = 0
else:
i += 1
return list_in
Testing it:
list1 = [1,4]
list2 = [1,5,6]
list3 = [3,7,9]
print(merge(list1, list2, list3))
Out:
[1, 1, 3, 4, 5, 6, 7, 9]
1
There really is no need to do a sort when the whole point of a merge is to merge already-sorted data :-)
– paxdiablo
Nov 14 '18 at 2:16
1
@paxdiablo haha, yeah
– Geeocode
Nov 14 '18 at 2:19
@paxdiablo with this update now using the merge() name is legal, but somone downvoted... :)
– Geeocode
Nov 14 '18 at 2:51
add a comment |
Whithout any builtin function and so:
a quite simple form:
def merge(*lists_in):
list_in =
for l in lists_in:
list_in += l
i = 0
while True:
if i == list_in.__len__() -1:
break
if list_in[i] > list_in[i+1]:
temp = list_in[i]
list_in[i] = list_in[i+1]
list_in[i+1] = temp
i = 0
else:
i += 1
return list_in
Testing it:
list1 = [1,4]
list2 = [1,5,6]
list3 = [3,7,9]
print(merge(list1, list2, list3))
Out:
[1, 1, 3, 4, 5, 6, 7, 9]
Whithout any builtin function and so:
a quite simple form:
def merge(*lists_in):
list_in =
for l in lists_in:
list_in += l
i = 0
while True:
if i == list_in.__len__() -1:
break
if list_in[i] > list_in[i+1]:
temp = list_in[i]
list_in[i] = list_in[i+1]
list_in[i+1] = temp
i = 0
else:
i += 1
return list_in
Testing it:
list1 = [1,4]
list2 = [1,5,6]
list3 = [3,7,9]
print(merge(list1, list2, list3))
Out:
[1, 1, 3, 4, 5, 6, 7, 9]
edited Nov 14 '18 at 2:47
answered Nov 14 '18 at 2:05
GeeocodeGeeocode
2,44011021
2,44011021
1
There really is no need to do a sort when the whole point of a merge is to merge already-sorted data :-)
– paxdiablo
Nov 14 '18 at 2:16
1
@paxdiablo haha, yeah
– Geeocode
Nov 14 '18 at 2:19
@paxdiablo with this update now using the merge() name is legal, but somone downvoted... :)
– Geeocode
Nov 14 '18 at 2:51
add a comment |
1
There really is no need to do a sort when the whole point of a merge is to merge already-sorted data :-)
– paxdiablo
Nov 14 '18 at 2:16
1
@paxdiablo haha, yeah
– Geeocode
Nov 14 '18 at 2:19
@paxdiablo with this update now using the merge() name is legal, but somone downvoted... :)
– Geeocode
Nov 14 '18 at 2:51
1
1
There really is no need to do a sort when the whole point of a merge is to merge already-sorted data :-)
– paxdiablo
Nov 14 '18 at 2:16
There really is no need to do a sort when the whole point of a merge is to merge already-sorted data :-)
– paxdiablo
Nov 14 '18 at 2:16
1
1
@paxdiablo haha, yeah
– Geeocode
Nov 14 '18 at 2:19
@paxdiablo haha, yeah
– Geeocode
Nov 14 '18 at 2:19
@paxdiablo with this update now using the merge() name is legal, but somone downvoted... :)
– Geeocode
Nov 14 '18 at 2:51
@paxdiablo with this update now using the merge() name is legal, but somone downvoted... :)
– Geeocode
Nov 14 '18 at 2:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291851%2fmerging-3-lists-in-ascending-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
see my simple solution
– Geeocode
Nov 14 '18 at 2:07
Cutie Pie in my last update eliminated the only builtin
len()
function, take a look.– Geeocode
Nov 14 '18 at 2:29