-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccountBalanceAfterRoundedPurchase.py
29 lines (22 loc) · 1.17 KB
/
AccountBalanceAfterRoundedPurchase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Initially, you have a bank account balance of 100 dollars.
# You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars.
# At the store where you will make the purchase, the purchase amount is rounded to the nearest multiple of 10. In other words, you pay a non-negative amount, roundedAmount, such that roundedAmount is a multiple of 10 and abs(roundedAmount - purchaseAmount) is minimized.
# If there is more than one nearest multiple of 10, the largest multiple is chosen.
# Return an integer denoting your account balance after making a purchase worth purchaseAmount dollars from the store.
# Note: 0 is considered to be a multiple of 10 in this problem.
import math
def accountBalanceAfterPurchase(purchaseAmount):
ones = (purchaseAmount % 10) / 10
tens = purchaseAmount // 10
if ones < 0.5:
ones = math.floor(ones)
else:
ones = math.ceil(ones)
return 100 - ((tens + ones) * 10)
# Test cases
purchaseAmount = 9
print(accountBalanceAfterPurchase(purchaseAmount))
purchaseAmount = 15
print(accountBalanceAfterPurchase(purchaseAmount))
purchaseAmount = 5
print(accountBalanceAfterPurchase(purchaseAmount))