- In John's car the GPS records every s seconds the distance travelled from an origin (distances are measured in an arbitrary but consistent unit). For example, below is part of a record with s = 15:
x = [0.0, 0.19, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25];
- The sections are:
0.0 - 0.19,
0.19 - 0.5,
0.5 - 0.75,
0.75 - 1.0,
1.0 - 1.25,
1.25 - 1.5,
1.5 - 1.75,
1.75 - 2.0,
2.0 - 2.25;
- We can calculate John's average hourly speed on every section and we get:
[45.6, 74.4, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0];
- Given
s
andx
the task is to return as an integer the*floor*
of the maximum average speed per hour obtained on the sections ofx
. If x length is less than or equal to 1 return0
since the car didn't move.
- With the above data your function
gps(s, x)
should return74
- With floats it can happen that results depends on the operations order. To calculate hourly speed you can use:
(3600 * delta_distance) / s.
Happy coding!
def gps(s, x):
if len(x) <= 1:
return 0
result = []
for index in range(len(x) - 1, 0, -1):
result.append((3600 * (x[index] - x[index - 1])) / s)
return int(max(result))
# Example usage:
x = [0.0, 0.23, 0.46, 0.69, 0.92, 1.15, 1.38, 1.61]
s = 20
print(gps(s, x)) # Output: 41