jeudi 20 août 2020

finding minimum amount of different valued bags

Input is amount of stuff by kg and I have to get minimum total number of bags of 3kg and 5kg that can hold the stuff. And if it can not be divided in bags I have to return -1. Can anyone tell me which part of my code is incorrect.

kg = int(input())
# number of 5kg bags by index
kg_5 = []
if kg == 1 or kg == 2:
    print(-1)
else:
    for i in range(kg//3):
        if (kg-5*i) % 3 == 0 and kg-5*i >= 0:
            kg_5.append(i)
        else:
            kg_5.append(0)
    if (kg - max(kg_5)*5) % 3 == 0:
        num = max(kg_5) + (kg - max(kg_5)*5) // 3
        print(num)
    elif set(kg_5) == {0}:
        print(-1)

Aucun commentaire:

Enregistrer un commentaire