From 583716147575ac8b83dc152bbaed607ed7ef03b2 Mon Sep 17 00:00:00 2001 From: Lester Kim Date: Fri, 17 Jul 2020 21:36:27 -0400 Subject: [PATCH 1/4] refactor(3_merge_sort): Refactor merge_sort --- .../solutions/.model_solution/solution.py | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py b/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py index dd001d6a..4d9ce4d3 100644 --- a/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py +++ b/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py @@ -1,32 +1,36 @@ -def merge_sort(num_list): - if len(num_list)>1: - mid = int(len(num_list)/2) - lefthalf = num_list[:mid] - righthalf = num_list[mid:] - - merge_sort(lefthalf) - merge_sort(righthalf) - - i=0 - j=0 - k=0 - - while i < len(lefthalf) and j < len(righthalf): - if lefthalf[i] < righthalf[j]: - num_list[k]=lefthalf[i] - i=i+1 - else: - num_list[k]=righthalf[j] - j=j+1 - k=k+1 - - while i < len(lefthalf): - num_list[k]=lefthalf[i] - i=i+1 - k=k+1 - - while j < len(righthalf): - num_list[k]=righthalf[j] - j=j+1 - k=k+1 - return num_list +def merge_sort(numbers: list) -> list: + numbers_length = len(numbers) + + if numbers_length < 2: + return numbers + + middle_index = numbers_length // 2 + left_half = numbers[:middle_index] + right_half = numbers[middle_index:] + + return _merge(merge_sort(left_half), merge_sort(right_half)) + + +def _merge(left: list, right: list) -> list: + total_length = len(left) + len(right) + merged_list = [None] * total_length + + left_index = right_index = merged_list_index = 0 + + while left_index < len(left) and right_index < len(right): + if left[left_index] < right[right_index]: + merged_list[merged_list_index] = left[left_index] + left_index += 1 + else: + merged_list[merged_list_index] = right[right_index] + right_index += 1 + + merged_list_index += 1 + + for list_index, list_ in ((left_index, left), (right_index, right)): + while list_index < len(list_): + merged_list[merged_list_index] = list_[list_index] + list_index += 1 + merged_list_index += 1 + + return merged_list From 16089b35e949646aeeca23b0cde21affabaf0600 Mon Sep 17 00:00:00 2001 From: Lester Kim Date: Fri, 17 Jul 2020 21:45:09 -0400 Subject: [PATCH 2/4] refactor(3_merge_sort): Use append instead of merged_list_index --- .../solutions/.model_solution/solution.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py b/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py index 4d9ce4d3..fd12bab6 100644 --- a/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py +++ b/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py @@ -13,24 +13,22 @@ def merge_sort(numbers: list) -> list: def _merge(left: list, right: list) -> list: total_length = len(left) + len(right) - merged_list = [None] * total_length + merged_list = [] - left_index = right_index = merged_list_index = 0 + left_index = right_index = 0 while left_index < len(left) and right_index < len(right): if left[left_index] < right[right_index]: - merged_list[merged_list_index] = left[left_index] + merged_list.append(left[left_index]) left_index += 1 else: - merged_list[merged_list_index] = right[right_index] + merged_list.append(right[right_index]) right_index += 1 - merged_list_index += 1 - for list_index, list_ in ((left_index, left), (right_index, right)): while list_index < len(list_): - merged_list[merged_list_index] = list_[list_index] + merged_list.append(list_[list_index]) list_index += 1 - merged_list_index += 1 return merged_list + From 9046dc91960fd28d9b2f0baafe147f3badf24c00 Mon Sep 17 00:00:00 2001 From: Lester Kim Date: Fri, 17 Jul 2020 21:51:30 -0400 Subject: [PATCH 3/4] fix(3_merge_sort): Remove unused integer variable --- .../3_merge_sort/solutions/.model_solution/solution.py | 1 - 1 file changed, 1 deletion(-) diff --git a/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py b/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py index fd12bab6..c74ec7e3 100644 --- a/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py +++ b/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py @@ -12,7 +12,6 @@ def merge_sort(numbers: list) -> list: def _merge(left: list, right: list) -> list: - total_length = len(left) + len(right) merged_list = [] left_index = right_index = 0 From 389d40655058fe712851f981983fe960686c5cff Mon Sep 17 00:00:00 2001 From: Lester Kim Date: Fri, 17 Jul 2020 22:15:06 -0400 Subject: [PATCH 4/4] style(3_merge_sort): Update variables --- .../solutions/.model_solution/solution.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py b/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py index c74ec7e3..b3002199 100644 --- a/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py +++ b/introduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py @@ -1,10 +1,8 @@ def merge_sort(numbers: list) -> list: - numbers_length = len(numbers) - - if numbers_length < 2: + if len(numbers) < 2: return numbers - middle_index = numbers_length // 2 + middle_index = len(numbers) // 2 left_half = numbers[:middle_index] right_half = numbers[middle_index:] @@ -13,7 +11,6 @@ def merge_sort(numbers: list) -> list: def _merge(left: list, right: list) -> list: merged_list = [] - left_index = right_index = 0 while left_index < len(left) and right_index < len(right): @@ -24,10 +21,10 @@ def _merge(left: list, right: list) -> list: merged_list.append(right[right_index]) right_index += 1 - for list_index, list_ in ((left_index, left), (right_index, right)): - while list_index < len(list_): - merged_list.append(list_[list_index]) - list_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