Compare commits

..

No commits in common. "e80302e2cfcf0538f6014e8c4f9b96faccdeaac5" and "04de01ae05dac338d2f3f82f0ca7ebddb9ac8166" have entirely different histories.

View File

@ -1,9 +1,7 @@
from typing import List, Tuple, Union from typing import List, Tuple, Union
def split_list( def split_list(timings: List[Union[int, float, str]]) -> Tuple[List[Union[int, float]]]:
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
@ -11,7 +9,7 @@ def split_list(
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 integers, it accepts a multiset ( list ) of positive 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
@ -27,7 +25,7 @@ def split_list(
([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: Timings must be a list of numbers ValueError: only numbers please
""" """
@ -44,29 +42,30 @@ def split_list(
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("Timings must be a list of numbers") raise ValueError("only numbers please")
if len(timings) == 0: if len(timings) == 0:
return ([], [], 0) return ([], [], 0)
elif len(timings) == 1: elif len(timings) == 1:
return ([timings[0]], [], timings[0]) return ([timings[0]], [], 1)
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_2 = [timings[j] for j in range(n) if j not in indices] distributed_timings_1 = [timings[j] for j in indices]
diff = abs(sum(distributed_timings_1) - sum(distributed_timings_2)) distributed_timings_2 = [timings[j] for j in range(n) if j not in indices]
if diff < smallest_diff: diff = abs(sum(distributed_timings_1) - sum(distributed_timings_2))
smallest_diff = diff if diff < smallest_diff:
result = ( smallest_diff = diff
distributed_timings_1, result = (
distributed_timings_2, distributed_timings_1,
smallest_diff, distributed_timings_2,
) smallest_diff,
return result )
return result
if __name__ == "__main__": if __name__ == "__main__":