Add time calculator
This commit is contained in:
parent
8728366e8f
commit
a794f63cd8
5 changed files with 136 additions and 0 deletions
2
8-scientific-computing-python/2-time-calculator/.replit
Normal file
2
8-scientific-computing-python/2-time-calculator/.replit
Normal file
|
@ -0,0 +1,2 @@
|
|||
language = "python3"
|
||||
run = "python main.py"
|
|
@ -0,0 +1,3 @@
|
|||
# Time Calculator
|
||||
|
||||
This is the boilerplate for the Time Calculator project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator
|
10
8-scientific-computing-python/2-time-calculator/main.py
Normal file
10
8-scientific-computing-python/2-time-calculator/main.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
# This entrypoint file to be used in development. Start by reading README.md
|
||||
from time_calculator import add_time
|
||||
from unittest import main
|
||||
|
||||
|
||||
print(add_time("10:10 PM", "3:30", "monday"))
|
||||
|
||||
|
||||
# Run unit tests automatically
|
||||
main(module='test_module', exit=False)
|
|
@ -0,0 +1,68 @@
|
|||
import unittest
|
||||
from time_calculator import add_time
|
||||
|
||||
|
||||
class UnitTests(unittest.TestCase):
|
||||
maxDiff = None
|
||||
def test_same_period(self):
|
||||
actual = add_time("3:30 PM", "2:12")
|
||||
expected = "5:42 PM"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "3:30 PM", "2:12" to return "5:42 PM"')
|
||||
|
||||
def test_different_period(self):
|
||||
actual = add_time("11:55 AM", "3:12")
|
||||
expected = "3:07 PM"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:55 AM", "3:12" to return "3:07 PM"')
|
||||
|
||||
def test_next_day(self):
|
||||
actual = add_time("9:15 PM", "5:30")
|
||||
expected = "2:45 AM (next day)"
|
||||
self.assertEqual(actual, expected, 'Expected time to end with "(next day)" when it is the next day.')
|
||||
|
||||
def test_period_change_at_twelve(self):
|
||||
actual = add_time("11:40 AM", "0:25")
|
||||
expected = "12:05 PM"
|
||||
self.assertEqual(actual, expected, 'Expected period to change from AM to PM at 12:00')
|
||||
|
||||
def test_twenty_four(self):
|
||||
actual = add_time("2:59 AM", "24:00")
|
||||
expected = "2:59 AM (next day)"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00" to return "2:59 AM"')
|
||||
|
||||
def test_two_days_later(self):
|
||||
actual = add_time("11:59 PM", "24:05")
|
||||
expected = "12:04 AM (2 days later)"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:59 PM", "24:05" to return "12:04 AM (2 days later)"')
|
||||
|
||||
def test_high_duration(self):
|
||||
actual = add_time("8:16 PM", "466:02")
|
||||
expected = "6:18 AM (20 days later)"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02" to return "6:18 AM (20 days later)"')
|
||||
|
||||
def test_no_change(self):
|
||||
actual = add_time("5:01 AM", "0:00")
|
||||
expected = "5:01 AM"
|
||||
self.assertEqual(actual, expected, 'Expected adding 0:00 to return initial time.')
|
||||
|
||||
def test_same_period_with_day(self):
|
||||
actual = add_time("3:30 PM", "2:12", "Monday")
|
||||
expected = "5:42 PM, Monday"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "3:30 PM", "2:12", "Monday" to return "5:42 PM, Monday"')
|
||||
|
||||
def test_twenty_four_with_day(self):
|
||||
actual = add_time("2:59 AM", "24:00", "saturDay")
|
||||
expected = "2:59 AM, Sunday (next day)"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"')
|
||||
|
||||
def test_two_days_later_with_day(self):
|
||||
actual = add_time("11:59 PM", "24:05", "Wednesday")
|
||||
expected = "12:04 AM, Friday (2 days later)"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:59 PM", "24:05", "Wednesday" to return "12:04 AM, Friday (2 days later)"')
|
||||
|
||||
def test_high_duration_with_day(self):
|
||||
actual = add_time("8:16 PM", "466:02", "tuesday")
|
||||
expected = "6:18 AM, Monday (20 days later)"
|
||||
self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"')
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
|
@ -0,0 +1,53 @@
|
|||
def add_time(start, duration, day="none"):
|
||||
# Split the input variables into useful numbers
|
||||
start = start.split(":")
|
||||
duration = duration.split(":")
|
||||
start_hrs = int(start[0])
|
||||
start_mins = int(start[1].split(" ")[0])
|
||||
start_am_pm = start[1].split(" ")[1]
|
||||
duration_hrs = int(duration[0])
|
||||
duration_mins = int(duration[1])
|
||||
|
||||
if start_am_pm.lower() == "pm": start_hrs = start_hrs + 12 # Convert to 24 hour format because that makes more sense
|
||||
|
||||
new_hrs = start_hrs + duration_hrs
|
||||
new_mins = start_mins + duration_mins
|
||||
dayslater = 0
|
||||
|
||||
# make sure minutes are under 60 and hours get adjusted for every 60 extra minutes
|
||||
while new_mins > 59:
|
||||
new_mins = new_mins - 60
|
||||
new_hrs = new_hrs + 1
|
||||
|
||||
# make sure hours are under 24 and days get adjusted for every 24 extra hours
|
||||
while new_hrs > 23:
|
||||
new_hrs = new_hrs - 24
|
||||
dayslater = dayslater + 1
|
||||
|
||||
# Convert back to 12 hour format
|
||||
if new_hrs > 12:
|
||||
new_hrs = new_hrs - 12
|
||||
new_am_pm = "PM"
|
||||
elif new_hrs == 12:
|
||||
new_am_pm = "PM"
|
||||
else:
|
||||
new_am_pm = "AM"
|
||||
|
||||
if new_hrs == 0 and new_am_pm == "AM": new_hrs = 12 # Adjust for americans being really weird, why the hell is it 12AM and not 0???
|
||||
|
||||
newtime = str(new_hrs) + ":" + str(new_mins).zfill(2) + " " + new_am_pm # Build new timestring
|
||||
|
||||
if day != "none":
|
||||
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
||||
day = day.lower().title()
|
||||
index = days.index(day)
|
||||
new_index = (index + dayslater) % len(days) # wrap around the list to find new day
|
||||
newtime = newtime + ", " + days[new_index] # add day to timestring
|
||||
|
||||
# add number of days to timestring
|
||||
if dayslater == 1:
|
||||
newtime = newtime + " (next day)"
|
||||
elif dayslater > 1:
|
||||
newtime = newtime + " (" + str(dayslater) + " days later)"
|
||||
|
||||
return newtime # done!
|
Reference in a new issue