You are the "computer expert" of a local Athletic Association (C.A.A.).
Many teams of runners come to compete. Each time you get a string of 
all race results of every team who has run.
For example here is a string showing the individual results of a team of 5 runners:

` "01|15|59, 1|47|6, 01|17|20, 1|32|34, 2|3|17" `

Each part of the string is of the form: ` h|m|s `
where h, m, s (h for hour, m for minutes, s for seconds) are positive or null integer (represented as strings) with one or two digits.
There are no traps in this format.

To compare the results of the teams you are asked for giving
three statistics; **range, average and median**.

`Range` : difference between the lowest and highest values. 
In {4, 6, 9, 3, 7} the lowest value is 3, and the highest is 9, 
so the range is 9 − 3 = 6.

`Mean or Average` : To calculate mean, add together all of the numbers 
in a set and then divide the sum by the total count of numbers.

`Median` : In statistics, the median is the number separating the higher half 
of a data sample from the lower half. 
The median of a finite list of numbers can be found by arranging all 
the observations from lowest value to highest value and picking the middle one 
(e.g., the median of {3, 3, 5, 9, 11} is 5) when there is an odd number of observations. 
If there is an even number of observations, then there is no single middle value; 
the median is then defined to be the mean of the two middle values
(the median of {3, 5, 6, 9} is (5 + 6) / 2 = 5.5).

Your task is to return a string giving these 3 values.  For the example given above,
the string result will be

`"Range: 00|47|18 Average: 01|35|15 Median: 01|32|34"`

of the form:

`"Range: hh|mm|ss Average: hh|mm|ss Median: hh|mm|ss"`

where hh, mm, ss are integers (represented by strings) with *each 2 digits*.

*Remarks*: 

1. if a result in seconds is ab.xy... it will be given **truncated** as ab.

2. if the given string is "" you will return ""

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Mathematics

Starter code: def stat(strg):
	

Solutions:
def stat(strg):

	def get_time(s):
		(hh, mm, ss) = [int(v) for v in s.split('|')]
		return hh * 3600 + mm * 60 + ss

	def format_time(time):
		hh = time // 3600
		mm = time // 60 % 60
		ss = time % 60
		return '{hh:02d}|{mm:02d}|{ss:02d}'.format(**locals())

	def get_range(times):
		return times[-1] - times[0]

	def get_average(times):
		return sum(times) // len(times)

	def get_median(times):
		middle = len(times) >> 1
		return times[middle] if len(times) & 1 else (times[middle - 1] + times[middle]) // 2
	if strg == '':
		return strg
	times = [get_time(s) for s in strg.split(', ')]
	times.sort()
	rng = format_time(get_range(times))
	avg = format_time(get_average(times))
	mdn = format_time(get_median(times))
	return 'Range: {rng} Average: {avg} Median: {mdn}'.format(**locals())


from statistics import median, mean

def stat(s):
	if not s:
		return ''
	t = [itime(w) for w in s.split(',')]
	return 'Range: {} Average: {} Median: {}'.format(stime(max(t) - min(t)), stime(int(mean(t))), stime(int(median(t))))

def itime(w):
	return sum([int(c) * 60 ** i for (i, c) in enumerate(w.split('|')[::-1])])

def stime(n):
	return '{:02d}|{:02d}|{:02d}'.format(n // 3600, n % 3600 // 60, n % 60)


import numpy as np
import time

def stat(strg):
	if strg == '':
		return ''
	racetimes = strg.split(',')
	secondtimes = []
	for t in racetimes:
		(h, m, s) = t.split('|')
		seconds = int(h) * 3600 + int(m) * 60 + int(s)
		secondtimes.append(seconds)
	avgs = np.mean(secondtimes)
	ranges = max(secondtimes) - min(secondtimes)
	medians = np.median(secondtimes)
	avg = time.strftime('%H|%M|%S', time.gmtime(avgs))
	ra = time.strftime('%H|%M|%S', time.gmtime(ranges))
	med = time.strftime('%H|%M|%S', time.gmtime(medians))
	return 'Range: {} Average: {} Median: {}'.format(ra, avg, med)


from statistics import mean, median

def convert_ToInt(s):
	v = s.split('|')
	return sum((int(v[i]) * 60 ** (2 - i) for i in range(3)))

def convert_ToStr(n):
	return '{:0>2}|{:0>2}|{:0>2}'.format(*map(int, (n // 3600, n % 3600 // 60, n % 60)))

def stat(strg):
	if not strg:
		return ''
	data = list(map(convert_ToInt, strg.split(', ')))
	return 'Range: {} Average: {} Median: {}'.format(*map(convert_ToStr, [max(data) - min(data), mean(data), median(data)]))


from statistics import mean, median

def sec2time(s):
	return '%02d|%02d|%02d' % (s // 3600 % 60, s // 60 % 60, s % 60)

def stat(strg):
	if not strg:
		return ''
	data = sorted([sum((int(val) * 60 ** (2 - i) for (i, val) in enumerate(t.split('|')))) for t in strg.split(', ')])
	return 'Range: %s Average: %s Median: %s' % (sec2time(data[-1] - data[0]), sec2time(int(mean(data))), sec2time(median(data)))


from datetime import timedelta as d
from statistics import mean, median

def stat(s):
	if not s:
		return ''
	li = [sum((int(j) * k for (j, k) in zip(i.split('|'), [3600, 60, 1]))) for i in s.split(', ')]
	A = lambda x: '|'.join([i.zfill(2) for i in x.split(':')])[:8]
	return f'Range: {A(str(d(seconds=max(li) - min(li))))} Average: {A(str(d(seconds=mean(li))))} Median: {A(str(d(seconds=median(li))))}'


import re
import statistics as st
P = re.compile('(\\d+)\\|(\\d+)\\|(\\d+)')

def s_to_h(time):
	(h, r) = divmod(time, 3600)
	(m, s) = divmod(r, 60)
	return f'{str(int(h)).zfill(2)}|{str(int(m)).zfill(2)}|{str(int(s)).zfill(2)}'

def stat(strg):
	t = P.findall(strg)
	if not strg:
		return ''
	s = [int(h) * 60 * 60 + int(m) * 60 + int(sec) for (h, m, sec) in t]
	(range, mean, median) = (max(s) - min(s), st.mean(s), st.median(s))
	return f'Range: {s_to_h(range)} Average: {s_to_h(mean)} Median: {s_to_h(median)}'


import statistics

def count_sec(l):
	return l[0] * 3600 + l[1] * 60 + l[2]

def t(s):
	(min, sec) = divmod(s, 60)
	(hour, min) = divmod(min, 60)
	return '|'.join([str(int(i)).zfill(2) for i in (hour, min, sec)])

def stat(strg):
	if strg == '':
		return ''
	secs = [count_sec([int(f) for f in i.split('|')]) for i in strg.split(',')]
	h_range = t(max(secs) - min(secs))
	avg = t(statistics.mean(secs))
	med = t(statistics.median(secs))
	return f'Range: {h_range} Average: {avg} Median: {med}'