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
def split_list(
timings: List[Union[int, float, str]]
) -> Tuple[List[Union[int, float]], List[Union[int, float]], Union[int, float]]:
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
@ -11,7 +9,7 @@ def split_list(
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 integers,
it accepts a multiset ( list ) of positive integers,
distributes them, and returns a tuple, containing two lists,
with minimal difference between their sums
@ -27,7 +25,7 @@ def split_list(
([10, 11], [12.5, 9], 0.5)
>>> split_list(["twelve", "ten", "eleven", "nine"])
Traceback (most recent call last):
ValueError: Timings must be a list of numbers
ValueError: only numbers please
"""
@ -44,19 +42,20 @@ def split_list(
elif isinstance(current_element, (int, float)):
timings[i] = abs(current_element)
else:
raise ValueError("Timings must be a list of numbers")
raise ValueError("only numbers please")
if len(timings) == 0:
return ([], [], 0)
elif len(timings) == 1:
return ([timings[0]], [], timings[0])
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]
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: