Skip to content

Conversation

@LesterKim
Copy link

Refactor merge_sort with non-public _merge function; add typing and descriptive variable names. This includes a fix for returning a list if len(num_list) < 2.

Comment on lines +12 to +29
def _merge(left: list, right: list) -> list:
merged_list = []
left_index = right_index = 0

while left_index < len(left) and right_index < len(right):
if left[left_index] < right[right_index]:
merged_list.append(left[left_index])
left_index += 1
else:
merged_list.append(right[right_index])
right_index += 1

for index, numbers in ((left_index, left), (right_index, right)):
while index < len(numbers):
merged_list.append(numbers[index])
index += 1

return merged_list
Copy link
Author

@LesterKim LesterKim Jul 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once students learn about Python classes, they can create a non-public class _ListMerger that contains this function as its public method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant