Problem 194325 · easy · Level 01 Prerequisites & Setup

How Hard Is the Washing Machine Shaking?

energy · power · RMS · peak-to-peak · loops

An accelerometer bolted to a washing machine samples how hard the drum shakes it, in m/s² (positive one way, negative the other, 0 at rest). The service engineer wants four numbers for a burst of N samples x[0], ..., x[N-1]:

  1. energy: the sum of the squares, x[0]² + x[1]² + ... + x[N-1]². Squaring makes pushes in both directions count, and big pushes count much more than small ones.
  2. average power: the energy divided by N, the energy per sample. It does not grow just because the recording is longer.
  3. RMS (root mean square): the square root of the average power. It is back in m/s², and it is the size of the "typical" shake.
  4. peak-to-peak: the largest sample minus the smallest, the full swing from one extreme to the other.

Write vibration_strength(x) that returns these four numbers as a tuple (energy, power, rms, peak_to_peak).

Examples

Input:  x = [3, -4, 0, 4, -3]
Output: (50, 10.0, 3.1622776601683795, 8)
Explanation: 9 + 16 + 0 + 16 + 9 = 50; 50 / 5 = 10.0; the square root of 10 is about 3.162;
the swing is from -4 up to 4, which is 8.

Input:  x = [2, 2, 2, 2]
Output: (16, 4.0, 2.0, 0)
Explanation: a constant push has an RMS equal to itself and no swing at all.

Constraints

  • answers are compared with a tolerance of 1e-6, so ints and floats are both fine

Goals

  • Compute the energy of a sampled signal as a sum of squares
  • Turn energy into average power by dividing by the number of samples
  • Tell apart the RMS size of a signal and its peak-to-peak swing
Starting Python…