Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- class Checkout():
- def __init__(self, pricing_rules):
- self.pricing_rules = pricing_rules
- self.basket = {}
- def scan(self, item):
- self.basket[item] = self.basket[item] + 1 if item in self.basket else 1
- def total(self):
- basket_total = 0
- for item_type, quantity in self.basket.items():
- basket_total = basket_total + self.pricing_rules[item_type](quantity)
- return basket_total
- def bogof(quantity):
- return math.ceil(quantity / 2.0)
- def bulkbuy(threshold_quantity, original_price, new_price, actual_quantity):
- return original_price if actual_quantity < threshold_quantity else new_price
- #### test.py###
- import math
- from do_something import Checkout, bogof, bulkbuy
- def cf1_rule(quantity):
- return 1123 * quantity
- def fr1_rule(quantity):
- return 311 * bogof(quantity)
- def sr1_rule(quantity):
- return quantity * bulkbuy(3, 500, 450, quantity)
- def test_fr1():
- checkout = Checkout({'FR1': fr1_rule})
- checkout.scan('FR1')
- assert checkout.total() == 311
- def test_dataset_1():
- """
- Second test from provided data
- """
- checkout = Checkout({
- 'FR1': fr1_rule,
- 'SR1': sr1_rule,
- 'CF1': cf1_rule
- })
- checkout.scan('FR1')
- checkout.scan('SR1')
- checkout.scan('FR1')
- checkout.scan('FR1')
- checkout.scan('CF1')
- assert checkout.total() == 2245
- def test_dataset_2():
- """
- Second test from provided data
- """
- checkout = Checkout({'FR1': fr1_rule})
- checkout.scan('FR1')
- checkout.scan('FR1')
- assert checkout.total() == 311
- def test_dataset_3():
- """
- third test from provided data
- """
- checkout = Checkout({
- 'FR1': fr1_rule,
- 'SR1': sr1_rule
- })
- checkout.scan('SR1')
- checkout.scan('SR1')
- checkout.scan('FR1')
- checkout.scan('SR1')
- assert checkout.total() == 1661
- def test_scan_single():
- checkout = Checkout({})
- checkout.scan('FR1')
- assert checkout.basket == {'FR1': 1}
- def test_scan_same_item():
- checkout = Checkout({})
- checkout.scan('FR1')
- checkout.scan('FR1')
- assert checkout.basket == {'FR1': 2}
- def test_scan_same_item():
- checkout = Checkout({})
- checkout.scan('FR1')
- checkout.scan('CF1')
- assert checkout.basket == {'FR1': 1, 'CF1': 1}
- def test_bogof():
- assert bogof(2) == 1
- def test_bogof_2():
- assert bogof(3) == 2
- def test_bogof_1():
- assert bogof(1) == 1
- def test_not_bulkbuy():
- assert bulkbuy(3, 500, 450, 2) == 500
- def test_bulkbuy():
- assert bulkbuy(3, 500, 450, 3) == 450
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement