average time calculation??

Discussion in 'Python' started by pmec, Jan 10, 2013.

  1. pmec

    pmec Guest

    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:03:30 or 00:02:00 and 00:03:00 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
     
    pmec, Jan 10, 2013
    #1
    1. Advertising

  2. Two quick corrections to what I wrote...

    On 10 January 2013 18:13, Oscar Benjamin <> wrote:
    > On 10 January 2013 17:50, pmec <> wrote:
    >> 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.

    >
    > You might find it easier to do this arithmetic if using datetime.time
    > objects from the datetime module:
    > http://docs.python.org/2/library/datetime.html


    Actually I meant datetime.timedelta objects.

    >
    >>
    >> Example if i have 00:02:20 and 00:04:40 the average will be 00:03:30 or 00:02:00 and 00:03:00 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:]

    >
    > Are these really hundredths? At the top you said it goes
    > minutes:seconds:milliseconds. A hundredth of a second is 10
    > milliseconds so you need to be clear about which one you want.
    >
    >>
    >> total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 60

    >
    > What happened to the hundredths in the line above. Surely you wanted
    > to add 0.01 * hundredths there.
    >
    >>
    >> millisec = (total_seconds * 1000)
    >> millisec = millisec / 2

    >
    > In Python 2 this division will always round down if millisec is an
    > integer. Is that what you want?
    >
    >>
    >> micro_x = millisec

    >
    > Is the line above just there to confuse me? I thought millisec was a
    > good descriptive name. In this context micro would suggest
    > microseconds which would be 1000 times smaller.
    >
    >> minutes = micro_x / (60*1000)

    >
    > Wouldn't it have been easier to just get this from total_seconds?
    >
    >> micro_x = micro_x - minutes * (60*1000)
    >> seconds = micro_x / 1000

    >
    > This will behave differently in Python 3. Safest to use floor division // here:
    > http://stackoverflow.com/questions/...difference-between-and-when-used-for-division
    >
    >> micro_x = micro_x - seconds

    >
    > Surely that should be seconds*1000?
    >
    >>
    >> print '%02d:%02d:%s' % (minutes, seconds, micro_x)


    micro_x is in units of microseconds. Did you want microseconds or
    hundredths of a second here in the output?

    >
    > Until your done debugging, I suggest you print all of these values out
    > directly (without the format string):
    >
    > print(minutes, seconds, microx)
    >
    >>
    >> 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')

    >
    >
    > Oscar
     
    Oscar Benjamin, Jan 10, 2013
    #2
    1. Advertising

  3. pmec

    MRAB Guest

    On 2013-01-10 17:50, pmec wrote:
    > 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:03:30 or 00:02:00 and 00:03:00 will be 00:02:30
    >

    If the final 2 digits are milliseconds, then the second average would
    be 00:02:500; if they're centiseconds (hundredths of a second), then
    the second average would be 00:02:50.

    If the average you give is correct ('00:02:30'), then that suggests
    that they are in fact hours:minutes:seconds.

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

    I don't see the point of these 2 lines of removing the ':'; slice
    'lap1' and 'lap2' instead:

    mins1, secs1, hundreths1 = lap1[:2], lap1[3:5], lap1[6:]
    mins2, secs2, hundreths2 = lap2[:2], lap2[3:5], lap2[6:]

    Better yet, split on ':':

    mins1, secs1, hundreths1 = lap1.split(':')
    mins2, secs2, hundreths2 = lap2.split(':')

    Incidentally, you're talking about milliseconds, but the names say
    'hundreths'.

    A useful function is 'divmod', which, as its name suggests, performs
    both (integer) division and modulo in one step:

    seconds, millisec = divmod(millisec, 1000)
    minutes, seconds = divmod(seconds, 60)
     
    MRAB, Jan 10, 2013
    #3
  4. pmec

    pmec Guest

    Hi Oscar,

    Thank you for your reply, and you are absolutely right, I meant hundredths of a second to be outputed
     
    pmec, Jan 10, 2013
    #4
  5. pmec

    pmec Guest

    Hi Oscar,

    Thank you for your reply, and you are absolutely right, I meant hundredths of a second to be outputed
     
    pmec, Jan 10, 2013
    #5
  6. pmec

    pmec Guest

    Hi Oscar, again I do apologize for my beginner mistakes, I've changed the code taking in consideration some of your and MRAB suggestions.

    Could you give me an example on how could I use the datetime.timedelta function in this particular case.

    This is my code:


    def lap_average(lap1, lap2):

    mins1, secs1, hundreths1 = lap1.split(":")
    mins2, secs2, hundreths2 = lap2.split(":")

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

    print 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') #should output: 00:02:50
    lap_average('00:02:20', '00:04:40') # 00:03:30
    lap_average('02:40:40', '03:30:30') # etc
    lap_average('02:60:30', '60:40:40')



    Also I was a bit confused with what you said about :


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

    What happened to the hundredths in the line above. Surely you wanted
    to add 0.01 * hundredths there."


    I thought the above was already the entire time as hundredths of second??
     
    pmec, Jan 10, 2013
    #6
  7. pmec

    pmec Guest

    Hi Oscar, again I do apologize for my beginner mistakes, I've changed the code taking in consideration some of your and MRAB suggestions.

    Could you give me an example on how could I use the datetime.timedelta function in this particular case.

    This is my code:


    def lap_average(lap1, lap2):

    mins1, secs1, hundreths1 = lap1.split(":")
    mins2, secs2, hundreths2 = lap2.split(":")

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

    print 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') #should output: 00:02:50
    lap_average('00:02:20', '00:04:40') # 00:03:30
    lap_average('02:40:40', '03:30:30') # etc
    lap_average('02:60:30', '60:40:40')



    Also I was a bit confused with what you said about :


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

    What happened to the hundredths in the line above. Surely you wanted
    to add 0.01 * hundredths there."


    I thought the above was already the entire time as hundredths of second??
     
    pmec, Jan 10, 2013
    #7
  8. pmec

    Ian Kelly Guest

    On Thu, Jan 10, 2013 at 1:31 PM, pmec <> wrote:
    > Hi Oscar, again I do apologize for my beginner mistakes, I've changed the code taking in consideration some of your and MRAB suggestions.
    >
    > Could you give me an example on how could I use the datetime.timedelta function in this particular case.


    td1 = timedelta(minutes=mins1, seconds=secs1, milliseconds=hundredths1*10)
    td2 = timedelta(minutes=mins2, seconds=secs2, milliseconds=hundredths2*10)
    average = (td1 + td2) / 2
    mins, secs = divmod(average.seconds, 60)
    hundredths = average.microseconds // 10000
    print("{:02d}:{:02d}:{:02d}".format(mins, secs, hundredths))
     
    Ian Kelly, Jan 10, 2013
    #8
  9. pmec

    Thomas Boell Guest

    On Thu, 10 Jan 2013 09:50:37 -0800 (PST)
    pmec <> wrote:

    > 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:03:30 or 00:02:00 and 00:03:00 will be 00:02:30


    This is how I would probably go about it:
    Convert your strings to floating point values which describe the time
    in seconds. Look at string.split() if you do it by hand. You could also
    use a regular expression ('re' module).
    Then, calculate the average: (a+b)*0.5
    Then, convert back to your string format if you must.

    This may sound like more work at first but it is probably easier and
    less error-prone than messing with those separate values.

    Make sure you properly understand the string format first.
    minutes:seconds:milliseconds sounds unusual to me, but if you know for
    certain that is the format, then it is :)
     
    Thomas Boell, Jan 12, 2013
    #9
  10. On Sun, Jan 13, 2013 at 2:32 AM, Thomas Boell <> wrote:
    > This is how I would probably go about it:
    > Convert your strings to floating point values which describe the time
    > in seconds.



    Either floats or integers (which would be milliseconds, or whatever
    your smallest unit is). I tend to prefer integers for this sort of
    work, but do whichever you feel more comfortable working with.

    ChrisA
     
    Chris Angelico, Jan 12, 2013
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Meena Lalwani
    Replies:
    1
    Views:
    1,156
    Kevin Spencer
    Aug 31, 2005
  2. GregM

    Calculating average time

    GregM, Jul 7, 2005, in forum: Python
    Replies:
    3
    Views:
    4,664
    Peter Tillotson
    Jul 8, 2005
  3. falcon
    Replies:
    10
    Views:
    583
    falcon
    Mar 13, 2006
  4. flamesrock
    Replies:
    8
    Views:
    552
    Hendrik van Rooyen
    Nov 24, 2006
  5. Peter Szinek

    Computing average of time deltas

    Peter Szinek, Mar 19, 2007, in forum: Ruby
    Replies:
    1
    Views:
    148
    Phrogz
    Mar 19, 2007
Loading...

Share This Page