利用嵌套循环Python

时间:2013-11-05 20:22:00

标签: python nested-loops

我是Python noob试图学习我在网上找到的编码练习。我需要购买一些鸡块,而且鸡块有三种不同的包装尺寸(6,9和20)。

我需要设置一个优化我的购买功能(首先最大化20包,然后是9包,然后是6包)。首要任务是确保没有剩余,然后对包装尺寸进行优化。因此对于139,答案将是5个20包,3个9包和2个6包。目前我已经使用一系列嵌套的while循环完成了该功能,但我知道必须有一个更优雅的解决方案。

感谢您的帮助!

def nuggets(nuggs_needed):
    packs = [6,9,20]
    twenty_counter = nuggs_needed//20 # Start 20 pack counter with largerst divisable number#
    ttl_nuggs=0
    while twenty_counter>=0:
        ttl_nuggs = 0
        twenty_nuggs_ttl = twenty_counter*20 #total nuggest for 20 pack#
        new_nuggs_needed = nuggs_needed-twenty_nuggs_ttl #remaining nuggs after 20 pack#

        nine_counter = new_nuggs_needed//9
        while nine_counter>=0:
            nine_nuggs_ttl = nine_counter*9
            ttl_nuggs = twenty_nuggs_ttl+nine_nuggs_ttl                
            new_nuggs_needed = nuggs_needed-ttl_nuggs

            six_counter = new_nuggs_needed//6
            while six_counter>=0:
                six_nuggs_ttl = six_counter*6
                ttl_nuggs = twenty_nuggs_ttl+six_nuggs_ttl+nine_nuggs_ttl
                new_nuggs_needed = nuggs_needed-ttl_nuggs
                print '{0} 20packs, {1} 9packs, {2} 6packs = {3}total nuggets'.format(twenty_counter,nine_counter,six_counter,ttl_nuggs)
                if ttl_nuggs == nuggs_needed:
                    print 'Thats it: {0} 20packs, {1} 9packs, {2} 6packs = {3}total nuggets'.format(twenty_counter,nine_counter,six_counter,ttl_nuggs)
                    print 'Hooray!'
                    break
                six_counter-=1

            if ttl_nuggs == nuggs_needed:
                break
            nine_counter-=1

        if ttl_nuggs == nuggs_needed:
                break  
        twenty_counter-=1



nuggets(139)

4 个答案:

答案 0 :(得分:0)

您可以从单个循环中所需的总金额中减去:

packs = [20,9,6]
s = nuggs_needed
res = []
for num in sorted(packs, reverse=True):
    print s
    r = s//num
    res.append(r)
    s = s - r*num

res中的数字将是您需要的每种包装的数量。循环完成后,s将是您无法购买的金块数。希望它有所帮助!

答案 1 :(得分:0)

你可以用这个:

def nuggs(num_needed, sizes=(6, 9, 20)):
    result = {}
    remaining = num_needed

    for size in sorted(sizes, reverse=True):
        result[size] = remaining // size
        remaining = remaining % size

    if remaining > 0:
        result[min(sizes)] += 1  # add one of the smallest pack-size if there are any left :)

    print '{} Nuggets:'.format(num_needed)
    print ', '.join(['{} x {}-packs'.format(result[size], size)
                    for size in sorted(sizes, reverse=True)])

答案 2 :(得分:0)

值得关注divmod这类问题。像这样:

def nuggets(needed):
    twenties, remaining = divmod(needed, 20)
    nines, remaining = divmod(remaining, 9)
    sixes, remaining = divmod(remaining, 6)
    print "{0} 20packs, {1} 9packs, {2} 6packs ({3} unaccounted for) = {4} total nuggets".format(twenties, nines, sixes, remaining, needed)

请注意,您没有考虑剩余的金块(无法获得的数量)。

答案 3 :(得分:0)

如果你匆忙而且不想再想其他事情了,那就行了:

arrangement=[int(nugg/20),int((nugg%20)/9),int(int((nugg%20)/9)/6), \
             int(int((nugg%20)%9)%6)]
[6,2,0,1]

6- of 20 containing packs
2- of 9 containing packs
0- of 6 containing packs
1- of remaining items

简单if / else ITERATIVE SOLUTION:

def optimize_nugg(nugg_needed):
    remaining=nugg_needed
    list_20_9_6=[]
    while(remaining>0):

        if(nugg_needed>=20):
            list_20_9_6.append(int(nugg_needed/20))
            remaining=nugg_needed%20

        if(nugg_needed>=9 and nugg_needed<20):
            list_20_9_6.append(int(nugg_needed/9))
            remaining=nugg_needed%9

        if(nugg_needed>=6 and nugg_needed<9):
            list_20_9_6.append(int(nugg_needed/6))
            remaining=nugg_needed%6

        if(nugg_needed<6):
            list_20_9_6.append(remaining)
            remaining=0

        nugg_needed=remaining



    print(list_20_9_6)

回归解决方案:

def optimize_nugg(nugg_needed):
    print("the nugg needed here is ",nugg_needed)
    if(nugg_needed==0):
        return
    else:
        if(nugg_needed>=20):
            return int(nugg_needed/20), optimize_nugg(nugg_needed%20)
        if(nugg_needed>=9):
            return int(nugg_needed/9), optimize_nugg(nugg_needed%9)
        if(nugg_needed>=6):
            return int(nugg_needed/6), optimize_nugg(nugg_needed%6)
        else:
            return nugg_needed

print(optimize_nugg(139))