Skip to content

Instantly share code, notes, and snippets.

@hughlilly
Last active October 12, 2022 05:38
Show Gist options
  • Save hughlilly/084820ea5ae67fef69470446714cd60d to your computer and use it in GitHub Desktop.
Save hughlilly/084820ea5ae67fef69470446714cd60d to your computer and use it in GitHub Desktop.
Testing
'''
Task 1: Box calculator
This table shows available box sizes and the number of items each can hold:
+--------+-------+
| Size | Items |
+--------+-------+
| Big | 5 |
| Medium | 3 |
| Small | 1 |
+--------+-------+
'''
# This program fulfils the following four test-case assertions:
#
# Test-case A
# -----------
# 22 items should fill:
# 📦 4 big boxes (4 * 5 = 20)
# 📦 2 small boxes (2 * 1 = 2)
# 20 + 2 = 22
# 4 + 2 = 6 boxes total
# -----------
#
# Test-case B
# -----------
# 13 items should fill:
# 📦 2 big boxes (2 * 5 = 10)
# 📦 1 medium box (3 * 1 = 3)
# 10 + 3 = 13
# 2 + 1 = 3 boxes total
# -----------
#
# Test-case C
# -----------
# 9 items should fill:
# 📦 1 big box (5 * 1 = 5)
# 📦 1 medium box (3 * 1 = 3)
# 📦 1 small box (1 * 1 = 1)
# 5 + 3 + 1 = 9
# 1 + 1 + 1 = 3 boxes total
# -----------
#
# Test-case D
# -----------
# 84 items should fill:
# 📦 16 big box (5 * 16 = 80)
# 📦 1 medium box (3 * 1 = 3)
# 📦 1 small box (1 * 1 = 1)
# 80 + 3 + 1 = 84
# 16 + 1 + 1 = 18 boxes total
# -----------
def calculate_small_boxes(number: int) -> int:
"""Calculates the number of small boxes (these hold just one item each).
Parameters:
`number` : int, required
The number of items to put into small boxes
Returns:
int
"""
# Small box count will be whatever is left over after dividing
# the (remaining) number of items by 3
return number % 3
def calculate_medium_boxes(number: int) -> int:
"""Calculates the number of medium boxes (these hold 3 items each).
Parameters:
`number` : int, required
The number of items to put into medium boxes
Returns:
int
"""
return number // 3
def calculate_big_boxes(number: int) -> int:
"""Calculates the number of big boxes (these hold 5 items each).
Parameters:
`number` : int, required
The number of items to put into big boxes
Returns:
int
"""
return number // 5
def calculate_boxes(num_items: int):
"""Calculates the number of boxes of various sizes filled by a given\
number of items.
Parameters:
`num_items` : int, required
The number of items to put into boxes
"""
# Initialise box counts to zero
big_box_count = 0
medium_box_count = 0
small_box_count = 0
num_items_remaining = 0
# Count number of big boxes filled
big_box_count = calculate_big_boxes(num_items)
# Work out how many unboxed items remain
num_items_remaining = num_items % 5
# Count number of medium boxes filled
medium_box_count = calculate_medium_boxes(num_items_remaining)
# Count number of small boxes filled
small_box_count = calculate_small_boxes(num_items_remaining)
# For each size of box, print a summary message
# Use plural ("boxes") if more than one, otherwise singular ("box")
print("\nThat number of items filled:")
if big_box_count > 1:
print("📦 " + str(big_box_count) + " big boxes")
elif big_box_count == 1:
print("📦 1 big box")
if medium_box_count > 1:
print("📦 " + str(medium_box_count) + " medium boxes")
elif medium_box_count == 1:
print("📦 1 medium box")
# Add up the number of items to put into boxes used
box_count = big_box_count + medium_box_count
# If any small boxes were used, print a message about them and add
# their number to the overall count
if small_box_count:
if small_box_count > 1:
print("📦 " + str(small_box_count) + " small boxes")
elif small_box_count == 1:
print("📦 1 small box")
box_count += small_box_count
# If more than one, set string to "boxes", plural, otherwise "box",
# singular, using ternary operator
box_term = " boxes" if box_count > 1 else " box"
print("\nYou used " + str(box_count) + box_term + " in total.\n")
# End of program
# The name/main guard ensures this code will be run only when this file
# is directly executed/called
if __name__ == "__main__":
# Welcome message and explanation
print("Welcome to the Box Calculator.\nBig boxes hold 5 items; medium boxes hold 3 items, and small boxes hold just one item each.")
print("Please enter a positive integer.")
running = True
while running:
# Get number of items
num_items = int(input("How many items do you have? "))
if num_items > 0:
running = False
calculate_boxes(num_items)
else:
print("Sorry, we needed a positive integer.")
running = True
import unittest
from BoxCalculator import calculate_small_boxes, calculate_medium_boxes, calculate_big_boxes
class TestBoxCalculator(unittest.TestCase):
def test_small(self):
"""Test that `count_small_boxes` returns the same as the number passed.
(Small boxes can hold only one item each.) """
test_value = 1
expected = 1
result = calculate_small_boxes(test_value)
self.assertEqual(result, expected, "Each small box holds 1 item")
def test_small_more_than_3(self):
"""Test that `count_small_boxes` returns None if given input of 3 or more"""
test_value = 3
result = calculate_small_boxes(test_value)
self.assertIsNone(result,
"Use medium boxes for 3 or more items!")
def test_medium(self):
"""Test that `count_medium_boxes` returns one box for every 3 items."""
test_value = 12
expected = 4
result = calculate_medium_boxes(test_value)
self.assertEqual(result, expected, "Each medium box holds 3 items")
def test_big(self):
"""Test that `count_big_boxes` returns one box for every 5 items."""
test_value = 15
expected = 3
result = calculate_big_boxes(test_value)
self.assertEqual(result, expected, "Each big box holds 5 items")
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment