Compare commits

..

4 Commits

Author SHA1 Message Date
pre-commit-ci[bot]
e80302e2cf [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-08-02 14:33:08 +00:00
Margaret
f7198f1e89
Update split_workload.py 2023-08-02 17:32:14 +03:00
Margaret
4277c8c58a
Following recommendations on sorts/split_workload.py
Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
2023-08-02 17:28:08 +03:00
Margaret
5e6590b85c following recommendations 2023-08-02 17:27:39 +03:00

View File

@ -1,7 +1,9 @@
from typing import List, Tuple, Union from typing import List, Tuple, Union
def split_list(timings: List[Union[int, float, str]]) -> Tuple[List[Union[int, float]]]: def split_list(
timings: List[Union[int, float, str]]
) -> Tuple[List[Union[int, float]], List[Union[int, float]], Union[int, float]]:
""" """
This algorithm is a brute-force search over (nearly) all 2^n This algorithm is a brute-force search over (nearly) all 2^n
@ -9,7 +11,7 @@ def split_list(timings: List[Union[int, float, str]]) -> Tuple[List[Union[int, f
the asymptotic runtime of this code is: O(n * 2^n) the asymptotic runtime of this code is: O(n * 2^n)
this is a case of the partition problem. this is a case of the partition problem.
it accepts a multiset ( list ) of positive integers, it accepts a multiset ( list ) of integers,
distributes them, and returns a tuple, containing two lists, distributes them, and returns a tuple, containing two lists,
with minimal difference between their sums with minimal difference between their sums
@ -25,7 +27,7 @@ def split_list(timings: List[Union[int, float, str]]) -> Tuple[List[Union[int, f
([10, 11], [12.5, 9], 0.5) ([10, 11], [12.5, 9], 0.5)
>>> split_list(["twelve", "ten", "eleven", "nine"]) >>> split_list(["twelve", "ten", "eleven", "nine"])
Traceback (most recent call last): Traceback (most recent call last):
ValueError: only numbers please ValueError: Timings must be a list of numbers
""" """
@ -42,20 +44,19 @@ def split_list(timings: List[Union[int, float, str]]) -> Tuple[List[Union[int, f
elif isinstance(current_element, (int, float)): elif isinstance(current_element, (int, float)):
timings[i] = abs(current_element) timings[i] = abs(current_element)
else: else:
raise ValueError("only numbers please") raise ValueError("Timings must be a list of numbers")
if len(timings) == 0: if len(timings) == 0:
return ([], [], 0) return ([], [], 0)
elif len(timings) == 1: elif len(timings) == 1:
return ([timings[0]], [], 1) return ([timings[0]], [], timings[0])
else:
result = None result = None
n = len(timings) n = len(timings)
smallest_diff = float("inf") smallest_diff = float("inf")
all_nums_positive = [c >= 0 for c in timings] all_nums_positive = [c >= 0 for c in timings]
for i in range(1, 2**n - 1): for i in range(1, 2**n - 1):
indices = [j for j in range(n) if (i & (1 << j)) != 0] 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] 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)) diff = abs(sum(distributed_timings_1) - sum(distributed_timings_2))
if diff < smallest_diff: if diff < smallest_diff: