freecodecamp airthematic calculator problem 2
def add_time(start, duration, days = None):
# start time parser that convert to integer\
timelist = start.split(" ")
timehour = timelist[0].split(":")
timehour.append(timelist[1])
timeminute = int(timehour[0])*60 + int(timehour[1])
#print(timeminute)
if timehour[2] == 'PM':
timeminute = timeminute + 720
#print(timeminute, "Starttime in minute")
# duration time parser that conver to integer
timedifference = duration.split(":")
DurationInMin = int(timedifference[0])*60 + int(timedifference[1])
#print(DurationInMin,"DurationInMin")
totaltimeinminute = timeminute + DurationInMin
#print(totaltimeinminute, "totaltimeinminute")
#print(DurationInMin, "durationinminute")
#print(timeminute, "time")
Day_dict = ["None1","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
if totaltimeinminute <= 720 :
daychange = 0
AMPM = 'AM'
Min = totaltimeinminute % 60
Min1 = totaltimeinminute % 60
if len(str(Min1)) == 1 :
Min = "0"+str(Min1)
Hr = int((totaltimeinminute//60))
if Hr == 0 :
Hr = "12"
if days != None :
newtime = "{}:{} {}, {}".format(Hr,Min,AMPM,days)
else:
newtime = "{}:{} {}".format(Hr,Min,AMPM)
return newtime
elif totaltimeinminute >720 and totaltimeinminute <= 1440 :
daychange = 0
AMPM = 'PM'
totaltimeinpm = totaltimeinminute - 720
Min = totaltimeinpm % 60
Min1 = totaltimeinpm % 60
if len(str(Min1)) == 1 :
Min = "0"+str(Min1)
Hr = int((totaltimeinpm//60))
if Hr == 0 :
Hr = "12"
if days != None :
newtime = "{}:{} {}, {}".format(Hr,Min,AMPM,days)
else:
newtime = "{}:{} {}".format(Hr,Min,AMPM)
return newtime
elif totaltimeinminute > 1440 :
if days != None:
daylow1 = days.lower()
daylow2 = daylow1.capitalize()
count = 0
index = 0
for i in Day_dict:
if i == daylow2:
index = count
print(index,"index")
count = count + 1
# 0 mon 12345 6Sunday
print(days, "intial input")
day1 = int(round(totaltimeinminute/1440))
print(day1, "day1")
if days != None :
if index+day1 < 8:
index = index +day1
elif index +day1 >=8 and index +day1 <15:
index = index + day1 - 7
elif index +day1 >= 15 and index + day1 < 22:
index = index + day1 -14
elif index + day1 >= 22 and index + day1 < 29:
index = index + day1 - 21
elif index +day1 >= 29 and index + day1 <36:
index = index + day1 -28
print(index ,"newindex")
weekday = Day_dict[index]
print(weekday, "output")
if day1 == 1 :
dayprint = "(next day)"
elif day1 > 1:
dayprint = "({} days later)".format(day1)
print(day1,"day")
hour1 = int(round(((totaltimeinminute/1440)-day1)*1440))
print(hour1,"hour1")
if hour1 <= 720 :
daychange = 0
AMPM = 'AM'
Min = hour1 % 60
Min1 = hour1 % 60
if len(str(Min1)) == 1 :
Min = "0"+str(Min1)
Hr = int((hour1//60))
if Hr == 0 :
Hr = "12"
if days != None :
newtime = "{}:{} {}, {} {}".format(Hr,Min,AMPM,weekday,dayprint)
else:
newtime = "{}:{} {} {}".format(Hr,Min,AMPM,dayprint)
print(newtime)
return newtime
elif hour1 >720 and hour1 <= 1440 :
daychange = 0
AMPM = 'PM'
totaltimeinpm1 = hour1 - 720
Min = totaltimeinpm1% 60
Min1 = totaltimeinpm1 % 60
if len(str(Min1)) == 1 :
Min = "0"+str(Min1)
Hr = int((hour1//60))
if Hr == 0 :
Hr = "12"
if days != None :
newtime = "{}:{} {}, {} {}".format(Hr,Min,AMPM,weekday,dayprint)
else:
newtime = "{}:{} {} {}".format(Hr,Min,AMPM,dayprint)
print(newtime,"2")
return newtime
# sumation of integer of start and duration
# understand the summed up value and convert in particular format
#
# time in minutes
# if 11:20 AM > timepassed = 11*60 + 20
# if 11:20 PM > timepassed = 11*60 + 20 +720
# add 2:20 min in 11:20 AM Tuesday minutes
# (11*60 + 20) + (2* 60 + 20) = 820
# if Days does not change then DayCount = 0
# if value <= 720 then use AM
# if value > 720 and Value <= 1440 then PM
# if Value 720 PM 0 ... 1420 PM 0 0020 AM 1
'''### Assignment
Write a function named `add_time` that takes in two required parameters and one optional parameter:
* a start time in the 12-hour clock format (ending in AM or PM)
* a duration time that indicates the number of hours and minutes
* (optional) a starting day of the week, case insensitive
The function should add the duration time to the start time and return the result.
If the result will be the next day, it should show `(next day)` after the time. If the result will be more than one day later, it should show `(n days later)` after the time, where "n" is the number of days later.
If the function is given the optional starting day of the week parameter, then the output should display the day of the week of the result. The day of the week in the output should appear after the time and before the number of days later.
Below are some examples of different cases the function should handle. Pay close attention to the spacing and punctuation of the results.
```py
add_time("3:00 PM", "3:10")
# Returns: 6:10 PM
add_time("11:30 AM", "2:32", "Monday")
# Returns: 2:02 PM, Monday
add_time("11:43 AM", "00:20")
# Returns: 12:03 PM
add_time("10:10 PM", "3:30")
# Returns: 1:40 AM (next day)
add_time("11:43 PM", "24:20", "tueSday")
# Returns: 12:03 AM, Thursday (2 days later)
add_time("6:30 PM", "205:12")
# Returns: 7:42 AM (9 days later)
```
Do not import any Python libraries. Assume that the start times are valid times. The minutes in the duration time will be a whole number less than 60, but the hour can be any whole number.
### Development
Write your code in `time_calculator.py`. For development, you can use `main.py` to test your `time_calculator()` function. Click the "run" button and `main.py` will run.
### Testing
The unit tests for this project are in `test_module.py`. We imported the tests from `test_module.py` to `main.py` for your convenience. The tests will run automatically whenever you hit the "run" button.
### Submitting
Copy your project's URL and submit it to freeCodeCamp.
'''
Comments
Post a Comment