From a794f63cd84594c0d592311af770193faef9383e Mon Sep 17 00:00:00 2001 From: CherryKitten Date: Sun, 27 Nov 2022 10:10:49 +0100 Subject: [PATCH] Add time calculator --- .../2-time-calculator/.replit | 2 + .../2-time-calculator/README.md | 3 + .../2-time-calculator/main.py | 10 +++ .../2-time-calculator/test_module.py | 68 +++++++++++++++++++ .../2-time-calculator/time_calculator.py | 53 +++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 8-scientific-computing-python/2-time-calculator/.replit create mode 100644 8-scientific-computing-python/2-time-calculator/README.md create mode 100644 8-scientific-computing-python/2-time-calculator/main.py create mode 100644 8-scientific-computing-python/2-time-calculator/test_module.py create mode 100644 8-scientific-computing-python/2-time-calculator/time_calculator.py diff --git a/8-scientific-computing-python/2-time-calculator/.replit b/8-scientific-computing-python/2-time-calculator/.replit new file mode 100644 index 0000000..c88f45f --- /dev/null +++ b/8-scientific-computing-python/2-time-calculator/.replit @@ -0,0 +1,2 @@ +language = "python3" +run = "python main.py" \ No newline at end of file diff --git a/8-scientific-computing-python/2-time-calculator/README.md b/8-scientific-computing-python/2-time-calculator/README.md new file mode 100644 index 0000000..0416b5c --- /dev/null +++ b/8-scientific-computing-python/2-time-calculator/README.md @@ -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 diff --git a/8-scientific-computing-python/2-time-calculator/main.py b/8-scientific-computing-python/2-time-calculator/main.py new file mode 100644 index 0000000..d5c0db0 --- /dev/null +++ b/8-scientific-computing-python/2-time-calculator/main.py @@ -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) \ No newline at end of file diff --git a/8-scientific-computing-python/2-time-calculator/test_module.py b/8-scientific-computing-python/2-time-calculator/test_module.py new file mode 100644 index 0000000..5e90154 --- /dev/null +++ b/8-scientific-computing-python/2-time-calculator/test_module.py @@ -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() diff --git a/8-scientific-computing-python/2-time-calculator/time_calculator.py b/8-scientific-computing-python/2-time-calculator/time_calculator.py new file mode 100644 index 0000000..bb757c4 --- /dev/null +++ b/8-scientific-computing-python/2-time-calculator/time_calculator.py @@ -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! \ No newline at end of file