Home > Software engineering >  Can you set up a condition in python to check for whole numbers?
Can you set up a condition in python to check for whole numbers?

Time:05-18

I am trying to create a function in python which can calculate the most efficient ways to pack items. a large pack will have the capacity to hold 25 items and small packet will have the capacity to hold 5 items.

def packages_needed (items, large_pack, small_pack)

I am really stuck on how to define / set up the reasoning

what I am thinking of is setting up a condition in which x/a is a whole number then z would just be that. and if the remainder is not 0 then z would whole number plus remainder.

im not sure if my reasoning makes sense, but I would really appreciate any suggestions/help.

what I have so far is:


z = 0
x = large_pack
y= small_pack

for a in items:```



CodePudding user response:

You can use the standard division floor operator(//) to determine how many times the number of items fits into the large and small size packs.

def division_ceiling(a: int, b: int) -> int:
    """
    Accepts number of items. Returns how many small packs are needed.
    """
    return -1 * (-a // b)


def packages_needed(items: int) -> (int, int):
    """
    Accepts number of items. Returns tuple (large pack, small pack)
    """
    large_size = 25
    small_size = 5

    large_packs = items // large_size
    small_packs = division_ceiling(items - large_packs * large_size, small_size)
    return (large_packs, small_packs)
    """
    if you want 1-25 items to be 1 large pack then do this:
    if items <= 25:
        return (1, 0)
    else:
        large_packs = items // large_size
        small_packs = division_ceiling(items - large_packs * large_size, small_size)
        return (large_packs, small_packs)
    """


print(packages_needed(int(input("Number of items?"))))

Input: 31

Output: (1, 2)

Input: 6

Output: (0, 2)
  • Related