Problem Statement
You have a device that measures temperature once a minute. Sadly, the temperature sensor of your device is not 100% reliable, and sometimes it reports wildly inaccurate values.
You don't have the resources to buy a new device so you decide to fix the problem by writing software for your device that will throw away invalid measurements. We consider a measurement invalid if:
- the value is less than -273, or
- each value that was measured within 2 minutes before and after this one differs from this value by more than 2
In other words, when deciding whether a measurement is valid, you usually consider the previous two, and the next two measurements.
You are given a
Definition
- Class:
- MeasuringTemperature
- Method:
- averageTemperature
- Parameters:
- int[]
- Returns:
- double
- Method signature:
- double averageTemperature(int[] measuredValues)
- (be sure your method is public)
Notes
- The returned value must be accurate to within a relative or absolute error of 1E-9.
- The lowest possible temperature (in degrees Celsius) is -273.15.
- It may seem weird that only integer temperatures are used in the input. This is only done to avoid rounding errors.
Constraints
- measuredValues contains between 2 and 50 elements, inclusive.
- Each element of measuredValues will be between -1000 and 1000, inclusive.
Examples
{9, 11, 12, 13, 15}
Returns: 12.0
All measurements are valid. During this period of time it was getting warmer. The average temperature is (9+11+12+13+15)/5 = 12.
{0, 0, 0, 2, 997, -1, 0}
Returns: 0.16666666666666666
The fifth measurement is clearly invalid. The average of the valid ones is slightly positive.
{0, 0, 0, 2, -4, -1, 0}
Returns: 0.16666666666666666
This time, the fifth measurement is only slightly off, but still we consider it to be invalid.
{0, 0, 0, 2, -3, -1, 0}
Returns: -0.2857142857142857
All the measurements are valid.
{1,2,3,100,100,1,2}
Returns: 29.857142857142858
Again, all these measurements are valid. (Sadly, the sensor malfunctioned twice in a row. Our approach can't deal with this situation.)
{1,2,3,4,5,6,7,10}
Returns: 4.0
The last measurement is invalid. (The measurements made within 2 minutes from the last measurement gave results 6 and 7, and neither of these values is close enough to 10.)
{-35, -34, -34, -34, -35, 72, -34, 52, -36, -35, -36, 52, -36, -35, 981, -33}
Returns: -34.75
It's freezing cold and the sensor is malfunctioning quite often, but luckily we can identify all the wrong measurements (72, 52, 52, and 981) as invalid.
{-273, -273, -274, -273}
Returns: -273.0
The third measurement gives a temperature lower than -273, and thus is considered to be invalid.
{10, 20, 30, 40}
Returns: -300.0
No valid measurements here.
{0,0}
Returns: 0.0
{10,12}
Returns: 11.0
{10,13}
Returns: -300.0
{12,10}
Returns: 11.0
{13,10}
Returns: -300.0
{-10,-12}
Returns: -11.0
{-10,-13}
Returns: -300.0
{-12,-10}
Returns: -11.0
{-13,-10}
Returns: -300.0
{0,0,-500,0,-500,1}
Returns: 0.25
{1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000}
Returns: 1000.0
{1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,1000}
Returns: 1000.0
{10,10,13}
Returns: 10.0
{10,13,10}
Returns: 10.0
{13,10,10}
Returns: 10.0
{10,13,16,10}
Returns: -300.0
{10,13,16,20,10}
Returns: -300.0
{10,-274,-275,-274,10}
Returns: -300.0
{-13,12,-14,11,-15,10,-16,9,-17,8,-18,7,6,-19,5,-400,-400,4,-390,-300,-270,3,-12,3,2}
Returns: -1.0
{47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47}
Returns: 0.0
{47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,47}
Returns: 1.88
{10,-274,11,12,-275,-286,13}
Returns: 11.0
{-3,-4,-3,-5,-3,-2,-3}
Returns: -3.2857142857142856
{-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-2,-3,-2,-3,-3,-4,-3,-3,-2,-1,0}
Returns: -2.9
{-274,-273}
Returns: -273.0
{-274,273}
Returns: -300.0
{10, 13, 12 }
Returns: 11.666666666666666
{1, 1, 4 }
Returns: 1.0
{-273, -273 }
Returns: -273.0
{2, 10 }
Returns: -300.0
{10, 20, 30, 40 }
Returns: -300.0
{0, 0, 0, 2, 997, -1, 0 }
Returns: 0.16666666666666666
{-274, -275, -276 }
Returns: -300.0
{-273, -273, -274, -273, -500, -700, 700 }
Returns: -273.0
{2, -2, -2, -2, -2, 1 }
Returns: -2.0
{-310, 0, 1, 1 }
Returns: 0.6666666666666666
{-273, -273, -274, -273 }
Returns: -273.0
{1, 2 }
Returns: 1.5
{0, 0 }
Returns: 0.0
{0, 100 }
Returns: -300.0
{-272, -274 }
Returns: -272.0
{-35, -34, -34, -34, -35, 72, -34, 52, -36, -35, -36, 52, -36, -35, 981, -33 }
Returns: -34.75
{1, 5, 10 }
Returns: -300.0
{10, 20, 21, 40, 41 }
Returns: 30.5
{10, 0, 9 }
Returns: 9.5
{1, 100 }
Returns: -300.0
{1, 7 }
Returns: -300.0
{1, 2, 3, 4, 5, 6, 7, 10 }
Returns: 4.0
{9, 19 }
Returns: -300.0
{0, 4, 2, 6, 1 }
Returns: 2.6
{1, 6, 1 }
Returns: 1.0
{10, 0 }
Returns: -300.0
{1, 2, 3, 100, 100, 1, 2 }
Returns: 29.857142857142858
{1, 1, 3, 1, 1 }
Returns: 1.4
{3, 1, 4, 1, 3 }
Returns: 2.4
{0, 0, 0, 2, -3, -1, 0, -273, -272 }
Returns: -60.77777777777778
{50, 10, 11, 12, 13, 14, 70 }
Returns: 12.0