Accumulator battery in Python

Consider a mobile phone in eco mode that activates when battery level reaches 20%. In eco mode, the battery drains two times slower than normal mode. Given the current time t and battery percentage p, we need to calculate how many minutes remain until the phone turns off.

Problem Statement

In normal mode, if the battery drains 1% per minute, then in eco mode it drains 1% per 2 minutes (half the rate). Starting with 100% battery, after t minutes we have p percent remaining. We must find the time until complete drain.

Example Scenario

If t = 75 minutes and p = 25%, the battery drained from 100% to 25% in 75 minutes ?

Drain rate = (100 - 25) / 75 = 75% / 75 min = 1% per minute
Time from 25% to 20% = 5% / 1% per min = 5 minutes
Time from 20% to 0% = 20% / 0.5% per min = 40 minutes
Total remaining time = 5 + 40 = 45 minutes

Algorithm

The solution depends on whether the current battery level is above or below 20% ?

  • If p < 20 (already in eco mode): Calculate remaining time using eco drain rate
  • If p ? 20 (normal mode): Calculate time to reach 20% + time to drain from 20% to 0%

Implementation

Method 1: Direct Formula Approach

def battery_time_remaining(t, p):
    if p < 20:
        # Already in eco mode
        return 2 * p * t / (120 - 2 * p)
    else:
        # Normal mode, need to account for eco mode transition
        return (p + 20) * t / (100 - p)

# Test with example: 75 minutes, 25% remaining
print(f"Time remaining: {battery_time_remaining(75, 25)} minutes")

# Test with eco mode: 80 minutes, 18% remaining  
print(f"Time remaining: {battery_time_remaining(80, 18)} minutes")
Time remaining: 45.0 minutes
Time remaining: 34.285714285714285 minutes

Method 2: Step-by-Step Calculation

def detailed_battery_calculation(t, p):
    if p >= 20:
        # Calculate normal drain rate
        normal_rate = (100 - p) / t  # percent per minute
        
        # Time to reach 20% at normal rate
        time_to_20 = (p - 20) / normal_rate
        
        # Time from 20% to 0% at eco rate (half speed)
        eco_rate = normal_rate / 2
        time_to_0 = 20 / eco_rate
        
        return time_to_20 + time_to_0
    else:
        # Already in eco mode
        # Calculate eco drain rate from given data
        total_drained = 100 - p
        eco_portion = 20 - p if p < 20 else 0
        normal_portion = total_drained - eco_portion
        
        # Remaining time at current eco rate
        eco_rate = (total_drained * 2) / (t + normal_portion)
        return p / (eco_rate / 2)

# Test cases
print(f"Detailed calculation (75, 25): {detailed_battery_calculation(75, 25):.1f} minutes")
print(f"Detailed calculation (80, 18): {detailed_battery_calculation(80, 18):.1f} minutes")
Detailed calculation (75, 25): 45.0 minutes
Detailed calculation (80, 18): 34.3 minutes

Comparison

Method Approach Best For
Direct Formula Mathematical formula Quick calculations
Step-by-Step Breakdown the process Understanding the logic

Key Points

  • Eco mode activates at 20% battery level
  • Eco mode drains at half the normal rate
  • Different formulas apply based on current battery level
  • Consider both normal and eco drain periods for accurate calculation

Conclusion

The battery drain problem requires considering two different drain rates. Use the direct formula for efficient calculation, or the step-by-step method for better understanding of the underlying logic.

Updated on: 2026-03-25T10:11:23+05:30

391 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements