Ich habe eine Reihe von Schwimmern und eine Zielsumme. Ich versuche, alle möglichen Kombinationen von Elementen zu finden, die zur Zielsumme addieren würden. /p>
Einschränkungen: < /p>
Der gleiche Wert kann im Array mehrmals angezeigt werden. < /li>
< li>
0 List[Tuple[float, ...]]:
arr = np.asarray(arr, dtype=np.float64)
n = len(arr)
# HM to store combinations Key: Sum, Value: indices
dp = defaultdict(set)
# Convert sum to int
def get_sum_key(value: float) -> int:
return int(round(value / epsilon))
# Add all individual elements
for i, num in enumerate(arr):
dp[get_sum_key(num)].add((i,))
result = []
# Combining each new number with all existing combinations
for i in range(n):
curr_num = arr[i]
# Make a copy of current sums to avoid modifying while iterating
current_sums = list(dp.items())
for sum_key, combinations in current_sums:
new_sum = sum_key * epsilon + curr_num
new_sum_key = get_sum_key(new_sum)
# Add new combination
for comb in combinations:
# Check to ensure no duplicate indices
if i > max(comb):
new_comb = comb + (i,)
dp[new_sum_key].add(new_comb)
# Check for target sum
if (abs(new_sum - target) < epsilon and
len(new_comb) >= min_elements):
result.append(tuple(float(arr[idx]) for idx in new_comb))
return sorted(result, key=len)
arr=generate_random_array(size=20, min_val=1000, max_val=100000, seed=42)
target_sum=arr[1]+arr[2]+arr[4]+arr[5]
combinations = find_target_sum_combinations(arr, target_sum)
print(f"\nCombinations that sum to {target_sum}:")
for comb in combinations:
print(f"{comb} = {sum(comb)}")
Ich habe eine Reihe von Schwimmern und eine Zielsumme. Ich versuche, alle möglichen Kombinationen von Elementen zu finden, die zur Zielsumme addieren würden. /p> Einschränkungen: < /p> [list] [*] Der gleiche Wert kann im Array mehrmals angezeigt werden. < /li> < li>[code]0 < array.size < 1000000[/code] [*][code]0 List[Tuple[float, ...]]:
arr = np.asarray(arr, dtype=np.float64) n = len(arr)
# HM to store combinations Key: Sum, Value: indices dp = defaultdict(set)
# Convert sum to int def get_sum_key(value: float) -> int: return int(round(value / epsilon))
# Add all individual elements for i, num in enumerate(arr): dp[get_sum_key(num)].add((i,))
result = []
# Combining each new number with all existing combinations for i in range(n): curr_num = arr[i] # Make a copy of current sums to avoid modifying while iterating current_sums = list(dp.items())
for sum_key, combinations in current_sums: new_sum = sum_key * epsilon + curr_num new_sum_key = get_sum_key(new_sum)
# Add new combination for comb in combinations: # Check to ensure no duplicate indices if i > max(comb): new_comb = comb + (i,) dp[new_sum_key].add(new_comb)
# Check for target sum if (abs(new_sum - target) < epsilon and len(new_comb) >= min_elements): result.append(tuple(float(arr[idx]) for idx in new_comb))
combinations = find_target_sum_combinations(arr, target_sum) print(f"\nCombinations that sum to {target_sum}:") for comb in combinations: print(f"{comb} = {sum(comb)}") [/code]
In meiner Deep-Learning-Übung musste ich einen Parameter D1 mit der gleichen Größe wie A1 initialisieren, also habe ich Folgendes getan:
D1 = np.random.randn(A1.shape ,A1.shape )
Wenn ich mein Skript ausführen möchte, habe ich den Fehler erhalten: modulenotFoundError: kein Modul mit dem Namen 'Numpy' . Aber das Modul ist bereits installiert, wie ich die Antwort auf den...
Ich unterhalte Opentsne und möchte Numpy2 unterstützen. Das Paket hängt von Numpy ab und verwendet Cython, daher ist der Build-Prozess etwas mehr involviert. Daher wechselte mein pyproject.toml in...