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
#π is this code pythonic?
33 messages Β· Page 1 of 1 (latest)
@fierce summit
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.
Closes after a period of inactivity, or when you send !close.
!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]))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[2, 7, 2, 7, 9, 10, 3, 2, 6, 5]
that's my point
the code after the recursive calls don't properly merge the sorted lists
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
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
!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]))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[2, 2, 3, 3, 5, 6, 7, 7, 9, 10]
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...)
I would probably extract the logic of merging the two sorted lists into its own method
otherwise the code looks ok
by the way how did you run the code in discord
im noob
!e
code here
!e
Missing required argument
code
!e print("Thanks")
:white_check_mark: Your 3.13 free threaded eval job has completed with return code 0.
Thanks
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.