I’m currently doing the time calculator challenge
its done, passing every test apart from 3rd one( Expected time to end with '(next day)'
when it is the next day.)
Its passing the 5th one ( Calling add_time('2:59 AM', '24:00')
should return '2:59 AM (next day)'
.) which is an extension of 3rd one
code:
def add_time(start, duration,wday=''):
start=start.replace(":"," ").split()
duration=duration.replace(":"," ").split()
meri=["AM","PM"]
d=meri.index(start[2])
meri=meri[d:]+meri[:d]
weekdays=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
while True:
#start time
hr=int(start[0])
#given time
dhr=int(duration[0])
#start min
mn=start[1]
#given min
gmn=int(duration[1])
dmn=int(duration[1])+int(mn)
days=0
nd=0
day=0
mrd=""
if dmn>=60:
dmn-=60
hr+=1
meridiem=start[2]
if dhr<=12:
hr+=dhr
if meridiem=="AM" and hr>=12:
if hr==12:
pass
else:
hr=hr-12
nd=~nd
print("w")
elif meridiem=="PM" and hr>12:
hr=hr-12
day+=1
nd=0
elif meridiem=="AM" and hr<12:
if hr==12:
nd=~nd
pass
else:
hr+dhr
mrd=meri[nd]
break
elif dhr>=24:
day=0
if dhr==24 and gmn==0:
days=1
else:
days=(dhr//24)+1
if gmn!=0:
mhr=dhr*60+dmn
else:
mhr=dhr*60
thr=0
while mhr>=0:
thr+=1
if thr==60:
thr=0
if hr>12:
hr=1
nd+=1
if nd==1:
mrd=meri[~(nd-1)]
if nd==2:
mrd=meri[~(nd-1)]
if nd==2:
day+=1
if(hr+dhr)*60>1440:
g=1
nd=1
mrd=meri[~nd]
hr+=1
mhr-=1
if hr>12:
hr-=12
if gmn==0:
mrd=meri[~nd]
else:
mrd=meri[nd]
break
elif meridiem=="PM" and dhr>12 and dhr<24:
days=dhr//12
dhr=dhr%12
else:
pass
fd=""
if len(str(dmn))==1:
dmn="0"+str(dmn)
if len(wday)!=0:
wday=wday.capitalize()
cnt=days%7
o=weekdays.index(wday)
weekdays=weekdays[o:]+weekdays[:o]
fd=", "+weekdays[cnt]
new_time=f"{hr}:{dmn} {mrd}{fd}"
if days==1:
new_time+=" (next day)"
elif days>1:
new_time+=f' ({days} days later)'
print(new_time)
return new_time
add_time('11:59 PM', '24:05', 'Wednesday')
add_time('8:16 PM', '466:02', 'tuesday')
add_time('2:59 AM', '24:00')
add_time('2:59 AM', '24:00', 'saturDay')
can anyone tell me whats the problem?