Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Level-1/hack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_4(self):
payment = c.Item(type='payment', description='invoice_4', amount=1e19, quantity=1)
reimbursement = c.Item(type='payment', description='reimbursement_4', amount=-1e19, quantity=1)
order_4 = c.Order(id='4', items=[payment, tv, reimbursement])
self.assertEqual(c.validorder(order_4), 'Order ID: 4 - Payment imbalance: $-1000.00')
self.assertNotEqual(c.validorder(order_4), 'Order ID: 4 - Full payment received!')

if __name__ == '__main__':
unittest.main()
unittest.main()
24 changes: 18 additions & 6 deletions Level-1/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,34 @@
MAX_QUANTITY = 100 # maximum quantity of an item in the shop
MAX_TOTAL = 1e6 # maximum total amount accepted for an order

# sets a reasonable min & max value for the invoice amounts
def validamount(amount):
return amount > -1*MAX_ITEM_AMOUNT and amount < MAX_ITEM_AMOUNT

# sets a reasonable min & max value for the item quantities
def validquantity(quantity):
return quantity > -1 * MAX_QUANTITY and quantity < MAX_QUANTITY

def validorder(order):
net = 0

for item in order.items:
if item.type == 'payment':
# sets a reasonable min & max value for the invoice amounts
if item.amount > -1*MAX_ITEM_AMOUNT and item.amount < MAX_ITEM_AMOUNT:
net += item.amount
if not validamount(item.amount):
return("Invalid amount")
net += item.amount
elif item.type == 'product':
if item.quantity > 0 and item.quantity <= MAX_QUANTITY and item.amount > 0 and item.amount <= MAX_ITEM_AMOUNT:
net -= item.amount * item.quantity
if not validquantity(item.quantity):
return("Invalid quantity")
if not validamount(item.amount):
return("Invalid amount")

net -= item.amount * item.quantity
if net > MAX_TOTAL or net < -1*MAX_TOTAL:
return("Total amount exceeded")
else:
return("Invalid item type: %s" % item.type)

if net != 0:
return("Order ID: %s - Payment imbalance: $%0.2f" % (order.id, net))
else:
Expand Down