Skip to content

Instantly share code, notes, and snippets.

@ianfoo
Created April 20, 2019 02:53
Show Gist options
  • Select an option

  • Save ianfoo/89f36b3b076ea7d6d605eec4baad5e86 to your computer and use it in GitHub Desktop.

Select an option

Save ianfoo/89f36b3b076ea7d6d605eec4baad5e86 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
def can_combine_for_a0(stock):
"""Determine if different sized sheets of paper can combine to an A0 sheet.
Given a tuple of A-format paper sizes (A0, A1, A2...), with each entry
representing the number of sheets for a given size (i.e., element 0 is
number of A0, element 1 is number of A1, etc), determine whether the
collection of sheets can be pieced together to produce at least one
full sheet of A0 paper.
See https://www.papersizes.org/a-paper-sizes.htm for more information
about A paper sizes.
"""
pct = 0.0
factor = 1.0
for x in stock:
pct += x * factor
if pct >= 1.0:
return True
factor /= 2
return False
def print_collection(collection):
desc = ""
for i, n in enumerate(collection):
if n > 0:
desc += f"{n} A{i}, "
return desc[:-2]
if __name__ == "__main__":
test_cases = [
(0, 3),
(0, 1, 2),
(0, 0, 0, 0, 15),
(2, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 5, 0, 3, 0, 0, 1, 0, 0, 0, 5),
(0, 1, 1, 1, 2),
]
for x in test_cases:
result = can_combine_for_a0(x)
num_sizes = sum([1 for n in x if n > 0])
print(f"{num_sizes:2d} size{'s' if num_sizes > 1 else ''}: {print_collection(x)}: {result}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment