### Thread: Average calculation with hundredths of a second??

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0

#### Average calculation with hundredths of a second??

Hi guys I'm trying to get the average of two times. In other words I have two times in minutes:seconds:hundredths_of_seconds and I'm trying to get the average of these times but i just cant seem to get the hundredths of seconds right.

for example if i have 00:02:20 and 00:04:40 (minutes:seconds:hundredths) it should return 00:03:30
i was able to get the minutes and the seconds just fine but i am going desperately crazy in trying to get the hundredths of seconds right.

here is the code that i have:

Code:
```def lap_average(lap1, lap2):

mins1, secs1, hundred_sec1 = lap1.split(":")
mins2, secs2, hundred_sec2 = lap2.split(":")

minutes = int(mins1) + int(mins2)
seconds = float(secs1) + float(secs2)
h_seconds = float(hundred_sec1) + float(hundred_sec2)
hundredths = int(60000 * minutes + 1000 * seconds + 0.001 * h_seconds)
hundredths = hundredths // 2

secs, hundredths = divmod(hundredths, 1000)
minutes, secs = divmod(secs, 60)
hundredths = ((hundredths/1000)*1000)
print minutes, secs, hundredths

# test times
lap_average('03:40:00', '05:20:00')
lap_average('03:00:02', '02:00:00')
lap_average('02:25:50', '06:50:75')
lap_average('00:02:00', '00:03:00')
lap_average('00:02:20', '00:04:40')
lap_average('02:40:40', '03:30:30')
lap_average('02:60:30', '60:40:40')```

P.S - please be nice i'm a newbie
2. Hang on, you're doing great! You tried to convert to a common size to average, and then back again. Furthermore, you have tests! What are the correct answers to those tests? (computed with j software, www.jsoftware.com , executable Iverson notation.)
Code:
```   F
3 40  0
5 20  0

3  0  2
2  0  0

2 25 50
6 50 75

0  2  0
0  3  0

0  2 20
0  4 40

2 40 40
3 30 30

2 60 30
60 40 40

mean"1&.(60 60 100&#.)F
4 30    0
2 30    1
4 38 12.5
0  2   50
0  3   30
3  5   35
31 50   35```
Corrected as an entirely integer computation:
Code:
```def lap_average(lap1, lap2):

mins1, secs1, hundred_sec1 = map(int,lap1.split(":"))
mins2, secs2, hundred_sec2 = map(int,lap2.split(":"))

minutes = mins1 + mins2
seconds = secs1 + secs2
h_seconds = hundred_sec1 + hundred_sec2
hundredths = h_seconds + 100*(seconds + 60*(minutes))   # integer calculation
hundredths = hundredths // 2

secs, hundredths = divmod(hundredths, 100)
minutes, secs = divmod(secs, 60)
print minutes, secs, hundredths

print '''expect {
4 30    0
2 30    1
4 38 12.5
0  2   50
0  3   30
3  5   35
31 50   35
}'''

# test times
lap_average('03:40:00', '05:20:00')
lap_average('03:00:02', '02:00:00')
lap_average('02:25:50', '06:50:75')
lap_average('00:02:00', '00:03:00')
lap_average('00:02:20', '00:04:40')
lap_average('02:40:40', '03:30:30')
lap_average('02:60:30', '60:40:40')```
Last edited by b49P23TIvg; January 10th, 2013 at 11:28 PM. Reason: Remove extra character
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0
Hi b49P23TIvg,

Thank you so much for your help, i knew i was doing some mistake with the mathematical side but i just couldn't see what.

Once again Thank you.