唯一和大于数字

时间:2019-11-05 20:56:28

标签: python python-3.x

因此,我正在尝试编写一个代码,该代码采用一个介于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

我希望找到一种更简单的方法。这行得通,但是对于大量数字而言,似乎很多无用的计算。 有什么想法吗?

2 个答案:

答案 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)

在这种情况下,也许这将是一个解决方案。用伪代码:

  1. 读取数字列表并从高到低排序
  2. 从列表中删除所有10,并将其添加为解决方案
  3. 循环直到列表为空
  4. 获取列表中最大的数字(这是列表排序时的第一个数字)并将其从列表中删除
  5. 检查是否可以通过添加最小数字来求和,如果不进行更大的数字运算,就可以得出等于或大于10的总和,直到完成循环。如果找到解决方案,请中断循环,将其添加到解决方案中,然后从解决问题的列表中删除该元素。
  6. 如果解决了中断问题,并使用列表中的下一个最大数字重复外部while循环。如果未解决,则将总和的下一个大数添加到列表中,并将其从列表中删除,然后检查是否可以找到解决方案,然后再次运行for循环再添加较小的数字

代码

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]