英文:
Python: Modify second list to achieve specific sum for the first item with least changes
问题
def find_top_n_exceeding(nums, num):
n = 0
total_sum = 0
for i, value in enumerate(nums):
total_sum += value
n = i + 1
if total_sum >= num:
break
return nums[:n-1]+[(num-sum(nums[:n-1]))]
# 示例用法
input_list = [5, 10, 15, 20, 25]
target_sum = 47
output_list = find_top_n_exceeding(input_list, target_sum)
print(output_list)
英文:
How can I modify the second list to achieve a specific sum for the first item while making the least possible changes?
For example I have two lists, list1 = [100, 50] and list2 = [40, 50, 60, 70]. The desired output, list3, should have the value adjusted by breaking down some values from list2.
For example, we try to find the number by order, summing the first three values in list2 (40 + 50 + 60), the total exceeds the first item in list1 (100). To meet this condition, the value 60 is split into two parts, making list3 [40, 50, 10, 50, 70]. The process is similar for the second item in list1, utilizing the remaining 50 as the first item, resulting in the final output list3 as [40, 50, 10, 50, 70].
so far that's what I have
def find_top_n_exceeding(nums, num):
n = 0
total_sum = 0
for i, value in enumerate(nums):
total_sum += value
n = i + 1
if total_sum >= num:
break
return nums[:n-1]+[(num-sum(nums[:n-1]))]
# Example usage
input_list = [5, 10, 15, 20, 25]
target_sum = 47
output_list = find_top_n_exceeding(input_list, target_sum)
print(output_list)
答案1
得分: 0
以下是您要翻译的部分:
"Algorithm below will navigate through list1 and list2 simultaneously.
For all elements in list1:
- Add up remaining elements of list2 until the sum is Greater than or equal to the element in list1
- If the sum is exactly equal to the element in list1, continue this loop
- If we need to split value:
- Calculate overflow, split value and replace list2 with the surplus
- Mark the surplus with the remaining elements by setting the pointer back, and continue the loop
Note that list2 will be completely messed up as every element is replaced with the excess values (if those exists).
Thus, if you don't want list2 to be changed, you may want to put this in function and copy list2 temporarily with:
- Using copy library
import copy temp_list2 = copy.copy(list2)
- Using slices
temp_list2 = list2[:]
Hope this will work for you!
list3 = []
# next element (index) of list2 to be added/splitted
next_elem2 = 0
for elem1 in list1:
temp_sum = 0
while temp_sum < elem1:
temp_sum += list2[next_elem2]
list3.append(list2[next_elem2])
next_elem2 += 1
if temp_sum == elem1:
continue
else:
# we need to split value
overflow = temp_sum - elem1
last_added = list3.pop()
list3.append(last_added - overflow)
# the overflow should be used in the next lists also
next_elem2 -= 1
list2[next_elem2] = overflow
# Elements from (and including) next_elem2 are never searched.
# We need to add these also to list3
list3 += list2[next_elem2:]
print(list3)
如果这是您期望的内容,我强烈建议您彻底阅读程序,直到完全理解其中的操作原理。
英文:
Algorithm below will navigate through list1 and list2 simultaneously.
For all elements in list1:
- Add up remaining elements of list2 until the sum is Greater than or equal to the element in list1
- If the sum is exactly equal to the element in list1, continue this loop
- If we need to split value:
- Calculate overflow, split value and replace list2 with the surplus
- Mark the surplus with the remaining elements by setting the pointer back, and continue the loop
Note that list2 will be completely messed up as every element is replaced with the excess values (if those exists).
Thus, if you don't want list2 to be changed, you may want to put this in function and copy list2 temporarily with:
- Using copy library
import copy temp_list2 = copy.copy(list2)
- Using slices
temp_list2 = list2[:]
Hope this will work for you!
list3 = []
# next element (index) of list2 to be added/splitted
next_elem2 = 0
for elem1 in list1:
temp_sum = 0
while temp_sum < elem1:
temp_sum += list2[next_elem2]
list3.append(list2[next_elem2])
next_elem2 += 1
if temp_sum == elem1:
continue
else:
# we need to split value
overflow = temp_sum - elem1
last_added = list3.pop()
list3.append(last_added - overflow)
# the overflow should be used in the next lists also
next_elem2 -= 1
list2[next_elem2] = overflow
# Elements from (and including) next_elem2 are never searched.
# We need to add these also to list3
list3 += list2[next_elem2:]
print(list3)
If this is what you expected, I strongly recommend you to read through the program thoroughly until you completely understand what's going on
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论