1,105,450 Community Members

average time calculation??

Member Avatar
pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi there guys i've got a script that's suppose to find the average of two times as strings. The times are in minutes:seconds:milliseconds
i'm doing ok in printing the right minutes and seconds my problem is with the milliseconds.

Example if i have 00:02:20 and 00:04:40 the average will be 00:02:30

Can anyone help me out with this please. Here is the code that i have so far:

def lap_average(lap1, lap2): 
    t1 = lap1.replace(":",'') 
    t2 = lap2.replace(":",'')   

    mins1, secs1, hundreths1 = t1[:2], t1[2:4], t1[4:] 
    mins2, secs2, hundreths2 = t2[:2], t2[2:4], t2[4:] 

    total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 60 

    millisec = (total_seconds * 1000)  
    millisec = millisec / 2 

    micro_x = millisec 
    minutes = micro_x / (60*1000) 

    micro_x = micro_x - minutes * (60*1000) 
    seconds = micro_x / 1000 

    micro_x = micro_x - seconds  

   print '%02d:%02d:%s' % (minutes, seconds, micro_x) 

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')

Thanks in Advance

Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
0
 

The average would be
(lap1 + lap2)/2
you would have to add the lap times as milliseconds then take the result and
recreate the minutes:seconds:milliseconds format

Member Avatar
pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Ene,
I think i'm already doing that. Am i not? In any cases would it be possible to provide an example.

thanks

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

If you use format
minutes:seconds:milliseconds
and show
00:02:20
do you mean
00:02:200

Member Avatar
pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want to show the milliseconds with two integers (e.g 00:02:20) the problem is i cannot get the milliseconds average correct.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Remember that '/' in Python2 is integer division.

Ahh, corrected line
micro_x = micro_x - seconds * 1000

A few test print help.

You are really using the format minutes:seconds:centiseconds

Member Avatar
pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi vegaseat,

Thanks for the reply, unfortunately i've tested your suggestion and it did not worked with the times i'm using in the code. This was the result:

04:30:0
02:30:0
04:37:500
00:02:500   # should be 00:02:30
00:03:0     # should be 00:03:30
03:05:0     # etc
31:50:0
Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

00:02:500 # should be 00:02:30

A half a second is 500 milliseconds so 2:500 (== 2.5) is correct

00:03:0 # should be 00:03:30

We don't know what code you are using so how do you expect someone to help? Take vegaseat's suggestion and print the variables total_seconds, millisec, minutes, micro_x, and seconds on the line following each calculation to determine where the error is occurring.

Also, you can use

t1 = lap1.split(":") 

def convert_to_int(lap):
    t = lap.split(":")
    return int(t[0]), int(t[1]), int(t[2])

def lap_average(lap1, lap2):
    print convert_to_int(lap1)
    print convert_to_int(lap2)

lap_average('03:40:00', '05:20:00') 

Print the "t" tuple if you don't know what it will contain.

Member Avatar
pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi woooee,
Thanks for your reply but in case you did not notice the code is posted in the first post. However I do appriciate your help. Also I would like to make a correction when said 'milliseconds' I actually meant hundredths of a second.

here is what i have so far:

def lap_average(lap1, lap2): 

    mins1, secs1, centesimos1 = lap1.split(":") 
    mins2, secs2, centesimos2 = lap2.split(":")   

    minutes = int(mins1) + int(mins2)
    seconds = float(secs1) + float(secs2)
    centesimos = float(centesimos1) + float(centesimos2)
    hundredths = int(60000 * minutes + 1000 * seconds + 0.001 * centesimos)
    hundredths = hundredths // 2

    secs, hundredths = divmod(hundredths, 1000)
    minutes, secs = divmod(secs, 60)

    print minutes, secs, hundredths

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')

which is outputs the following:

4  30 0
2  30 0
4  37 500  # and I am trying to get 04:37:50
0  2  500  # 00:02:50
0  3  0    # 00:03:30   
3  5  0    # etc
31 50 0

Can anyone help me just to get the hundreds of second right pleaseeee..

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

As stated above, I would suggest you add a print statment (after the line quoted below) and compare the output to what it should be, calculated by hand. If it does not come out then break each part down and compare, i.e calculate minutes only and compare and subtract from what total milli-seconds should be if correct giving seconds and milli-seconds remaining, then you can compare seconds and do the same and then milli-seconds (you are dividing and multiplying by 1000 not 100 so milli-second is correct).

hundredths = int(60000 * minutes + 1000 * seconds + 0.001 * centesimos)

So the following, and obviously milliseconds is the problem but you have to get to what they should be verses what they are in some way.

    minutes = int(mins1) + int(mins2)

    ## for the 2nd test ('03:00:02', '02:00:00') = 2:30:01 as median
    ## = how many milliseconds should your program have
Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
1
 

Try this:

def lap_average(lap1, lap2):

    # get minutes, seconds, centiseconds
    m1, s1, c1 = [int(x) for x in lap1.split(':')]
    m2, s2, c2 = [int(x) for x in lap2.split(':')]
    # form a list of centisecond values
    tlist1 = [m1*60*100, s1*100, c1]
    tlist2 = [m2*60*100, s2*100, c2]
    # get the total centiseconds
    centis = sum(tlist1) + sum(tlist2) 
    # take integer average
    centis = centis // 2
    # get minutes, seconds from centiseconds
    seconds, centis = divmod(centis, 100)
    minutes, secs = divmod(seconds, 60)
    print('-'*33)
    print("Given lap times %s %s" % (lap1, lap2))
    print("Average time = %02d:%02d:%02d" % (minutes, secs, centis)) 


# 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')

''' result ...
---------------------------------
Given lap times 03:40:00 05:20:00
Average time = 04:30:00
---------------------------------
Given lap times 03:00:02 02:00:00
Average time = 02:30:01
---------------------------------
Given lap times 02:25:50 06:50:75
Average time = 04:38:12
---------------------------------
Given lap times 00:02:00 00:03:00
Average time = 00:02:50
---------------------------------
Given lap times 00:02:20 00:04:40
Average time = 00:03:30
---------------------------------
Given lap times 02:40:40 03:30:30
Average time = 03:05:35
---------------------------------
Given lap times 02:60:30 60:40:40
Average time = 31:50:35
'''
Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
2
 

In order to save volunteers here from wasting time, this thread has been cross-posted on all of the other forums groups, Active State, python-list, python-forum, devshed, dreamincode, and maybe others, so decide for yourself if you want to spend any time on this or not.

Member Avatar
ZZucker
Practically a Posting Shark
829 posts since Jan 2008
Reputation Points: 313 [?]
Q&As Helped to Solve: 89 [?]
Skill Endorsements: 2 [?]
 
0
 

Holy smokes woooee, this person has been a busy little beaver.
All this because he/she confuses thousands with hundreths of a second.

Question Answered as of 1 Year Ago by woooee, vegaseat, Ene Uran and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article