Найдите все возможные комбинации 25 и 50 пайсовых монет, которые добавляются к изменению 5 рупий - PullRequest
0 голосов
/ 30 января 2019

Мне нужно найти питонский способ найти все комбинации монет по 25 и 50 пайс, которые добавляются к изменению 5 рупий.

Я новичок в python и задаюсь вопросом, могут ли itertools.combination или itertools.permutaion помочь мне найти это значение.

Expected output
Combination #1: 25 Paisa x 0 + 50 Paisa x 10 = 5 Rs.
Combination #1: 25 Paisa x 2 + 50 Paisa x 9  = 5 Rs.
Combination #1: 25 Paisa x 4 + 50 Paisa x 8  = 5 Rs.
Combination #1: 25 Paisa x 6 + 50 Paisa x 7  = 5 Rs.
Combination #1: 25 Paisa x 8 + 50 Paisa x 6  = 5 Rs.
Combination #1: 25 Paisa x 10 + 50 Paisa x 5 = 5 Rs.
Combination #1: 25 Paisa x 12 + 50 Paisa x 4 = 5 Rs.
Combination #1: 25 Paisa x 14 + 50 Paisa x 3 = 5 Rs.
Combination #1: 25 Paisa x 16 + 50 Paisa x 2 = 5 Rs.
Combination #1: 25 Paisa x 18 + 50 Paisa x 1 = 5 Rs.
Combination #1: 25 Paisa x 20 + 50 Paisa x 0 = 5 Rs.

Ответы [ 2 ]

0 голосов
/ 06 февраля 2019

Вот оптимальное решение для поиска комбинаций любого набора заданных монет, которые складываются в любую общую сумму.

def change(amount, coins_available, coins_so_far):
    if sum(coins_so_far) == amount:
            yield coins_so_far
    elif sum(coins_so_far) > amount:
            pass
    elif coins_available == []:
            pass
    else:
            for c in change(amount, coins_available[:], coins_so_far+[coins_available[0]]):
                    yield c
            for c in change(amount, coins_available[1:], coins_so_far):
                    yield c

amount = 500
coins = [5, 25]

solutions = [s for s in change(amount, coins, [])]
i = 1
for s in solutions:
    print str(i) + " : " + str(s)
    i += 1

print 'optimal solution:', min(solutions, key=len)
0 голосов
/ 30 января 2019

Вот простое решение для этого

coins = []

num_of_coins_25_paisa = 500/25

for i in range(num_of_coins_25_paisa+1):
    sum_of_25_paisa = 25*i
    remaining_amount = 500 - sum_of_25_paisa

    if remaining_amount % 50 == 0:
        coins.append((i,remaining_amount/50))

i = 1
for group in coins:
    print "Combination #1 : 25 Paisa x {0} + 50 Paisa x {1}  = 5 Rs.".format(group[0],group[1])

Вывод:

Combination #1: 25 Paisa x 0 + 50 Paisa x 10 = 5 Rs.
Combination #1: 25 Paisa x 2 + 50 Paisa x 9 = 5 Rs.
Combination #1: 25 Paisa x 4 + 50 Paisa x 8 = 5 Rs.
Combination #1: 25 Paisa x 6 + 50 Paisa x 7 = 5 Rs.
Combination #1: 25 Paisa x 8 + 50 Paisa x 6 = 5 Rs.
Combination #1: 25 Paisa x 10 + 50 Paisa x 5 = 5 Rs.
Combination #1: 25 Paisa x 12 + 50 Paisa x 4 = 5 Rs.
Combination #1: 25 Paisa x 14 + 50 Paisa x 3 = 5 Rs.
Combination #1: 25 Paisa x 16 + 50 Paisa x 2 = 5 Rs.
Combination #1: 25 Paisa x 18 + 50 Paisa x 1 = 5 Rs.
Combination #1: 25 Paisa x 20 + 50 Paisa x 0 = 5 Rs.
...