loading...

Daily Challenge #114 - Speed Control

thepracticaldev profile image dev.to staff ・2 min read

In John's car the GPS records every s seconds the distance traveled from an origin point (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.50, 1.5-1.75, 1.75-2.0, 2.0-2.25

We can calculate John's average hourly speed in each section:
[45.6, 74.4, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0]

Given s and x, the task is to return as an integer the floor of the maximum average speed per hour obtained on the sections of x. If x length is less than or equal to 1 return 0 since the car didn't move.

Tests:
1
x = [0.0, 0.02, 0.36, 0.54, 0.72, 0.9, 1.08, 1.26, 1.44, 1.62, 1.8];
s = 17;

2
x = [0.0, 0.01, 0.36, 0.6, 0.84, 1.05, 1.26, 1.47, 1.68, 1.89, 2.1, 2.31, 2.52, 2.73, 2.94, 3.15];
s = 14;

Good luck, you can use (3600 * delta_distance) / s to calculate speed!


This challenge comes from g964 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

markdown guide
 

Here is the PHP code snippets:

function gps($s, $x) {
  $hourlySpeed = [];

  $index = 1;
  for (; $index<count($x); $index+=1) {
    $hourlySpeed[] = round(($x[$index] - $x[$index-1]) * 3600 / $s, 1);
  }

  $ans = $hourlySpeed[0];
  $index = 1;
  for (; $index<count($hourlySpeed); $index+=1) {
    if ($hourlySpeed[$index] - $hourlySpeed[$index-1] <= 0) {
      continue;
    }

    if ($ans < $hourlySpeed[$index]) {
      $ans = $hourlySpeed[$index];
    }
  }

  return $ans;
}
 

Elm

import List exposing (map, maximum)


type alias GpsCoordinate =
    Float


type alias GpsSection =
    ( GpsCoordinate, GpsCoordinate )


type alias GpsTime =
    Int


type alias GpsSpeed =
    Float


type alias GpsAverageSpeed =
    Int


toGpsSections : List GpsCoordinate -> List GpsSection
toGpsSections list =
    case list of
        first :: second :: rest ->
            ( first, second ) :: toGpsSections (second :: rest)

        _ ->
            []


toGpsSpeed : GpsTime -> GpsSection -> GpsSpeed
toGpsSpeed gpsTime ( firstCoordinate, secondCoordinate ) =
    (secondCoordinate - firstCoordinate) * 3600.0 / toFloat gpsTime


toGpsAverageSpeed : GpsTime -> List GpsCoordinate -> GpsAverageSpeed
toGpsAverageSpeed gpsTime gpsCoordinates =
    gpsCoordinates
        |> toGpsSections
        |> map (toGpsSpeed gpsTime)
        |> maximum
        |> Maybe.withDefault 0.0
        |> floor

Playground

Here.

 

JS

function result(x, s) {
  return Math.floor(
    Math.max(...
      x.reduce((a,b)=>[`${a}-${b}`, b])[0]
      .split(",")
      .map(e=>eval(e))
      .map(e=>Math.abs(e))
      .map(e=>3600*e/s)
    )
  )
}
 
const bepis = (s, x) => {
  let max = 0
  const lasti = x.length - 1
  let prev = x[0]
  for (let i = 1; i < lasti; i++) {
    const next = x[i]
    const avg = (next - prev) * 3600 / s
    if (avg > max) max = avg
    prev = next
  }
  return Math.floor(max)
}