aboutsummaryrefslogtreecommitdiff
path: root/challenge-323/eric-cheung/python/ch-2.py
blob: 50e9c6117f755e1113854c01481dcca8b0f57a53 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
## Ref.:
## https://leetcode.com/problems/calculate-amount-paid-in-taxes/description/

## Example 1
nIncome = 10
arrTax = [[3, 50], [7, 10], [12, 25]]

## Example 2
## nIncome = 2
## arrTax = [[1, 0], [4, 25], [5, 50]]

## Example 3
## nIncome = 0
## arrTax = [[2, 50]]

nBalance = nIncome
nPrevTaxCeil = 0
dTaxAmout = 0

for arrTaxPair in arrTax:
    if nBalance == 0:
        break

    nTempTax = arrTaxPair[0]
    nCeil = nTempTax - nPrevTaxCeil
    dTaxPer = arrTaxPair[1] / 100

    ## if nBalance >= nCeil:
        ## dTaxAmout = dTaxAmout + nCeil * dTaxPer
        ## nBalance = nBalance - nCeil
    ## else:
        ## dTaxAmout = dTaxAmout + nBalance * dTaxPer
        ## nBalance = 0

    nTempAmount = min(nCeil, nBalance)
    dTaxAmout = dTaxAmout + nTempAmount * dTaxPer
    nBalance = nBalance - nTempAmount

    nPrevTaxCeil = nTempTax

    ## print ("")
    ## print ("===")
    ## print (nBalance, dTaxAmout)
    ## print ("===")

print (dTaxAmout)