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

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

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

thanks

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

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

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

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

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.

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..

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.