Skip to content

Commit 5bac76d

Browse files
dangbbpoyea
andauthored
Fix iter_merge_sort bug (TheAlgorithms#6153)
* Fixed bug where array length 2 can't be sorted * Add MCC and DU path test Add test to conversions/octal_to_decimal.py and sorts\iterative_merge_sort.py * "" * Update octal_to_decimal.py Co-authored-by: John Law <[email protected]>
1 parent dceb30a commit 5bac76d

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

conversions/octal_to_decimal.py

+36
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,48 @@ def oct_to_decimal(oct_string: str) -> int:
22
"""
33
Convert a octal value to its decimal equivalent
44
5+
>>> oct_to_decimal("")
6+
Traceback (most recent call last):
7+
...
8+
ValueError: Empty string was passed to the function
9+
>>> oct_to_decimal("-")
10+
Traceback (most recent call last):
11+
...
12+
ValueError: Non-octal value was passed to the function
13+
>>> oct_to_decimal("e")
14+
Traceback (most recent call last):
15+
...
16+
ValueError: Non-octal value was passed to the function
17+
>>> oct_to_decimal("8")
18+
Traceback (most recent call last):
19+
...
20+
ValueError: Non-octal value was passed to the function
21+
>>> oct_to_decimal("-e")
22+
Traceback (most recent call last):
23+
...
24+
ValueError: Non-octal value was passed to the function
25+
>>> oct_to_decimal("-8")
26+
Traceback (most recent call last):
27+
...
28+
ValueError: Non-octal value was passed to the function
29+
>>> oct_to_decimal("1")
30+
1
31+
>>> oct_to_decimal("-1")
32+
-1
533
>>> oct_to_decimal("12")
634
10
735
>>> oct_to_decimal(" 12 ")
836
10
937
>>> oct_to_decimal("-45")
1038
-37
39+
>>> oct_to_decimal("-")
40+
Traceback (most recent call last):
41+
...
42+
ValueError: Non-octal value was passed to the function
43+
>>> oct_to_decimal("0")
44+
0
45+
>>> oct_to_decimal("-4055")
46+
-2093
1147
>>> oct_to_decimal("2-0Fm")
1248
Traceback (most recent call last):
1349
...

sorts/iterative_merge_sort.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ def iter_merge_sort(input_list: list) -> list:
3232
3333
>>> iter_merge_sort([5, 9, 8, 7, 1, 2, 7])
3434
[1, 2, 5, 7, 7, 8, 9]
35+
>>> iter_merge_sort([1])
36+
[1]
37+
>>> iter_merge_sort([2, 1])
38+
[1, 2]
39+
>>> iter_merge_sort([2, 1, 3])
40+
[1, 2, 3]
41+
>>> iter_merge_sort([4, 3, 2, 1])
42+
[1, 2, 3, 4]
43+
>>> iter_merge_sort([5, 4, 3, 2, 1])
44+
[1, 2, 3, 4, 5]
45+
>>> iter_merge_sort(['c', 'b', 'a'])
46+
['a', 'b', 'c']
47+
>>> iter_merge_sort([0.3, 0.2, 0.1])
48+
[0.1, 0.2, 0.3]
49+
>>> iter_merge_sort(['dep', 'dang', 'trai'])
50+
['dang', 'dep', 'trai']
3551
>>> iter_merge_sort([6])
3652
[6]
3753
>>> iter_merge_sort([])
@@ -51,7 +67,7 @@ def iter_merge_sort(input_list: list) -> list:
5167

5268
# iteration for two-way merging
5369
p = 2
54-
while p < len(input_list):
70+
while p <= len(input_list):
5571
# getting low, high and middle value for merge-sort of single list
5672
for i in range(0, len(input_list), p):
5773
low = i
@@ -62,12 +78,16 @@ def iter_merge_sort(input_list: list) -> list:
6278
if p * 2 >= len(input_list):
6379
mid = i
6480
input_list = merge(input_list, 0, mid, len(input_list) - 1)
81+
break
6582
p *= 2
6683

6784
return input_list
6885

6986

7087
if __name__ == "__main__":
7188
user_input = input("Enter numbers separated by a comma:\n").strip()
72-
unsorted = [int(item.strip()) for item in user_input.split(",")]
89+
if user_input == "":
90+
unsorted = []
91+
else:
92+
unsorted = [int(item.strip()) for item in user_input.split(",")]
7393
print(iter_merge_sort(unsorted))

0 commit comments

Comments
 (0)