#πŸ”’ is this code pythonic?

33 messages Β· Page 1 of 1 (latest)

fierce summit
#

def merge_sort(a_list):
 if len(a_list) > 1:
  mid = len(a_list) // 2
  left_half = a_list[:mid]
  right_half = a_list[mid:]
  merge_sort(left_half)
  merge_sort(right_half)
  left_index = 0
  right_index = 0
  alist_index = 0
  
  if left_index < len(left_half) and right_ind < len(right_half):
   if left_half[left_index] <= right_half[right_index]:
    a_list[alist_index] = left_half[left_index]
    left_index += 1
   else:
    a_list[alist_index] = right_half[right_index]
    right_index += 1
   alist_index += 1
  
  if left_index < len(left_half):
   a_list[alist_index] = left_half[left_index]
   left_index += 1
   alist_index += 1
  
  if right_index < len(right_half):
   a_list[alist_index] = right_half[right_index]
   right_index += 1
   alist_index += 1
  
  return a_list
flat daggerBOT
#

@fierce summit

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pseudo pelican
#

!e


def merge_sort(a_list):
 if len(a_list) > 1:
  mid = len(a_list) // 2
  left_half = a_list[:mid]
  right_half = a_list[mid:]
  merge_sort(left_half)
  merge_sort(right_half)
  left_index = 0
  right_index = 0
  alist_index = 0
  
  if left_index < len(left_half) and right_index < len(right_half):
   if left_half[left_index] <= right_half[right_index]:
    a_list[alist_index] = left_half[left_index]
    left_index += 1
   else:
    a_list[alist_index] = right_half[right_index]
    right_index += 1
   alist_index += 1
  
  if left_index < len(left_half):
   a_list[alist_index] = left_half[left_index]
   left_index += 1
   alist_index += 1
  
  if right_index < len(right_half):
   a_list[alist_index] = right_half[right_index]
   right_index += 1
   alist_index += 1
  
  return a_list
print(merge_sort([7, 2, 3, 7, 9, 10, 3, 2, 6, 5]))
flat daggerBOT
pseudo pelican
#

that's my point
the code after the recursive calls don't properly merge the sorted lists

fierce summit
#

wait it worked yesterday, i think i made an error i will fix it

#

1 sec

pseudo pelican
#

did you test it on a smaller list? because merging the two lists should require some form of iteration, but you only have two sets of if statements

#

which might work if the list was like only 4 elements

fierce summit
#

im an idiot

#

i wrote an if statement instead of running a while loop

#

'''py

#
def merge_sort(a_list):
 if len(a_list) > 1:
  mid = len(a_list) // 2
  left_half = a_list[:mid]
  right_half = a_list[mid:]
  merge_sort(left_half)
  merge_sort(right_half)
  left_index = 0
  right_index = 0
  alist_index = 0
  
  while left_index < len(left_half) and right_ind < len(right_half):
   if left_half[left_index] <= right_half[right_index]:
    a_list[alist_index] = left_half[left_index]
    left_index += 1
   else:
    a_list[alist_index] = right_half[right_index]
    right_index += 1
   alist_index += 1
  
  while left_index < len(left_half):
   a_list[alist_index] = left_half[left_index]
   left_index += 1
   alist_index += 1
  
  while right_index < len(right_half):
   a_list[alist_index] = right_half[right_index]
   right_index += 1
   alist_index += 1
  
  return a_list
#

this

#

this works

pseudo pelican
#

!e

def merge_sort(a_list):
 if len(a_list) > 1:
  mid = len(a_list) // 2
  left_half = a_list[:mid]
  right_half = a_list[mid:]
  merge_sort(left_half)
  merge_sort(right_half)
  left_index = 0
  right_index = 0
  alist_index = 0
  
  while left_index < len(left_half) and right_index < len(right_half):
   if left_half[left_index] <= right_half[right_index]:
    a_list[alist_index] = left_half[left_index]
    left_index += 1
   else:
    a_list[alist_index] = right_half[right_index]
    right_index += 1
   alist_index += 1
  
  while left_index < len(left_half):
   a_list[alist_index] = left_half[left_index]
   left_index += 1
   alist_index += 1
  
  while right_index < len(right_half):
   a_list[alist_index] = right_half[right_index]
   right_index += 1
   alist_index += 1
  
  return a_list
print(merge_sort([7, 2, 3, 7, 9, 10, 3, 2, 6, 5]))
flat daggerBOT
pseudo pelican
#

right

#

now onto making it pythonic

#

a few things
the if statement at the top could be inverted into a guard clause, which reduces nesting

if len(a_list) <= 1:
    return a_list
(...rest of code...)
pseudo pelican
#

otherwise the code looks ok

fierce summit
#

the sort method is more effecient, i'll just use that

#

my code was useless

fierce summit
#

im noob

pseudo pelican
#

!e

code here
fierce summit
#

!e

flat daggerBOT
#
Missing required argument

code

fierce summit
#

!e print("Thanks")

flat daggerBOT
flat daggerBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.