From 44c77d11e5ba60e8a4aaa0c24491f5fa2ee69155 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Thu, 3 Nov 2022 00:54:49 +0300 Subject: [PATCH 1/3] Reduce the complexity of sorts/merge_insertion_sort.py --- sorts/merge_insertion_sort.py | 67 ++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/sorts/merge_insertion_sort.py b/sorts/merge_insertion_sort.py index ecaa535457f4..5080ebb073c0 100644 --- a/sorts/merge_insertion_sort.py +++ b/sorts/merge_insertion_sort.py @@ -14,6 +14,41 @@ from __future__ import annotations +def binary_search_insertion(sorted_list, item): + left = 0 + right = len(sorted_list) - 1 + while left <= right: + middle = (left + right) // 2 + if left == right: + if sorted_list[middle] < item: + left = middle + 1 + break + elif sorted_list[middle] < item: + left = middle + 1 + else: + right = middle - 1 + sorted_list.insert(left, item) + return sorted_list + + +def merge(left, right): + result = [] + while left and right: + if left[0][0] < right[0][0]: + result.append(left.pop(0)) + else: + result.append(right.pop(0)) + return result + left + right + + +def sortlist_2d(list_2d): + length = len(list_2d) + if length <= 1: + return list_2d + middle = length // 2 + return merge(sortlist_2d(list_2d[:middle]), sortlist_2d(list_2d[middle:])) + + def merge_insertion_sort(collection: list[int]) -> list[int]: """Pure implementation of merge-insertion sort algorithm in Python @@ -38,38 +73,6 @@ def merge_insertion_sort(collection: list[int]) -> list[int]: True """ - def binary_search_insertion(sorted_list, item): - left = 0 - right = len(sorted_list) - 1 - while left <= right: - middle = (left + right) // 2 - if left == right: - if sorted_list[middle] < item: - left = middle + 1 - break - elif sorted_list[middle] < item: - left = middle + 1 - else: - right = middle - 1 - sorted_list.insert(left, item) - return sorted_list - - def sortlist_2d(list_2d): - def merge(left, right): - result = [] - while left and right: - if left[0][0] < right[0][0]: - result.append(left.pop(0)) - else: - result.append(right.pop(0)) - return result + left + right - - length = len(list_2d) - if length <= 1: - return list_2d - middle = length // 2 - return merge(sortlist_2d(list_2d[:middle]), sortlist_2d(list_2d[middle:])) - if len(collection) <= 1: return collection From 07dcda68d0848c68aa29406f37826fa530f83a76 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Thu, 3 Nov 2022 01:17:58 +0300 Subject: [PATCH 2/3] Add tests --- sorts/merge_insertion_sort.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sorts/merge_insertion_sort.py b/sorts/merge_insertion_sort.py index 5080ebb073c0..4a5bdea0a33f 100644 --- a/sorts/merge_insertion_sort.py +++ b/sorts/merge_insertion_sort.py @@ -15,6 +15,10 @@ def binary_search_insertion(sorted_list, item): + """ + >>> binary_search_insertion([1, 2, 7, 9, 10], 4) + [1, 2, 4, 7, 9, 10] + """ left = 0 right = len(sorted_list) - 1 while left <= right: @@ -32,6 +36,10 @@ def binary_search_insertion(sorted_list, item): def merge(left, right): + """ + >>> merge([[1, 6], [9, 10]], [[2, 3], [4, 5], [7, 8]]) + [[1, 6], [2, 3], [4, 5], [7, 8], [9, 10]] + """ result = [] while left and right: if left[0][0] < right[0][0]: @@ -42,6 +50,10 @@ def merge(left, right): def sortlist_2d(list_2d): + """ + >>> sortlist_2d([[9, 10], [1, 6], [7, 8], [2, 3], [4, 5]]) + [[1, 6], [2, 3], [4, 5], [7, 8], [9, 10]] + """ length = len(list_2d) if length <= 1: return list_2d From d35d4745650f4574920999b057db8ee750db426d Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Thu, 3 Nov 2022 01:22:39 +0300 Subject: [PATCH 3/3] Lower the --max-complexity threshold in the file .flake8 --- .flake8 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.flake8 b/.flake8 index 834d1f63d13e..a40640eda80e 100644 --- a/.flake8 +++ b/.flake8 @@ -1,7 +1,7 @@ [flake8] max-line-length = 88 # max-complexity should be 10 -max-complexity = 20 +max-complexity = 17 extend-ignore = # Formatting style for `black` E203 # Whitespace before ':'