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:
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
- is a multiple of
Sample 1:
2 600000 250000
577500 250000
Explanation:
Example case 1: We know that the total income is Rs. lakh ( lakh rupees = rupees). The total tax for each slab is calculated as follows:
- Up to lakh, the tax is Rs. , since the tax rate is percent.
- From above Rs. lakh to Rs. lakh, the tax rate is percent. Therefore, this tax is , which is Rs. .
- From above Rs. lakh to Rs. lakh, the tax rate is percent. Therefore, this tax is , which is Rs. .
- Summing them up, we get that the total tax on Chef's whole income is Rs. . Since the net income is the total income minus the tax reduction, it is Rs. minus Rs. , which is Rs. .
Example case 2: For income up to Rs. lakh, we have no tax, so the net income is the same as the total income: Rs. 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.
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 ðŸ˜
Comments
Post a Comment