We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,111 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

average time calculation??

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

5
Contributors
12
Replies
3 Days
Discussion Span
4 Months Ago
Last Updated
14
Views
Question
Answered
pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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

Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7

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

thanks

pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

vegaseat
DaniWeb's Hypocrite
Moderator
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36

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

pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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

vegaseat
DaniWeb's Hypocrite
Moderator
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36

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
pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

woooee
Posting Maven
2,705 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

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

pmec
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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
woooee
Posting Maven
2,705 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

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
'''
Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7

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.

woooee
Posting Maven
2,705 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

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

ZZucker
Master Poster
780 posts since Jan 2008
Reputation Points: 342
Solved Threads: 60
Skill Endorsements: 1
Question Answered as of 4 Months Ago by woooee, Ene Uran, vegaseat and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.7078 seconds using 2.71MB