When "not to" use one liners in Python 😒

One liners are optimal and can be beneficial at many times, but they are not always the best-choice. For example, when you have such a big one liner that it becomes difficult to understand the working of the code, in such cases we have a trade-off between looks of the code and the number of lines it takes. Like I just solved a problem and this can be perfectly related to the topic of when not to use one liners.

This is the Problem Tax Slabs of CodeChef.

Problem Statement:-

In India, every individual is charged with income tax on the total income each year. This tax is applied to specific ranges of income, which are called income tax slabs. The slabs of income tax keep changing from year to year. This fiscal year (2020-21), the tax slabs and their respective tax rates are as follows:

Total income (in rupees)Tax rate
up to Rs. 250,0000%
from Rs. 250,001 to Rs. 500,0005%
from Rs. 500,001 to Rs. 750,00010%
from Rs. 750,001 to Rs. 1,000,00015%
from Rs. 1,000,001 to Rs. 1,250,00020%
from Rs. 1,250,001 to Rs. 1,500,00025%
above Rs. 1,500,00030%

See the sample explanation for details on how the income tax is calculated.

You are given Chef's total income rupees (Rs.). Find his net income. The net income is calculated by subtracting the total tax (also called tax reduction) from the total income. Note that you do not need to worry about any other kind of tax reductions, only the one described above.

Input

  • The first line of the input contains a single integer  denoting the number of test cases. The description of  test cases follows.
  • The first and only line of each test case contains a single integer .

Output

For each test case, print a single line containing one integer — Chef's net income.

Constraints

  • 1103
  • 0107
  •  is a multiple of 100

Sample 1:

Input
Output
2
600000
250000
577500
250000

Explanation:

Example case 1: We know that the total income is Rs. 6 lakh (1 lakh rupees = 105 rupees). The total tax for each slab is calculated as follows:

  • Up to 2.5 lakh, the tax is Rs. 0, since the tax rate is 0 percent.
  • From above Rs. 2.5 lakh to Rs. 5 lakh, the tax rate is 5 percent. Therefore, this tax is 0.05(500,000250,000), which is Rs. 12,500.
  • From above Rs. 5 lakh to Rs. 6 lakh, the tax rate is 10 percent. Therefore, this tax is 0.10(600,000500,000), which is Rs. 10,000.
  • Summing them up, we get that the total tax on Chef's whole income is Rs. 22,500. Since the net income is the total income minus the tax reduction, it is Rs. 600,000 minus Rs. 22,500, which is Rs. 577,500.

Example case 2: For income up to Rs. 2.5 lakh, we have no tax, so the net income is the same as the total income: Rs. 2.5 lakh.

This problem took a hell lot of thinking bcz of the code below...I spent almost 25 mins in this code, and the bug in this code is still unknown, after so much thinking( not to mention I was up for the whole night, and was not looking any less than a Coca addict, with my brain going to unscheduled sleep modes), I said FUDGE IT, and began with a whole new code in PyPy3 and came up with the one mentioned below this one.  

for _ in range(int(input())):
    n=int(input())
    print(int(n)) if(n<=250000) else print(int(n-(0.05*(n-250000)))) if(n>=250001
    and n<=500000) else print(int(n-(0.10*(n-500000)+0.05*(500000-250000))))
    if(n>=500001 and n<=750000) else print(int(n-(0.10*(750000-500000)+
    (0.05*(500000-250000) ))+(n-750000)*0.15)) if(n>=750001 and n<=1000000)
    else print(int(n-( 0.10*(750000-500000)+0.05*(500000-250000) )+(1000000-750000)*
    0.15+(n-1000000)*0.20) ) if(n>=1000001 and n<=1250000) else print(int(n-(0.10*
    (750000-500000)+0.05*(500000-250000)+(1000000-750000)*0.15+(1250000-1000000)*0.20+
    (n-1250000)*0.25))) if(n>=1250001 and n<=1500000) else print(int(n-(0.10*(750000-
    500000)+0.05*(500000-250000)+(1000000-71250000)*0.25+0.30*(n-1500000))))
   

Rumor has it, that the main reason behind the failure of Falcon 9...was this code!

This below code is the second implementation of the process and this passed all test-cases in 1 go(I slept for around 15 mins after clicking on submit for the very first time, wasn't in a condition to see another Wrong Answer in the Output xd). Although when I opened my eyes the "Correct Answer" was worth the efforts 😭

for _ in range(int(input())):
    n=int(input())
    tax=0
    if(n<=250000):
        # print(n)
        pass
    elif(n>=250001 and n<=500000):
        tax=(n-250000)*0.05
        # print(n-tax)
    elif(n>=500001 and n<=750000):
        tax=(500000-250000)*0.05+(n-500000)*0.10
        # print(n-tax)
    elif(n>=750001 and n<=1000000):
        tax=(500000-250000)*0.05+(750000-500000)*0.10+(n-750000)*0.15
        # print(n-tax)
    elif(n>=1000001 and n<=1250000):
        tax=(500000-250000)*0.05+(750000-500000)*0.10+(1000000-750000)*0.15+
(n-1000000)*0.20
        # print(n-tax)
    elif(n>=1250001 and n<=1500000):
        tax=(500000-250000)*0.05+(750000-500000)*0.10+(1000000-750000)*0.15+
(1250000-1000000)*0.20+(n-1250000)*0.25
        # print(n-tax)
    else:
        tax=(500000-250000)*0.05+(750000-500000)*0.10+(1000000-750000)*0.15+
(1250000-1000000)*0.20+(1500000-1250000)*0.25+(n-1500000)*0.30
    print(int(n-tax))

Comments

Popular Posts