mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Fix iter_merge_sort
bug (#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 <johnlaw.po@gmail.com>
This commit is contained in:
parent
dceb30aad6
commit
5bac76d7a5
|
@ -2,12 +2,48 @@ def oct_to_decimal(oct_string: str) -> int:
|
||||||
"""
|
"""
|
||||||
Convert a octal value to its decimal equivalent
|
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")
|
>>> oct_to_decimal("12")
|
||||||
10
|
10
|
||||||
>>> oct_to_decimal(" 12 ")
|
>>> oct_to_decimal(" 12 ")
|
||||||
10
|
10
|
||||||
>>> oct_to_decimal("-45")
|
>>> oct_to_decimal("-45")
|
||||||
-37
|
-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")
|
>>> oct_to_decimal("2-0Fm")
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
|
|
|
@ -32,6 +32,22 @@ def iter_merge_sort(input_list: list) -> list:
|
||||||
|
|
||||||
>>> iter_merge_sort([5, 9, 8, 7, 1, 2, 7])
|
>>> iter_merge_sort([5, 9, 8, 7, 1, 2, 7])
|
||||||
[1, 2, 5, 7, 7, 8, 9]
|
[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])
|
>>> iter_merge_sort([6])
|
||||||
[6]
|
[6]
|
||||||
>>> iter_merge_sort([])
|
>>> iter_merge_sort([])
|
||||||
|
@ -51,7 +67,7 @@ def iter_merge_sort(input_list: list) -> list:
|
||||||
|
|
||||||
# iteration for two-way merging
|
# iteration for two-way merging
|
||||||
p = 2
|
p = 2
|
||||||
while p < len(input_list):
|
while p <= len(input_list):
|
||||||
# getting low, high and middle value for merge-sort of single list
|
# getting low, high and middle value for merge-sort of single list
|
||||||
for i in range(0, len(input_list), p):
|
for i in range(0, len(input_list), p):
|
||||||
low = i
|
low = i
|
||||||
|
@ -62,6 +78,7 @@ def iter_merge_sort(input_list: list) -> list:
|
||||||
if p * 2 >= len(input_list):
|
if p * 2 >= len(input_list):
|
||||||
mid = i
|
mid = i
|
||||||
input_list = merge(input_list, 0, mid, len(input_list) - 1)
|
input_list = merge(input_list, 0, mid, len(input_list) - 1)
|
||||||
|
break
|
||||||
p *= 2
|
p *= 2
|
||||||
|
|
||||||
return input_list
|
return input_list
|
||||||
|
@ -69,5 +86,8 @@ def iter_merge_sort(input_list: list) -> list:
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
user_input = input("Enter numbers separated by a comma:\n").strip()
|
user_input = input("Enter numbers separated by a comma:\n").strip()
|
||||||
|
if user_input == "":
|
||||||
|
unsorted = []
|
||||||
|
else:
|
||||||
unsorted = [int(item.strip()) for item in user_input.split(",")]
|
unsorted = [int(item.strip()) for item in user_input.split(",")]
|
||||||
print(iter_merge_sort(unsorted))
|
print(iter_merge_sort(unsorted))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user