From ae28145ce5b1c73ee66554dfbf583ef6ad4de0a5 Mon Sep 17 00:00:00 2001 From: dangbb <19020033@vnu.edu.vn> Date: Mon, 16 May 2022 16:42:04 +0700 Subject: [PATCH 1/4] Fixed bug where array length 2 can't be sorted --- sorts/iterative_merge_sort.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py index 5ee0badab9e6..0d0999802f07 100644 --- a/sorts/iterative_merge_sort.py +++ b/sorts/iterative_merge_sort.py @@ -51,7 +51,7 @@ def iter_merge_sort(input_list: list) -> list: # iteration for two-way merging p = 2 - while p < len(input_list): + while p <= len(input_list): # getting low, high and middle value for merge-sort of single list for i in range(0, len(input_list), p): low = i @@ -62,6 +62,7 @@ def iter_merge_sort(input_list: list) -> list: if p * 2 >= len(input_list): mid = i input_list = merge(input_list, 0, mid, len(input_list) - 1) + break p *= 2 return input_list @@ -69,5 +70,8 @@ def iter_merge_sort(input_list: list) -> list: if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() - unsorted = [int(item.strip()) for item in user_input.split(",")] + if user_input == '': + unsorted = [] + else: + unsorted = [int(item.strip()) for item in user_input.split(",")] print(iter_merge_sort(unsorted)) From d087b161ae6aa6401761101353579d110d0b5b2e Mon Sep 17 00:00:00 2001 From: dangbb <19020033@vnu.edu.vn> Date: Wed, 18 May 2022 01:11:30 +0700 Subject: [PATCH 2/4] Add MCC and DU path test Add test to conversions/octal_to_decimal.py and sorts\iterative_merge_sort.py --- conversions/octal_to_decimal.py | 38 +++++++++++++++++++++++++++++++++ sorts/iterative_merge_sort.py | 16 ++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/conversions/octal_to_decimal.py b/conversions/octal_to_decimal.py index 5a7373fef7e3..d8203bc856dd 100644 --- a/conversions/octal_to_decimal.py +++ b/conversions/octal_to_decimal.py @@ -2,12 +2,50 @@ def oct_to_decimal(oct_string: str) -> int: """ Convert a octal value to its decimal equivalent + >>> oct_to_decimal("") + Traceback (most recent call last): + ... + ValueError: Empty string was passed to the function + >>> oct_to_decimal("-") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("8") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("-e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("-8") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("1") + 1 + >>> oct_to_decimal("-1") + -1 + + >>> oct_to_decimal("12") 10 >>> oct_to_decimal(" 12 ") 10 >>> oct_to_decimal("-45") -37 + >>> oct_to_decimal("-") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("0") + 0 + >>> oct_to_decimal("-4055") + -2093 >>> oct_to_decimal("2-0Fm") Traceback (most recent call last): ... diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py index 0d0999802f07..67124e75840a 100644 --- a/sorts/iterative_merge_sort.py +++ b/sorts/iterative_merge_sort.py @@ -32,6 +32,22 @@ def iter_merge_sort(input_list: list) -> list: >>> iter_merge_sort([5, 9, 8, 7, 1, 2, 7]) [1, 2, 5, 7, 7, 8, 9] + >>> iter_merge_sort([1]) + [1] + >>> iter_merge_sort([2, 1]) + [1, 2] + >>> iter_merge_sort([2, 1, 3]) + [1, 2, 3] + >>> iter_merge_sort([4, 3, 2, 1]) + [1, 2, 3, 4] + >>> iter_merge_sort([5, 4, 3, 2, 1]) + [1, 2, 3, 4, 5] + >>> iter_merge_sort(['c', 'b', 'a']) + ['a', 'b', 'c'] + >>> iter_merge_sort([0.3, 0.2, 0.1]) + [0.1, 0.2, 0.3] + >>> iter_merge_sort(['dep', 'dang', 'trai']) + ['dang', 'dep', 'trai'] >>> iter_merge_sort([6]) [6] >>> iter_merge_sort([]) From a0dea47f76ad781a72fe0ee749585e4e63617036 Mon Sep 17 00:00:00 2001 From: John Law Date: Sat, 21 May 2022 21:26:01 +0800 Subject: [PATCH 3/4] "" --- sorts/iterative_merge_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py index 67124e75840a..327974fa61ae 100644 --- a/sorts/iterative_merge_sort.py +++ b/sorts/iterative_merge_sort.py @@ -86,7 +86,7 @@ def iter_merge_sort(input_list: list) -> list: if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() - if user_input == '': + if user_input == "": unsorted = [] else: unsorted = [int(item.strip()) for item in user_input.split(",")] From 4338f510e3eb540d635b737acced59aa3c510c2a Mon Sep 17 00:00:00 2001 From: John Law Date: Sat, 21 May 2022 21:30:35 +0800 Subject: [PATCH 4/4] Update octal_to_decimal.py --- conversions/octal_to_decimal.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/conversions/octal_to_decimal.py b/conversions/octal_to_decimal.py index d8203bc856dd..551311e2651e 100644 --- a/conversions/octal_to_decimal.py +++ b/conversions/octal_to_decimal.py @@ -30,8 +30,6 @@ def oct_to_decimal(oct_string: str) -> int: 1 >>> oct_to_decimal("-1") -1 - - >>> oct_to_decimal("12") 10 >>> oct_to_decimal(" 12 ")