Problem Set 6(Elizabeth's exercise)

Elizabeth visits her friend Andrew and then returns home by the same route. She always walks 4 kilometers per hour (km/h) when walking uphill, 8 km/h when walking downhill, and  6 km/h when walking on level ground. Suppose the path from Elizabeth’s home to Andrew’s home consists of ‘x’ meter in the level ground, ‘y’ meter in the uphill, ‘z’ meter in the  downhill and Elizabeth starts from home by 6 a.m. Write an algorithm and the subsequent Python code to determine when Elizabeth will reach Andrew’s home and when she will reach her home back if she spends ‘m1’ minutes in Andrew’s home. For example, if x is 1000, ‘y’ is 500, ‘z’ is 300  and m1 is ’30’ minutes, then Elizabeth takes 38 min to reach Andrew’s home so she will reach Andrew’s home by 6 hour 21 min. Elizabeth will take 19 min to walk from Andrew’s home to her home and time when will reach her home back is 7 hour 10 min.
The hour can be expressed in 12-hour format (not 24-hour format).  The minutes elapsed should be rounded down to the nearest integer.
Input Format
First line contains the distance ‘x’ in level ground
Next line contains the distance ‘y’ in uphill
Next line contains the distance ‘z’ in downhill
Next line contains the value for ‘ml’ minutes spent at Andrew’s home
Output Format
Print time by which Elizabeth will reach Andrew’s home. Print hours and minutes separated by a space
Print time by which Elizabeth will reach back her home. Print hours and minutes separated by a space
Input:
uphill,dowhill,level ground distances and time taken at andrew's house

Processing:
from math import ceil
x = int(input())
y = int(input())
z = int(input())
ml = int(input())

def time_taken(level,up,down):
    time = ceil(level*60/6000) + ceil(up*60/4000) + ceil(down*60/8000)
    return time

def time_clock(time_consumed):
    hrs = 6 + time_consumed//60
    minute = 0 + time_consumed%60
    if len(str(minute)) == 1:
        minute = '0' + str(minute)
    return hrs,minute

time_andrew = time_taken(x,y,z)
time_home = time_taken(x,z,y) + ml + time_andrew

Output:
Display the time when elizabeth meets Andrew and time when she returns home
Proof:
Program:
from math import ceil
x = int(input())
y = int(input())
z = int(input())
ml = int(input())

def time_taken(level,up,down):
    time = ceil(level*60/6000) + ceil(up*60/4000) + ceil(down*60/8000)
    return time

def time_clock(time_consumed):
    hrs = 6 + time_consumed//60
    minute = 0 + time_consumed%60
    if len(str(minute)) == 1:
        minute = '0' + str(minute)
    return hrs,minute

time_andrew = time_taken(x,y,z)
time_home = time_taken(x,z,y) + ml + time_andrew
print(time_clock(time_andrew)[0],time_clock(time_andrew)[1],sep = ' ')
print(time_clock(time_home)[0],time_clock(time_home)[1],sep = ' ')


Algorithm:
Step1. Get x,y,z and ml
Step2. Define the time taken function with three parameters level, up, down
Step2.1 assign time as the sum of level multiplied by 60/6000, up multiplied by 60/4000 and down multiplied by 60/8000
Step2.2 return time
Step3. Define time_clock function with one parameter time_consumed
Step3.1 assign hrs as the sum of 6 and the value obtained after dividing time_consumed by 60
Step3.2 assign minute as the sum of 0 and the value obtained which is the remainder when time_consumed is divided by 60
Step3.3 return hrs,minute
Step4. Call the time_taken function twice to calculate the time when she reached andrew’s home and when she returned home
Step5. Call the time_clock function twice get the time in the correct format and display the time
Step6. End

2 comments:

  1. This code not running.there is a minute difference of 1 in both elizabeth and andrews time

    ReplyDelete
    Replies
    1. changed the code a little..sorry for the mistake..now its up and running

      Delete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...