Repair weighting and early return

An instrument draws constant power within each of three successive intervals:

  • 80 W80\ \text{W} for 2.0 s2.0\ \text{s};
  • 140 W140\ \text{W} for 1.0 s1.0\ \text{s};
  • 100 W100\ \text{W} for 3.0 s3.0\ \text{s}.

The time-averaged power is defined by


Pˉ=∑iPiΔti∑iΔti.\bar{P}=\frac{\sum_i P_i\Delta t_i}{\sum_i\Delta t_i}.


The input lists have matching lengths, and every duration is positive. A programmer writes:


Python
def mean_power(powers_W, durations_s):
    energy = 0.0
    elapsed = 0.0
    for i in range(len(powers_W)):
        energy += powers_W[i]
        elapsed += durations_s[i]
        return energy / elapsed


Which revision calculates the correct time-averaged power for the complete run?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Programming Techniques for Data Analysis Questions: