Accumulator battery in Python

PythonServer Side ProgrammingProgramming

Suppose we have a mobile phone which is in "eco mode". This mode activates once your battery level reaches 20 percent. In this eco mode the battery drains two times slower than in normal mode. Now when we leave our home, we have 100% of battery. Then t minutes after we have p percent of battery left. We have to find how many minutes we have until our phone will turn off.

So, if the input is like t = 75 and p = 25, then the output will be 45

To solve this, we will follow these steps −

  • if p < 20, then

    • return 2*p*t/(120-2*p)

  • otherwise return (p+20) *t/(100-p)

Let us see the following implementation to get better understanding −


 Live Demo

class Solution:
   def solve(self, t, p):
      if p < 20:
         return 2*p*t/(120-2*p)
      return (p+20)*t/(100-p)
ob = Solution()
print(ob.solve(75, 25))




Updated on 02-Sep-2020 12:40:10