DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #36 - Let's go for a run!

Today's challenge requires you to calculate one's running pace.

For example:

runningPace(distance, time)
distance - A float with the number of kilometres
time - A string containing the time it took to travel the distance; it will always be minutes:seconds

The function should return the pace, "5:20" means it took 5 minutes and 20 seconds to travel one kilometre.

Happy Coding!


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

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

Top comments (9)

Collapse
 
easyaspython profile image
Dane Hillard • Edited
def running_pace(distance, time):
    minutes, seconds = [int(part) for part in time.split(':')]

    total_seconds = minutes * 60 + seconds
    seconds_per_km = total_seconds / distance

    pace_minutes = int(seconds_per_km // 60)
    pace_seconds = int(seconds_per_km % 60)
    return f'{pace_minutes}:{pace_seconds:02d}'
Collapse
 
ynndvn profile image
La blatte • Edited

Here it goes!

runningPace = (distance, time) => {
  const [minutes, seconds] = time.split(':').map((p) => parseInt(p));
  const total = 60 * minutes + seconds;
  const pace = total / distance;
  return `${Math.floor(pace / 60)}:${('00' + Math.floor(pace % 60)).slice(-2)}`
}
Collapse
 
matrossuch profile image
Mat-R-Such

Python

def runningPace(distance, time):
    time= time.split(':')
    time=int(time[0])*60+int(time[1])
    pace=int(time/distance)
    m=pace//60
    s=str(pace-(m*60))
    if int(s)<10:    s='0'+str
    return '%d:%s' %(m,s)
Collapse
 
lisabenmore profile image
Lisa Benmore

Not brilliant but...

function runningPace (distance, time) {
    const twoDigits = num => String(num).length < 2 ? '0' + num : num;

    const mins = parseInt(time.split(':')[0]);
    const secs = parseInt(time.split(':')[1]);
    const totalSecs = (mins * 60) + secs;

    const timePerKm = totalSecs / parseInt(distance);

    const results = [
        twoDigits(Math.floor(timePerKm / 60)),
        twoDigits(Math.round(timePerKm % 60))
    ];

    return results.join(':');
}
Collapse
 
peter279k profile image
peter279k

Here is the simple solution with JavaScript:

function runningPace(distance, time)
{
  if (distance === 1) {
    return time;
  }

  var timeArray = time.split(':');
  var minute = Number(timeArray[0]) * 60
  var seconds = Number(timeArray[1]) + minute

  seconds = Math.floor(seconds / distance);

  minute = Math.floor(seconds / 60);
  var second = seconds - minute * 60;

  if (second >= 0 && second <= 9) {
    second = "0" + String(second);
  }

  return minute + ":" + second;
}
Collapse
 
hectorpascual profile image
Héctor Pascual

Python :

def running_pace(distance, time):
    time_split = time.split(':')
    time_parsed = float(time_split[0])+float(time_split[1])/60
    time_1km = time_parsed/distance #1 km divided speed in [km / m]
    seconds_1_km = int(float(str(time_1km - int(time_1km))[1:])*60)
    minutes_1_km = int(time_1km)
    return f"{minutes_1_km}:{seconds_1_km}"
Collapse
 
hanachin profile image
Seiei Miyagi • Edited

Ruby <3

def runningPace(d, t)
  t.split ?: |> map &:to_r |> then { @1 * 60 + @2 |> quo d |> divmod 60 |> then &"%02d:%02d".:% }
end
Collapse
 
maneesh8040 profile image
Maneesh Kumar 🇮🇳

function runningPace(distance, time){
const dist = distance * 1000; //in meters
const timeInSecond =parseInt(time.split(':')[0]) * 60 + parseInt(time.split(':')[1]); //in seconds
const pace = dist / timeInSecond;
return pace

        }
Collapse
 
ianfabs profile image
Ian Fabs

I know these don't get many likes, but I really appreciate these. Like really appreciate them!