Back to Freecodecamp

Challenge 184: 2026 Winter Games Day 5: Cross-Country Skiing

curriculum/challenges/english/blocks/daily-coding-challenges-python/697a49e6ff50d756c9b69361.md

latest3.2 KB
Original Source

--description--

Given an array of finish times for a cross-country ski race, convert them into times behind the winner.

  • Given times are strings in "H:MM:SS" format.
  • Given times will be in order from fastest to slowest.
  • The winners time (fastest time) should correspond to "0".
  • Each other time should show the time behind the winner, in the format "+M:SS".

For example, given ["1:25:32", "1:26:10", "1:27:05"], return ["0", "+0:38", "+1:33"].

--hints--

get_relative_results(["1:25:32", "1:26:10", "1:27:05"]) should return ["0", "+0:38", "+1:33"].

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_relative_results(["1:25:32", "1:26:10", "1:27:05"]), ["0", "+0:38", "+1:33"])`)
}})

get_relative_results(["1:00:01", "1:00:05", "1:00:10"]) should return ["0", "+0:04", "+0:09"].

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_relative_results(["1:00:01", "1:00:05", "1:00:10"]), ["0", "+0:04", "+0:09"])`)
}})

get_relative_results(["1:10:06", "1:10:23", "1:10:48", "1:12:11"]) should return ["0", "+0:17", "+0:42", "+2:05"].

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_relative_results(["1:10:06", "1:10:23", "1:10:48", "1:12:11"]), ["0", "+0:17", "+0:42", "+2:05"])`)
}})

get_relative_results(["0:49:13", "0:49:15", "0:50:14", "0:51:30", "0:51:58", "0:52:16", "0:53:12", "0:53:31", "0:56:19", "1:02:20"]) should return ["0", "+0:02", "+1:01", "+2:17", "+2:45", "+3:03", "+3:59", "+4:18", "+7:06", "+13:07"].

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_relative_results(["0:49:13", "0:49:15", "0:50:14", "0:51:30", "0:51:58", "0:52:16", "0:53:12", "0:53:31", "0:56:19", "1:02:20"]), ["0", "+0:02", "+1:01", "+2:17", "+2:45", "+3:03", "+3:59", "+4:18", "+7:06", "+13:07"])`)
}})

get_relative_results(["2:01:15", "2:10:45", "2:10:53", "2:11:04", "2:11:55", "2:13:27", "2:14:30", "2:15:10"]) should return ["0", "+9:30", "+9:38", "+9:49", "+10:40", "+12:12", "+13:15", "+13:55"].

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_relative_results(["2:01:15", "2:10:45", "2:10:53", "2:11:04", "2:11:55", "2:13:27", "2:14:30", "2:15:10"]), ["0", "+9:30", "+9:38", "+9:49", "+10:40", "+12:12", "+13:15", "+13:55"])`)
}})

--seed--

--seed-contents--

py
def get_relative_results(results):

    return results

--solutions--

py
def get_relative_results(results):
    def time_to_seconds(time_str):
        hours, minutes, seconds = map(int, time_str.split(':'))
        return hours * 3600 + minutes * 60 + seconds
    
    def seconds_to_time_format(secs):
        mins = secs // 60
        secs_remainder = secs % 60
        return f"+{mins}:{secs_remainder:02d}"
    
    winner_seconds = time_to_seconds(results[0])
    
    relative_results = []
    for i, time in enumerate(results):
        if i == 0:
            relative_results.append('0')
        else:
            current_seconds = time_to_seconds(time)
            difference = current_seconds - winner_seconds
            relative_results.append(seconds_to_time_format(difference))
    
    return relative_results