Skip to main content
deleted 1 character in body; edited tags; edited title
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

Merge Sort Cleanupalgorithm in Python

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)

Merge Sort Cleanup

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)

Merge Sort algorithm in Python

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)
fixing the bug around 0's
Source Link
Chris
  • 131
  • 3

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)

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:
        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)

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)
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Source Link
Chris
  • 131
  • 3
Loading