mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Compare commits
6 Commits
9d77d9f917
...
885e8db94a
Author | SHA1 | Date | |
---|---|---|---|
|
885e8db94a | ||
|
a1bdd6e30c | ||
|
1b79142b68 | ||
|
84636cc08c | ||
|
6ad14ebe6e | ||
|
e82d110c44 |
@ -1,9 +1,6 @@
|
|||||||
from typing import List, Tuple, Union
|
|
||||||
|
|
||||||
|
|
||||||
def split_list(
|
def split_list(
|
||||||
timings: list[int | float | str],
|
timings: list[int | float | str],
|
||||||
) -> tuple[list[int | float], list[int | float], int | float]:
|
) -> tuple[list[int | float], list[int | float], int | float] | None:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
This algorithm is a brute-force search over (nearly) all 2^n
|
This algorithm is a brute-force search over (nearly) all 2^n
|
||||||
@ -30,35 +27,35 @@ def split_list(
|
|||||||
ValueError: Timings must be a list of numbers
|
ValueError: Timings must be a list of numbers
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
valid_timings = list[int | float]
|
||||||
for i, current_element in enumerate(timings):
|
for current_element in enumerate(timings):
|
||||||
if (
|
if (
|
||||||
isinstance(current_element, str)
|
isinstance(current_element, str)
|
||||||
and current_element.replace(".", "", 1).isdigit()
|
and current_element.replace(".", "", 1).isdigit()
|
||||||
):
|
):
|
||||||
is_current_elem_int = float(current_element).is_integer()
|
is_current_elem_int = float(current_element).is_integer()
|
||||||
if not is_current_elem_int:
|
if not is_current_elem_int:
|
||||||
timings[i] = abs(float(current_element))
|
valid_timings.append(abs(float(current_element)))
|
||||||
else:
|
else:
|
||||||
timings[i] = abs(int(float(current_element)))
|
valid_timings.append(abs(int(float(current_element))))
|
||||||
elif isinstance(current_element, (int, float)):
|
elif isinstance(current_element, (int, float)):
|
||||||
timings[i] = abs(current_element)
|
valid_timings.append(abs(current_element))
|
||||||
else:
|
else:
|
||||||
raise ValueError("Timings must be a list of numbers")
|
raise ValueError("Timings must be a list of numbers")
|
||||||
|
|
||||||
if len(timings) == 0:
|
if len(valid_timings) == 0:
|
||||||
return ([], [], 0)
|
return ([], [], 0)
|
||||||
elif len(timings) == 1:
|
elif len(valid_timings) == 1:
|
||||||
return ([timings[0]], [], timings[0])
|
return ([valid_timings[0]], [], valid_timings[0])
|
||||||
|
|
||||||
result = None
|
result = None
|
||||||
n = len(timings)
|
n = len(valid_timings)
|
||||||
smallest_diff = float("inf")
|
smallest_diff = float("inf")
|
||||||
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 = [valid_timings[j] for j in indices]
|
||||||
diff = abs(sum(distributed_timings_1) - sum(distributed_timings_2))
|
distributed_timings_2 = [valid_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:
|
if diff < smallest_diff:
|
||||||
smallest_diff = diff
|
smallest_diff = diff
|
||||||
result = (
|
result = (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user