Skip to content

Instantly share code, notes, and snippets.

@jpic
Created March 14, 2024 14:17
Show Gist options
  • Save jpic/0e9453d3b82ec80b9aa56d40fe3222f7 to your computer and use it in GitHub Desktop.
Save jpic/0e9453d3b82ec80b9aa56d40fe3222f7 to your computer and use it in GitHub Desktop.
import copy
import math
def box(remaining, capacity):
if len(remaining) >= capacity:
weights = [b.item_weight for b in remaining[:capacity]]
return sum(weights)
def package(bottles):
remaining = bottles
packages = []
num_12 = math.floor(len(remaining) / 12)
for _ in range(0, num_12):
weights = [b.item_weight for b in remaining[:12]]
packages.append(sum(weights))
remaining = remaining[12:]
for capacity in (9, 6, 3):
box_weight = box(remaining, capacity)
if box_weight:
packages.append(box_weight)
remaining = remaining[capacity:]
for bottle in remaining:
packages.append(bottle.item_weight)
return packages
import pytest
import random
from shipping.packaging import package
class BottleMock:
def __init__(self, weight=1):
self.item_weight = weight
def scenario(*args):
return [1 for _ in range(0, sum(args))], list(args)
def test_scenario():
assert scenario(3, 1) == ([1, 1, 1, 1], [3, 1])
@pytest.mark.parametrize('weigths,packages', (
# test that we sum the weight properly
([1, 3, 2, 2], [6, 2]),
# now test that we package properly :)
scenario(3, 1),
scenario(6, 1, 1),
scenario(9, 1),
scenario(12, 12, 6, 1),
))
def test_package(weigths, packages):
bottles = [BottleMock(w) for w in weigths]
assert package(bottles) == packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment