Compare commits

...

4 Commits

Author SHA1 Message Date
github-actions
04de01ae05 updating DIRECTORY.md 2023-07-29 12:27:13 +00:00
Margaret
bc5ec66512 following recommendations 2023-07-29 15:26:52 +03:00
Margaret
3b0407f654 following recommendations 2023-07-29 15:24:19 +03:00
Margaret
0ffa117533 following recommendations 2023-07-29 15:23:39 +03:00
3 changed files with 73 additions and 79 deletions

View File

@ -1076,7 +1076,6 @@
* [Non Preemptive Shortest Job First](scheduling/non_preemptive_shortest_job_first.py)
* [Round Robin](scheduling/round_robin.py)
* [Shortest Job First](scheduling/shortest_job_first.py)
* [Split Workload](scheduling/split_workload.py)
## Searches
* [Binary Search](searches/binary_search.py)
@ -1140,6 +1139,7 @@
* [Shell Sort](sorts/shell_sort.py)
* [Shrink Shell Sort](sorts/shrink_shell_sort.py)
* [Slowsort](sorts/slowsort.py)
* [Split Workload](sorts/split_workload.py)
* [Stooge Sort](sorts/stooge_sort.py)
* [Strand Sort](sorts/strand_sort.py)
* [Tim Sort](sorts/tim_sort.py)

View File

@ -1,78 +0,0 @@
from math import fabs
def split_list(timings: list) -> tuple:
"""
this is a case of the partition problem.
it accepts a multiset ( list ) of positive integers,
distributes them, and returns a tuple, containing two lists,
with minimal difference between their sums
>>> split_list([27, 21, 92, 87, 1, 32])
([27, 21, 87], [92, 1, 32], 10)
>>> split_list([52, 385, 9956, 25, 2367, 1111, 17, 925])
([9956], [52, 385, 25, 2367, 1111, 17, 925], 5074)
>>> split_list([12, 10, 11, 9])
([10, 11], [12, 9], 0)
>>> split_list([-1551, 2712, 2325, 2623])
([1551, 2712], [2325, 2623], 685)
>>> split_list(["12.5", "10", "11", "9"])
([10, 11], [12.5, 9], 0.5)
>>> split_list(["twelve", "ten", "eleven", "nine"])
([0], [0, 0, 0], 0)
"""
result = None
def split_workload(arr: list) -> tuple:
if len(arr) == 0:
return ([], [], 0)
if len(arr) == 1:
return ([arr[0]], [], 1)
else:
n = len(arr)
smallest_diff = float("inf")
if any(c < 0 for c in arr):
raise ValueError("Numbers can only be non-negative")
for i in range(1, 2**n - 1):
indices = [j for j in range(n) if (i & (1 << j)) != 0]
distributed_timings_1 = [arr[j] for j in indices]
distributed_timings_2 = [arr[j] for j in range(n) if j not in indices]
diff = abs(sum(distributed_timings_1) - sum(distributed_timings_2))
smallest_diff = min(smallest_diff, diff)
result = (
distributed_timings_1,
distributed_timings_2,
smallest_diff,
)
return result
try:
result = split_workload(timings)
except TypeError:
for val in range(0, len(timings)):
current_element = timings[val]
if (
isinstance(current_element, str)
and current_element.replace(".", "", 1).isdigit()
):
is_current_elem_int = float(current_element).is_integer()
if not is_current_elem_int:
timings[val] = fabs(float(current_element))
else:
timings[val] = abs(int(float(current_element)))
else:
timings[val] = 0
result = split_workload(timings)
except ValueError:
timings = [c * -1 if c < 0 else c for c in timings]
result = split_workload(timings)
return result
if __name__ == "__main__":
__import__("doctest").testmod()

72
sorts/split_workload.py Normal file
View File

@ -0,0 +1,72 @@
from typing import List, Tuple, Union
def split_list(timings: List[Union[int, float, str]]) -> Tuple[List[Union[int, float]]]:
"""
This algorithm is a brute-force search over (nearly) all 2^n
possible partitions and was created for educational purposes.
the asymptotic runtime of this code is: O(n * 2^n)
this is a case of the partition problem.
it accepts a multiset ( list ) of positive integers,
distributes them, and returns a tuple, containing two lists,
with minimal difference between their sums
>>> split_list([27, 21, 92, 87, 1, 32])
([27, 21, 87], [92, 1, 32], 10)
>>> split_list([52, 385, 9956, 25, 2367, 1111, 17, 925])
([9956], [52, 385, 25, 2367, 1111, 17, 925], 5074)
>>> split_list([12, 10, 11, 9])
([10, 11], [12, 9], 0)
>>> split_list([-1551, 2712, 2325, 2623])
([1551, 2712], [2325, 2623], 685)
>>> split_list(["12.5", "10", "11", "9"])
([10, 11], [12.5, 9], 0.5)
>>> split_list(["twelve", "ten", "eleven", "nine"])
Traceback (most recent call last):
ValueError: only numbers please
"""
for i, current_element in enumerate(timings):
if (
isinstance(current_element, str)
and current_element.replace(".", "", 1).isdigit()
):
is_current_elem_int = float(current_element).is_integer()
if not is_current_elem_int:
timings[i] = abs(float(current_element))
else:
timings[i] = abs(int(float(current_element)))
elif isinstance(current_element, (int, float)):
timings[i] = abs(current_element)
else:
raise ValueError("only numbers please")
if len(timings) == 0:
return ([], [], 0)
elif len(timings) == 1:
return ([timings[0]], [], 1)
else:
result = None
n = len(timings)
smallest_diff = float("inf")
all_nums_positive = [c >= 0 for c in timings]
for i in range(1, 2**n - 1):
indices = [j for j in range(n) if (i & (1 << j)) != 0]
distributed_timings_1 = [timings[j] for j in indices]
distributed_timings_2 = [timings[j] for j in range(n) if j not in indices]
diff = abs(sum(distributed_timings_1) - sum(distributed_timings_2))
if diff < smallest_diff:
smallest_diff = diff
result = (
distributed_timings_1,
distributed_timings_2,
smallest_diff,
)
return result
if __name__ == "__main__":
__import__("doctest").testmod()