Merge Sort Cleanup

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I am re-learning my fundamental algorithms, and have been told my Python is overly verbose.

Can you take a look at my merge sort algorithm and let me know how you would clean it up and make it more pythonic, if applicable?



def merge(left,right):
out =
l_rest =
i = left[0]
while i is not None:
if right:
if i < right[0]:
out.append(i)
left.remove(i)
else:
out.append(right[0])
right.remove(right[0])
else:
out.append(i)
left.remove(i)

if len(left) > 0:
i = left[0]
else:
i = None
for i in l_rest:
out.append(i)
for i in right:
out.append(i)
return out

def sort(lst):
if len(lst) == 1:
return lst

left = sort(lst[:len(lst)//2])
right = sort(lst[len(lst)//2:])
return merge(left,right)






share|improve this question





















  • Your code seems to have a bug: it doesn't include the pivot in the result.
    – Solomon Ucko
    Apr 4 at 16:18










  • @SolomonUcko Thanks, not sure I follow, but I will debug tonight.
    – Chris
    Apr 4 at 16:49










  • Sorry, at first I thought it was quicksort. What I meant was that, for example, if I sort a list of the numbers 0-99, repeated 100 times, then shuffled, the 0s are missing.
    – Solomon Ucko
    Apr 4 at 17:01






  • 1




    why not just out += l_rest + right instead of for i in l_rest: out.append(i); for i in right: out.append(i)
    – Yulia V
    Apr 4 at 18:21










  • @SolomonUcko fixed, that was nasty. Thanks. I chose to fix that and not fix YuliaV's suggestion inline... Not sure the best practice for implementing incremental improvements, but I like their idea.
    – Chris
    Apr 5 at 5:01
















up vote
1
down vote

favorite












I am re-learning my fundamental algorithms, and have been told my Python is overly verbose.

Can you take a look at my merge sort algorithm and let me know how you would clean it up and make it more pythonic, if applicable?



def merge(left,right):
out =
l_rest =
i = left[0]
while i is not None:
if right:
if i < right[0]:
out.append(i)
left.remove(i)
else:
out.append(right[0])
right.remove(right[0])
else:
out.append(i)
left.remove(i)

if len(left) > 0:
i = left[0]
else:
i = None
for i in l_rest:
out.append(i)
for i in right:
out.append(i)
return out

def sort(lst):
if len(lst) == 1:
return lst

left = sort(lst[:len(lst)//2])
right = sort(lst[len(lst)//2:])
return merge(left,right)






share|improve this question





















  • Your code seems to have a bug: it doesn't include the pivot in the result.
    – Solomon Ucko
    Apr 4 at 16:18










  • @SolomonUcko Thanks, not sure I follow, but I will debug tonight.
    – Chris
    Apr 4 at 16:49










  • Sorry, at first I thought it was quicksort. What I meant was that, for example, if I sort a list of the numbers 0-99, repeated 100 times, then shuffled, the 0s are missing.
    – Solomon Ucko
    Apr 4 at 17:01






  • 1




    why not just out += l_rest + right instead of for i in l_rest: out.append(i); for i in right: out.append(i)
    – Yulia V
    Apr 4 at 18:21










  • @SolomonUcko fixed, that was nasty. Thanks. I chose to fix that and not fix YuliaV's suggestion inline... Not sure the best practice for implementing incremental improvements, but I like their idea.
    – Chris
    Apr 5 at 5:01












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am re-learning my fundamental algorithms, and have been told my Python is overly verbose.

Can you take a look at my merge sort algorithm and let me know how you would clean it up and make it more pythonic, if applicable?



def merge(left,right):
out =
l_rest =
i = left[0]
while i is not None:
if right:
if i < right[0]:
out.append(i)
left.remove(i)
else:
out.append(right[0])
right.remove(right[0])
else:
out.append(i)
left.remove(i)

if len(left) > 0:
i = left[0]
else:
i = None
for i in l_rest:
out.append(i)
for i in right:
out.append(i)
return out

def sort(lst):
if len(lst) == 1:
return lst

left = sort(lst[:len(lst)//2])
right = sort(lst[len(lst)//2:])
return merge(left,right)






share|improve this question













I am re-learning my fundamental algorithms, and have been told my Python is overly verbose.

Can you take a look at my merge sort algorithm and let me know how you would clean it up and make it more pythonic, if applicable?



def merge(left,right):
out =
l_rest =
i = left[0]
while i is not None:
if right:
if i < right[0]:
out.append(i)
left.remove(i)
else:
out.append(right[0])
right.remove(right[0])
else:
out.append(i)
left.remove(i)

if len(left) > 0:
i = left[0]
else:
i = None
for i in l_rest:
out.append(i)
for i in right:
out.append(i)
return out

def sort(lst):
if len(lst) == 1:
return lst

left = sort(lst[:len(lst)//2])
right = sort(lst[len(lst)//2:])
return merge(left,right)








share|improve this question












share|improve this question




share|improve this question








edited Apr 5 at 4:59
























asked Apr 4 at 14:03









Chris

1063




1063











  • Your code seems to have a bug: it doesn't include the pivot in the result.
    – Solomon Ucko
    Apr 4 at 16:18










  • @SolomonUcko Thanks, not sure I follow, but I will debug tonight.
    – Chris
    Apr 4 at 16:49










  • Sorry, at first I thought it was quicksort. What I meant was that, for example, if I sort a list of the numbers 0-99, repeated 100 times, then shuffled, the 0s are missing.
    – Solomon Ucko
    Apr 4 at 17:01






  • 1




    why not just out += l_rest + right instead of for i in l_rest: out.append(i); for i in right: out.append(i)
    – Yulia V
    Apr 4 at 18:21










  • @SolomonUcko fixed, that was nasty. Thanks. I chose to fix that and not fix YuliaV's suggestion inline... Not sure the best practice for implementing incremental improvements, but I like their idea.
    – Chris
    Apr 5 at 5:01
















  • Your code seems to have a bug: it doesn't include the pivot in the result.
    – Solomon Ucko
    Apr 4 at 16:18










  • @SolomonUcko Thanks, not sure I follow, but I will debug tonight.
    – Chris
    Apr 4 at 16:49










  • Sorry, at first I thought it was quicksort. What I meant was that, for example, if I sort a list of the numbers 0-99, repeated 100 times, then shuffled, the 0s are missing.
    – Solomon Ucko
    Apr 4 at 17:01






  • 1




    why not just out += l_rest + right instead of for i in l_rest: out.append(i); for i in right: out.append(i)
    – Yulia V
    Apr 4 at 18:21










  • @SolomonUcko fixed, that was nasty. Thanks. I chose to fix that and not fix YuliaV's suggestion inline... Not sure the best practice for implementing incremental improvements, but I like their idea.
    – Chris
    Apr 5 at 5:01















Your code seems to have a bug: it doesn't include the pivot in the result.
– Solomon Ucko
Apr 4 at 16:18




Your code seems to have a bug: it doesn't include the pivot in the result.
– Solomon Ucko
Apr 4 at 16:18












@SolomonUcko Thanks, not sure I follow, but I will debug tonight.
– Chris
Apr 4 at 16:49




@SolomonUcko Thanks, not sure I follow, but I will debug tonight.
– Chris
Apr 4 at 16:49












Sorry, at first I thought it was quicksort. What I meant was that, for example, if I sort a list of the numbers 0-99, repeated 100 times, then shuffled, the 0s are missing.
– Solomon Ucko
Apr 4 at 17:01




Sorry, at first I thought it was quicksort. What I meant was that, for example, if I sort a list of the numbers 0-99, repeated 100 times, then shuffled, the 0s are missing.
– Solomon Ucko
Apr 4 at 17:01




1




1




why not just out += l_rest + right instead of for i in l_rest: out.append(i); for i in right: out.append(i)
– Yulia V
Apr 4 at 18:21




why not just out += l_rest + right instead of for i in l_rest: out.append(i); for i in right: out.append(i)
– Yulia V
Apr 4 at 18:21












@SolomonUcko fixed, that was nasty. Thanks. I chose to fix that and not fix YuliaV's suggestion inline... Not sure the best practice for implementing incremental improvements, but I like their idea.
– Chris
Apr 5 at 5:01




@SolomonUcko fixed, that was nasty. Thanks. I chose to fix that and not fix YuliaV's suggestion inline... Not sure the best practice for implementing incremental improvements, but I like their idea.
– Chris
Apr 5 at 5:01










1 Answer
1






active

oldest

votes

















up vote
-1
down vote













Starting a wiki with the suggested edits in the comments so far.



def merge(left,right):
out =
i = left[0]
while i is not None:
if right and i < right[0]:
out.append(i)
left.remove(i)
else:
out.append(right[0])
right.remove(right[0])

if len(left) > 0:
i = left[0]
else:
i = None

return out + left + right

def sort(lst):
if len(lst) == 1:
return lst

left = sort(lst[:len(lst)//2])
right = sort(lst[len(lst)//2:])
return merge(left,right)





share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    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: "196"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191254%2fmerge-sort-cleanup%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    -1
    down vote













    Starting a wiki with the suggested edits in the comments so far.



    def merge(left,right):
    out =
    i = left[0]
    while i is not None:
    if right and i < right[0]:
    out.append(i)
    left.remove(i)
    else:
    out.append(right[0])
    right.remove(right[0])

    if len(left) > 0:
    i = left[0]
    else:
    i = None

    return out + left + right

    def sort(lst):
    if len(lst) == 1:
    return lst

    left = sort(lst[:len(lst)//2])
    right = sort(lst[len(lst)//2:])
    return merge(left,right)





    share|improve this answer



























      up vote
      -1
      down vote













      Starting a wiki with the suggested edits in the comments so far.



      def merge(left,right):
      out =
      i = left[0]
      while i is not None:
      if right and i < right[0]:
      out.append(i)
      left.remove(i)
      else:
      out.append(right[0])
      right.remove(right[0])

      if len(left) > 0:
      i = left[0]
      else:
      i = None

      return out + left + right

      def sort(lst):
      if len(lst) == 1:
      return lst

      left = sort(lst[:len(lst)//2])
      right = sort(lst[len(lst)//2:])
      return merge(left,right)





      share|improve this answer

























        up vote
        -1
        down vote










        up vote
        -1
        down vote









        Starting a wiki with the suggested edits in the comments so far.



        def merge(left,right):
        out =
        i = left[0]
        while i is not None:
        if right and i < right[0]:
        out.append(i)
        left.remove(i)
        else:
        out.append(right[0])
        right.remove(right[0])

        if len(left) > 0:
        i = left[0]
        else:
        i = None

        return out + left + right

        def sort(lst):
        if len(lst) == 1:
        return lst

        left = sort(lst[:len(lst)//2])
        right = sort(lst[len(lst)//2:])
        return merge(left,right)





        share|improve this answer















        Starting a wiki with the suggested edits in the comments so far.



        def merge(left,right):
        out =
        i = left[0]
        while i is not None:
        if right and i < right[0]:
        out.append(i)
        left.remove(i)
        else:
        out.append(right[0])
        right.remove(right[0])

        if len(left) > 0:
        i = left[0]
        else:
        i = None

        return out + left + right

        def sort(lst):
        if len(lst) == 1:
        return lst

        left = sort(lst[:len(lst)//2])
        right = sort(lst[len(lst)//2:])
        return merge(left,right)






        share|improve this answer















        share|improve this answer



        share|improve this answer








        answered Apr 10 at 3:18



























        community wiki





        Chris























             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191254%2fmerge-sort-cleanup%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?