【问题标题】:How can I count how many numbers can be fitted within particular number?如何计算特定数字中可以容纳多少个数字?
【发布时间】:2020-04-11 19:30:24
【问题描述】:

示例 1:

save([4,4,4,3,3], 12) -> 3
# 4+4+4 <= 12, but 4+4+4+3 > 12

示例 2:

save([4,4,4,3,3], 11) -> 2
# 4+4 <= 11, but 4+4+4 > 11

首先我还是菜鸟,但这是我的“代码”哈哈。

def save(sizes, hd): 
    sum = 0
    for i in sizes:
        sum = sum + i
        if sum <= hd:
            a = (str(sum))
            print(a)   

save([4,4,4,3,3], 12)

这段代码的输出是:

4 
8 
12

如果我能数出这些数字的长度是正确的,但我尝试了很多方法仍然找不到(

感谢您的帮助!

【问题讨论】:

标签: python python-3.x


【解决方案1】:

你需要enumerate()函数:

def save(sizes, hd): 
    summm = 0
    for index,value in enumerate(sizes,1):  # start counting at 1
        summm += value
        if summm <= hd: 
            print(summm, index)    # or simply print index alone

save([4,4,4,3,3], 12)  

输出:

 12 3   # first number is the sum, second amount of numbers you added (aka 1-based index) 

【讨论】:

  • 在 Python 中你真的不需要 a = (str(summm))
【解决方案2】:

您可以使用 for i in range(len(sizes)) 。我认为这是最快的解决方案,但我相信这并不重要。

def save(sizes, hd):
    sum = 0
    for i in range(len(sizes)):     # i values are 0,1,2,...,len(sizes)-1
        sum += sizes[i]             # add i-th sizes element to sum
        if sum <= hd:               # check if we can print answer
            print(i + 1)            # because i starts from 0
        else:                       # if we can't print, we should exit cycle
            break                   # we can use return instead

顺便说一句,你不需要做这样的事情,因为'print()'函数将所有参数转换为str()

a = str(sum)
print(a)

希望你学到了一些新东西!

【讨论】:

    【解决方案3】:

    试试这个:

    def save(sizes, hd):
        sum = 0
        new_list = []
        for i in sizes:
            sum += i
            new_list.append(i)
            if sum >= hd:
                print(new_list)
                return len(new_list)
    
    
    print(save([4,4,4,3,3], 12))
    #print(new_list) => [4, 4, 4]
    #return len(new_list) => 3
    

    【讨论】:

      【解决方案4】:

      如果您使用已排序数字的累积总和构建一个列表,则目标值的位置将对应于适合您的目标的最大数字计数:

      from bisect import bisect_right
      from itertools import accumulate
      
      numbers = [4,4,4,3,3]
      target  = 12
      maxCount = bisect_right(list(accumulate(sorted(numbers))),target)
      
      print(maxCount) # 3
      

      bisect 模块提供了在排序列表 (bisect_right) 中对索引的高效搜索。 itertools 模块提供了一个函数(accumulate)来获取列表中数字的累积和

      【讨论】:

        【解决方案5】:

        为了简单起见,我稍微更改了您的代码:

        def save(sizes, hd): 
            size_list = [] #instead of an int, let's use a list object to hold values
            for size in sizes:
                size_list.append(i) # add the size to the list
                if sum(size_list) <= hd: # check if sum of all values in list is <= hd
                    return len(size_list) # return the number of sizes inside of list
        

        希望这会有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-11
          • 2021-09-10
          • 1970-01-01
          • 1970-01-01
          • 2011-04-15
          相关资源
          最近更新 更多