因此,我正在尝试编写一个代码,该代码采用一个介于1到10之间的数字列表,并找到大于或等于10的总和,然后从列表中删除这些数字。这是一个难题:首先,您需要检查列表中是否有10个数字,任何两个数字的总和为10,然后是3个数字,依此类推,直到5。此外,数字越小越好。因此,求和时需要去除大多数数字。到目前为止,我设法做到了两对:
n = input("How many numbers in the list? \n")
throw = []
for i in range(int(n)):
throw.append(random.randint(1, 10))
throw.sort()
increments = 0
print(throw)
increments += throw.count(10)
throw = list(filter(lambda i: i != 10, throw))
high = len(throw)-1
low = 0
acceptable_couples = []
get_rid = []
while low < high:
sums = throw[high] + throw[low]
if sums >= 10:
increments += 1
get_rid.append(throw[high])
get_rid.append(throw[low])
acceptable_couples.append((throw[high], throw[low]))
high -= 1
low += 1
else:
low += 1
for i in get_rid:
throw.remove(i)
我也做了3对,并考虑对4和5应用相同的方法:
while len(throw) >= 3:
z = 0
x = list(itertools.combinations(throw, 3))
for couple in x:
if sum(couple) >= 10:
z += 1
i = list(couple)
increments += 1
for j in couple:
throw.remove(j)
break
else:
continue
if z == 0:
break
我希望找到一种更简单的方法。这行得通,但是对于大量数字而言,似乎很多无用的计算。 有什么想法吗?
答案 0 :(得分:0)
不确定我是否正确理解了您的问题。我的解释是,您有一组介于1到10之间的数字,您需要从该集合中找到最长的子集,这些子集的总和等于或大于10,然后从原始集中删除这些数字,直到找不到任何数字为止。多套不止一个号码。
如果这是正确的解释,那么以下方法应该起作用:
import random
n = int(input("How many numbers in the list? \n"))
throw = [random.randint(1, 10) for _ in range(n)]
print(throw)
throw.sort()
list_of_solved_sets = []
sets_found = True
while sets_found:
_sum = 0
solved_set = []
solved_indexes = []
for index, element in enumerate(throw):
_sum += element
solved_set.append(element)
solved_indexes.append(index)
if _sum >= 10:
break
if len(solved_set) <= 1:
sets_found = False
else:
list_of_solved_sets.append(solved_set)
for index in reversed(solved_indexes):
del throw[index]
print('List of solved sets:')
for i, solved_set in enumerate(list_of_solved_sets):
print(f' {i+1}: {solved_set}')
请注意,您需要按照相反的顺序从列表throw
中删除索引!
答案 1 :(得分:0)
在这种情况下,也许这将是一个解决方案。用伪代码:
代码
import random
n = int(input("How many numbers in the list? \n"))
throw = [random.randint(1, 6) for _ in range(n)]
print(throw)
throw.sort(reverse=True)
print(throw)
list_of_solved_sets = []
# remove all numbers 10 from the list and add a solution
while throw and throw[0] == 10:
list_of_solved_sets.append([throw[0]])
del throw[0]
while throw:
# take the largest number in the set (which is the first number
# as list is sorted) and remove it from the list
solved_set = [throw[0]]
_sum = throw[0]
del throw[0]
while throw:
solved = False
# Check if by adding the smallest number we can get
# a sum greater or equal to 10 if not go to a bigger number
# until we complete the loop. Break loop if a solution is found, add
# to solutions and remove element from the list that solved it
for j in range(len(throw) - 1, -1, -1):
if _sum + throw[j] >= 10:
solved_set.append(throw[j])
del throw[j]
list_of_solved_sets.append(solved_set)
solved = True
break
# if solved break and repeat the outer while loop with next biggest number
# in the list. If not solved then add the next biggest number to the sum and
# check if we can find a solution adding smaller numbers running the for
#loop again
if solved:
break
else:
_sum += throw[0]
solved_set.append(throw[0])
del throw[0]
print('List of solved sets:')
for i, solved_set in enumerate(list_of_solved_sets):
print(f' {i+1}: {solved_set}')
结果示例:
How many numbers in the list?
10
[3, 2, 5, 1, 6, 3, 4, 3, 2, 1]
[6, 5, 4, 3, 3, 3, 2, 2, 1, 1]
List of solved sets:
1: [6, 4]
2: [5, 3, 2]
3: [3, 3, 2, 1, 1]