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

    Join Date
    Jan 2014
    Posts
    8
    Rep Power
    0

    Need help with some Python homework


    Here is the question that was asked and below that Iíll paste the code I have so far. Any pointers would be great. Please keep in mind this is only my second week with python (or any programming for that matter) so I have no idea what Iím doing. How would you code this? Anyways, any help is GREATLY APPRECIATED!

    Iíd kind of like to figure it out with % rather than subtracting everything also, but doesnít really matter either way.



    **If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?**



    seconds = 1
    hours = seconds / (60*60)
    seconds = seconds - hours*60*60
    minutes = seconds / 60
    seconds = seconds - minutes *60

    time_left_house = 6 * hours + 52 * minutes

    miles_run_easy_pace = 2 * (8 * minutes + 15 * seconds)

    miles_run_fast_pace = 3 * (7 * minutes + 12 * seconds)


    total_time_run = miles_run_easy_pace + miles_run_fast_pace + time_left_house

    print total_time_run, "Total time run: " , hours, 'Hours: ', minutes, 'Minutes: ', seconds, 'Seconds: Ď


    FYI, i'm using python 2.7.6
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Using executable Iverson notation we learn the answer is 6 seconds after 7:30.
    Code:
       +/&.(24 60 60&#.)6 52 0,(2*0 8 15),:3*0 7 12
    7 30 6
       Note 0
    The "under anaesthetics" example provides a graphic illustration. Several steps are composed:
    
      apply anaesthetics 
        cut open
          do procedure
        sew up
      wake up from anaesthetics
    
    The inverse steps are pretty important!
    )
    Which is to say we add the 6 time values under the operation "conversion to seconds". This answer makes sense because the average pace is around 8 miles per hour, we run 5 miles, and it takes, about 40 minutes.
    Code:
    >>> seconds_per_minute = 60
    >>> minutes_per_hour = 60
    >>> def hms_to_seconds(h, m, s):
    ...    return seconds_per_minute*(h*minutes_per_hour + m)
    ... 
    >>> hms_to_seconds(1,2,4) # expect 3600 + 120 + 4 = 3724
    3720
    >>> # oops, I forgot to add the seconds.
    ... 
    >>> def hms_to_seconds(h, m, s):
    ...    return seconds_per_minute * (h * minutes_per_hour + m) + s
    ... 
    >>> hms_to_seconds(1,2,4) # expect 3600 + 120 + 4 = 3724
    3724
    >>> def seconds_to_hms(seconds):
    ...     (minutes, s,) = divmod(seconds, seconds_per_minute)
    ...     (h, m,) = divmod(minutes, minutes_per_hour)
    ...     return (h, m, s,)
    ... 
    >>> seconds_to_hms(3724)  # expect 1, 2, 4
    (1, 2, 4)
    >>> seconds_to_hms(sum(hms_to_seconds(*hms) for hms in ((6, 52, 0,), (0, 8, 15,), (0, 7, 12,), (0, 7, 12,), (0, 7, 12,),  (0, 8, 15,),)))
    (7, 30, 6)
    >>>
    Last edited by b49P23TIvg; January 31st, 2014 at 09:36 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo