From 9a0b25d8d74286348a1d3f2e5953a1eebefd91a7 Mon Sep 17 00:00:00 2001 From: Randell Dawson Date: Tue, 29 Sep 2020 10:15:47 -0700 Subject: [PATCH] initial commit --- README.md | 47 ++++++++++++++++++++++++++++++++ main.py | 10 +++++++ test_module.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++ time_calculator.py | 7 +++++ 4 files changed, 132 insertions(+) create mode 100644 README.md create mode 100644 main.py create mode 100644 test_module.py create mode 100644 time_calculator.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..46a8fa2 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +### 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. diff --git a/main.py b/main.py new file mode 100644 index 0000000..76774ed --- /dev/null +++ b/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("11:06 PM", "2:02")) + + +# Run unit tests automatically +main(module='test_module', exit=False) \ No newline at end of file diff --git a/test_module.py b/test_module.py new file mode 100644 index 0000000..490f51e --- /dev/null +++ b/test_module.py @@ -0,0 +1,68 @@ +import unittest +from time_calculator import add_time + + +class UnitTests(unittest.TestCase): + + 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 "2:59 AM", "24:00", "Friday" 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() \ No newline at end of file diff --git a/time_calculator.py b/time_calculator.py new file mode 100644 index 0000000..bd4dc05 --- /dev/null +++ b/time_calculator.py @@ -0,0 +1,7 @@ +def add_time(start, duration): + + + + + + return new_time \ No newline at end of file