From 488d59e04f88622991d8b30bdc231a740388380c Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sun, 2 Mar 2025 13:18:23 -0800 Subject: [PATCH 1/7] Added files for new practice execise swift-scheduling. --- config.json | 18 ++++ .../swift-scheduling/.docs/instructions.md | 43 +++++++++ .../swift-scheduling/.docs/introduction.md | 6 ++ .../swift-scheduling/.meta/config.json | 21 +++++ .../swift-scheduling/.meta/example.py | 0 .../swift-scheduling/.meta/template.j2 | 20 ++++ .../swift-scheduling/.meta/tests.toml | 48 ++++++++++ .../swift-scheduling/swift_scheduling.py | 0 .../swift-scheduling/swift_scheduling_test.py | 93 +++++++++++++++++++ 9 files changed, 249 insertions(+) create mode 100644 exercises/practice/swift-scheduling/.docs/instructions.md create mode 100644 exercises/practice/swift-scheduling/.docs/introduction.md create mode 100644 exercises/practice/swift-scheduling/.meta/config.json create mode 100644 exercises/practice/swift-scheduling/.meta/example.py create mode 100644 exercises/practice/swift-scheduling/.meta/template.j2 create mode 100644 exercises/practice/swift-scheduling/.meta/tests.toml create mode 100644 exercises/practice/swift-scheduling/swift_scheduling.py create mode 100644 exercises/practice/swift-scheduling/swift_scheduling_test.py diff --git a/config.json b/config.json index 09bd642c98..5e76d618a8 100644 --- a/config.json +++ b/config.json @@ -981,6 +981,24 @@ ], "difficulty": 3 }, + { + "slug": "swift-scheduling", + "name": "Swift Scheduling", + "uuid": "ebddfc37-a3fc-4524-bd62-9c70f979713c", + "practices": [], + "prerequisites": ["basics", + "bools", + "conditionals", + "lists", + "list-methods", + "loops", + "numbers", + "strings", + "string-methods" + ], + "difficulty": 3, + "status": "wip" + }, { "slug": "ocr-numbers", "name": "OCR Numbers", diff --git a/exercises/practice/swift-scheduling/.docs/instructions.md b/exercises/practice/swift-scheduling/.docs/instructions.md new file mode 100644 index 0000000000..77eeed4d6d --- /dev/null +++ b/exercises/practice/swift-scheduling/.docs/instructions.md @@ -0,0 +1,43 @@ +# Instructions + +Your task is to convert delivery date descriptions to _actual_ delivery dates, based on when the meeting started. + +There are two types of delivery date descriptions: + +1. Fixed: a predefined set of words. +2. Variable: words that have a variable component, but follow a predefined set of patterns. + +## Fixed delivery date descriptions + +There are three fixed delivery date descriptions: + +- `"NOW"` +- `"ASAP"` (As Soon As Possible) +- `"EOW"` (End Of Week) + +The following table shows how to translate them: + +| Description | Meeting start | Delivery date | +| ----------- | ----------------------------- | ----------------------------------- | +| `"NOW"` | - | Two hours after the meeting started | +| `"ASAP"` | Before 12:00 | Today at 17:00 | +| `"ASAP"` | After 12:00 | Tomorrow at 12:00 | +| `"EOW"` | Monday, Tuesday, or Wednesday | Friday at 17:00 | +| `"EOW"` | Thursday or Friday | Sunday at 20:00 | + +## Variable delivery date descriptions + +There are two variable delivery date description patterns: + +- `"M"` (N-th month) +- `"Q"` (N-th quarter) + +| Description | Meeting start | Delivery date | +| ----------- | -------------------------- | ----------------------------------------------------------- | +| `"M"` | Before N-th month | At 8:00 on the _first_ workday¹ of this year's N-th month | +| `"M"` | After or in N-th month | At 8:00 on the _first_ workday¹ of next year's N-th month | +| `"Q"` | Before or in N-th quarter² | At 8:00 on the _last_ workday¹ of this year's N-th quarter² | +| `"Q"` | After N-th quarter² | At 8:00 on the _last_ workday¹ of next year's N-th quarter² | + +¹ A workday is a Monday, Tuesday, Wednesday, Thursday, or Friday. +² A year has four quarters, each with three months: January/February/March, April/May/June, July/August/September, and October/November/December. diff --git a/exercises/practice/swift-scheduling/.docs/introduction.md b/exercises/practice/swift-scheduling/.docs/introduction.md new file mode 100644 index 0000000000..2322f813ff --- /dev/null +++ b/exercises/practice/swift-scheduling/.docs/introduction.md @@ -0,0 +1,6 @@ +# Introduction + +This week, it is your turn to take notes in the department's planning meeting. +In this meeting, your boss will set delivery dates for all open work items. +Annoyingly, instead of specifying the _actual_ delivery dates, your boss will only _describe them_ in an abbreviated format. +As many of your colleagues won't be familiar with this corporate lingo, you'll need to convert these delivery date descriptions to actual delivery dates. diff --git a/exercises/practice/swift-scheduling/.meta/config.json b/exercises/practice/swift-scheduling/.meta/config.json new file mode 100644 index 0000000000..f3e8f0670d --- /dev/null +++ b/exercises/practice/swift-scheduling/.meta/config.json @@ -0,0 +1,21 @@ +{ + "authors": [ + "erikschierboom", + "bethanyg" + ], + "contributors": [], + "files": { + "solution": [ + "swift_scheduling.py" + ], + "test": [ + "swift_scheduling_test.py" + ], + "example": [ + ".meta/example.py" + ] + }, + "blurb": "Convert delivery date descriptions to actual delivery dates.", + "source": "Original exercise idea from Eric Schierboom.", + "source_url": "" +} diff --git a/exercises/practice/swift-scheduling/.meta/example.py b/exercises/practice/swift-scheduling/.meta/example.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/exercises/practice/swift-scheduling/.meta/template.j2 b/exercises/practice/swift-scheduling/.meta/template.j2 new file mode 100644 index 0000000000..eef5a58991 --- /dev/null +++ b/exercises/practice/swift-scheduling/.meta/template.j2 @@ -0,0 +1,20 @@ +{%- import "generator_macros.j2" as macros with context -%} +{{ macros.canonical_ref() }} + +{{ macros.header(imports=imports, ignore=ignore) }} + + +{% macro test_case(case) -%} + {%- set input = case["input"] -%} + def test_{{ case["description"] | to_snake }}(self): + self.assertEqual( + {{ case["property"] | to_snake }}{{ case["input"]["meetingStart"], case["input"]["description"] }}, + "{{ case["expected"] }}" + ) +{%- endmacro %} + + +class {{ exercise | camel_case }}Test(unittest.TestCase): + {% for case in cases %} + {{ test_case(case) }} + {% endfor %} diff --git a/exercises/practice/swift-scheduling/.meta/tests.toml b/exercises/practice/swift-scheduling/.meta/tests.toml new file mode 100644 index 0000000000..5b76efd35a --- /dev/null +++ b/exercises/practice/swift-scheduling/.meta/tests.toml @@ -0,0 +1,48 @@ +# This is an auto-generated file. Regular comments will be removed when this +# file is regenerated. Regenerating will not touch any manually added keys, +# so comments can be added in a "comment" key. + +[1d0e6e72-f370-408c-bc64-5dafa9c6da73] +description = "NOW translates to two hours later" + +[93325e7b-677d-4d96-b017-2582af879dc2] +description = "ASAP before one in the afternoon translates to today at five in the afternoon" + +[cb4252a3-c4c1-41f6-8b8c-e7269733cef8] +description = "ASAP at one in the afternoon translates to tomorrow at one in the afternoon" + +[6fddc1ea-2fe9-4c60-81f7-9220d2f45537] +description = "ASAP after one in the afternoon translates to tomorrow at one in the afternoon" + +[25f46bf9-6d2a-4e95-8edd-f62dd6bc8a6e] +description = "EOW on Monday translates to Friday at five in the afternoon" + +[0b375df5-d198-489e-acee-fd538a768616] +description = "EOW on Tuesday translates to Friday at five in the afternoon" + +[4afbb881-0b5c-46be-94e1-992cdc2a8ca4] +description = "EOW on Wednesday translates to Friday at five in the afternoon" + +[e1341c2b-5e1b-4702-a95c-a01e8e96e510] +description = "EOW on Thursday translates to Sunday at eight in the evening" + +[bbffccf7-97f7-4244-888d-bdd64348fa2e] +description = "EOW on Friday translates to Sunday at eight in the evening" + +[439bf09f-3a0e-44e7-bad5-b7b6d0c4505a] +description = "2M before the second month of this year translates to the first workday of the second month of this year" + +[86d82e83-c481-4fb4-9264-625de7521340] +description = "11M in the eleventh month translates to the first workday of the eleventh month of next year" + +[0d0b8f6a-1915-46f5-a630-1ff06af9da08] +description = "4M in the ninth month translates to the first workday of the fourth month of next year" + +[06d401e3-8461-438f-afae-8d26aa0289e0] +description = "Q1 in the first quarter translates to the last workday of the first quarter of this year" + +[eebd5f32-b16d-4ecd-91a0-584b0364b7ed] +description = "Q4 in the second quarter translates to the last workday of the fourth quarter of this year" + +[c920886c-44ad-4d34-a156-dc4176186581] +description = "Q3 in the fourth quarter translates to the last workday of the third quarter of next year" diff --git a/exercises/practice/swift-scheduling/swift_scheduling.py b/exercises/practice/swift-scheduling/swift_scheduling.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/exercises/practice/swift-scheduling/swift_scheduling_test.py b/exercises/practice/swift-scheduling/swift_scheduling_test.py new file mode 100644 index 0000000000..3bbe6f5797 --- /dev/null +++ b/exercises/practice/swift-scheduling/swift_scheduling_test.py @@ -0,0 +1,93 @@ +# These tests are auto-generated with test data from: +# https://github.com/exercism/problem-specifications/tree/main/exercises/swift-scheduling/canonical-data.json +# File last updated on 2025-03-02 + +import unittest + +from swift_scheduling import ( + delivery_date, +) + + +class SwiftSchedulingTest(unittest.TestCase): + def test_now_translates_to_two_hours_later(self): + self.assertEqual( + delivery_date("2012-02-13T09:00:00", "NOW"), "2012-02-13T11:00:00" + ) + + def test_asap_before_noon_translates_to_today_at_five_in_the_afternoon(self): + self.assertEqual( + delivery_date("1999-06-03T09:45:00", "ASAP"), "1999-06-03T17:00:00" + ) + + def test_asap_after_noon_translates_to_tomorrow_at_noon(self): + self.assertEqual( + delivery_date("2008-12-21T13:30:00", "ASAP"), "2008-12-22T12:00:00" + ) + + def test_eow_on_monday_translates_to_friday_at_five_in_the_afternoon(self): + self.assertEqual( + delivery_date("2025-02-03T16:00:00", "EOW"), "2025-02-07T17:00:00" + ) + + def test_eow_on_tuesday_translates_to_friday_at_five_in_the_afternoon(self): + self.assertEqual( + delivery_date("1997-04-29T10:50:00", "EOW"), "1997-05-02T17:00:00" + ) + + def test_eow_on_wednesday_translates_to_friday_at_five_in_the_afternoon(self): + self.assertEqual( + delivery_date("2005-09-14T11:00:00", "EOW"), "2005-09-16T17:00:00" + ) + + def test_eow_on_thursday_translates_to_sunday_at_eight_in_the_evening(self): + self.assertEqual( + delivery_date("2011-05-19T08:30:00", "EOW"), "2011-05-22T20:00:00" + ) + + def test_eow_on_friday_translates_to_sunday_at_eight_in_the_evening(self): + self.assertEqual( + delivery_date("2022-08-05T12:00:00", "EOW"), "2022-08-07T20:00:00" + ) + + def test_2_m_before_the_second_month_of_this_year_translates_to_the_first_workday_of_the_second_month_of_this_year( + self, + ): + self.assertEqual( + delivery_date("2007-01-02T14:15:00", "2M"), "2007-02-01T08:00:00" + ) + + def test_11_m_in_the_eleventh_month_translates_to_the_first_workday_of_the_eleventh_month_of_next_year( + self, + ): + self.assertEqual( + delivery_date("2013-11-21T15:30:00", "11M"), "2014-11-03T08:00:00" + ) + + def test_4_m_in_the_ninth_month_translates_to_the_first_workday_of_the_fourth_month_of_next_year( + self, + ): + self.assertEqual( + delivery_date("2019-11-18T15:15:00", "4M"), "2020-04-01T08:00:00" + ) + + def test_q1_in_the_first_quarter_translates_to_the_last_workday_of_the_first_quarter_of_this_year( + self, + ): + self.assertEqual( + delivery_date("2003-01-01T10:45:00", "Q1"), "2003-03-31T08:00:00" + ) + + def test_q4_in_the_second_quarter_translates_to_the_last_workday_of_the_fourth_quarter_of_this_year( + self, + ): + self.assertEqual( + delivery_date("2001-04-09T09:00:00", "Q4"), "2001-12-31T08:00:00" + ) + + def test_q3_in_the_fourth_quarter_translates_to_the_last_workday_of_the_third_quarter_of_next_year( + self, + ): + self.assertEqual( + delivery_date("2022-10-06T11:00:00", "Q3"), "2023-09-29T08:00:00" + ) From b97e2dc9176d2b2d6dde6de5c1d91bbf396f477e Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sun, 2 Mar 2025 13:26:26 -0800 Subject: [PATCH 2/7] removed source url from meta config. --- exercises/practice/swift-scheduling/.meta/config.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/exercises/practice/swift-scheduling/.meta/config.json b/exercises/practice/swift-scheduling/.meta/config.json index f3e8f0670d..9d4c63bea1 100644 --- a/exercises/practice/swift-scheduling/.meta/config.json +++ b/exercises/practice/swift-scheduling/.meta/config.json @@ -16,6 +16,5 @@ ] }, "blurb": "Convert delivery date descriptions to actual delivery dates.", - "source": "Original exercise idea from Eric Schierboom.", - "source_url": "" + "source": "Original exercise idea from Eric Schierboom." } From 910b7e50949bb41a02d27ebdfa458ddb74adc9bd Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sun, 2 Mar 2025 22:20:26 -0800 Subject: [PATCH 3/7] Added example solution and stub file. --- .../swift-scheduling/.meta/example.py | 57 +++++++++++++++++++ .../swift-scheduling/swift_scheduling.py | 2 + 2 files changed, 59 insertions(+) diff --git a/exercises/practice/swift-scheduling/.meta/example.py b/exercises/practice/swift-scheduling/.meta/example.py index e69de29bb2..89e51320c3 100644 --- a/exercises/practice/swift-scheduling/.meta/example.py +++ b/exercises/practice/swift-scheduling/.meta/example.py @@ -0,0 +1,57 @@ +from datetime import datetime, timedelta + + +def delivery_date(start, description): + start_date = datetime.fromisoformat(start) + + match description: + case 'NOW': + due_date = start_date + timedelta(hours=2) + case 'ASAP': + if str(start_date.time()) < "13:00:00": + due_date = start_date.replace(hour=17, minute=0) + else: + due_date = ( + start_date.replace(hour=12, minute=0) + + timedelta(days=1) + ) + case 'EOW': + if start_date.isoweekday() < 4: + due_date = ( + start_date.replace(hour=17, minute=0) + + timedelta(days=5 - start_date.isoweekday()) + ) + else: + due_date = ( + start_date.replace(hour=20, minute=0) + + timedelta(days=7 - start_date.isoweekday()) + ) + case description if description.endswith('M'): + month = int(description[:-1]) + target = datetime(start_date.year, month, 1, 8, 0, 0) + + if start_date.month >= target.month: + target = target.replace(year=target.year + 1) + if target.isoweekday() not in (6,7) and target.day in range(1, 8): + due_date = target + else: + if target.isoweekday() == 6: + due_date = target + timedelta(days = 2) + if target.isoweekday() == 7: + due_date = target + timedelta(days = 1) + case description if description.startswith('Q'): + target = int(description[1:]) + current = ((start_date.month + 2) // 3) + month = {"Q1":4,"Q2": 7,"Q3": 10,"Q4": 1}[description] + rollover = 1 if (current > target or target == 4) else 0 + + due_date = start_date.replace( + start_date.year + rollover, month, 1, 8, 0, 0 + ) - timedelta(days=1) + + if due_date.isoweekday() == 6: + due_date -= timedelta(days=1) + if due_date.isoweekday() == 7: + due_date -= timedelta(days=2) + + return due_date.isoformat() diff --git a/exercises/practice/swift-scheduling/swift_scheduling.py b/exercises/practice/swift-scheduling/swift_scheduling.py index e69de29bb2..332b255345 100644 --- a/exercises/practice/swift-scheduling/swift_scheduling.py +++ b/exercises/practice/swift-scheduling/swift_scheduling.py @@ -0,0 +1,2 @@ +def delivery_date(start, description): + pass \ No newline at end of file From 849db546fde0a692997beb816b75e1ee7dd453b9 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sun, 2 Mar 2025 22:23:17 -0800 Subject: [PATCH 4/7] Corrected stub file. --- exercises/practice/swift-scheduling/swift_scheduling.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/swift-scheduling/swift_scheduling.py b/exercises/practice/swift-scheduling/swift_scheduling.py index 332b255345..99fb9053eb 100644 --- a/exercises/practice/swift-scheduling/swift_scheduling.py +++ b/exercises/practice/swift-scheduling/swift_scheduling.py @@ -1,2 +1,2 @@ def delivery_date(start, description): - pass \ No newline at end of file + pass From 1c39ffd4ca35cb49914c2b5c2bbf01e3d1ed95a5 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Mon, 3 Mar 2025 10:22:32 -0800 Subject: [PATCH 5/7] Added new leapyear test case and regenerated test cases and tests toml. --- .../swift-scheduling/.meta/example.py | 4 +-- .../swift-scheduling/.meta/tests.toml | 3 +++ .../swift-scheduling/swift_scheduling_test.py | 26 +++++++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/exercises/practice/swift-scheduling/.meta/example.py b/exercises/practice/swift-scheduling/.meta/example.py index 89e51320c3..f3b4ec6b4e 100644 --- a/exercises/practice/swift-scheduling/.meta/example.py +++ b/exercises/practice/swift-scheduling/.meta/example.py @@ -12,7 +12,7 @@ def delivery_date(start, description): due_date = start_date.replace(hour=17, minute=0) else: due_date = ( - start_date.replace(hour=12, minute=0) + + start_date.replace(hour=13, minute=0) + timedelta(days=1) ) case 'EOW': @@ -54,4 +54,4 @@ def delivery_date(start, description): if due_date.isoweekday() == 7: due_date -= timedelta(days=2) - return due_date.isoformat() + return due_date.isoformat() \ No newline at end of file diff --git a/exercises/practice/swift-scheduling/.meta/tests.toml b/exercises/practice/swift-scheduling/.meta/tests.toml index 5b76efd35a..ef2aa5166b 100644 --- a/exercises/practice/swift-scheduling/.meta/tests.toml +++ b/exercises/practice/swift-scheduling/.meta/tests.toml @@ -29,6 +29,9 @@ description = "EOW on Thursday translates to Sunday at eight in the evening" [bbffccf7-97f7-4244-888d-bdd64348fa2e] description = "EOW on Friday translates to Sunday at eight in the evening" +[d651fcf4-290e-407c-8107-36b9076f39b2] +description = "EOW translates to leap day" + [439bf09f-3a0e-44e7-bad5-b7b6d0c4505a] description = "2M before the second month of this year translates to the first workday of the second month of this year" diff --git a/exercises/practice/swift-scheduling/swift_scheduling_test.py b/exercises/practice/swift-scheduling/swift_scheduling_test.py index 3bbe6f5797..3259d608bc 100644 --- a/exercises/practice/swift-scheduling/swift_scheduling_test.py +++ b/exercises/practice/swift-scheduling/swift_scheduling_test.py @@ -1,6 +1,6 @@ # These tests are auto-generated with test data from: # https://github.com/exercism/problem-specifications/tree/main/exercises/swift-scheduling/canonical-data.json -# File last updated on 2025-03-02 +# File last updated on 2025-03-03 import unittest @@ -15,14 +15,25 @@ def test_now_translates_to_two_hours_later(self): delivery_date("2012-02-13T09:00:00", "NOW"), "2012-02-13T11:00:00" ) - def test_asap_before_noon_translates_to_today_at_five_in_the_afternoon(self): + def test_asap_before_one_in_the_afternoon_translates_to_today_at_five_in_the_afternoon( + self, + ): self.assertEqual( delivery_date("1999-06-03T09:45:00", "ASAP"), "1999-06-03T17:00:00" ) - def test_asap_after_noon_translates_to_tomorrow_at_noon(self): + def test_asap_at_one_in_the_afternoon_translates_to_tomorrow_at_one_in_the_afternoon( + self, + ): + self.assertEqual( + delivery_date("2008-12-21T13:00:00", "ASAP"), "2008-12-22T13:00:00" + ) + + def test_asap_after_one_in_the_afternoon_translates_to_tomorrow_at_one_in_the_afternoon( + self, + ): self.assertEqual( - delivery_date("2008-12-21T13:30:00", "ASAP"), "2008-12-22T12:00:00" + delivery_date("2008-12-21T14:50:00", "ASAP"), "2008-12-22T13:00:00" ) def test_eow_on_monday_translates_to_friday_at_five_in_the_afternoon(self): @@ -47,7 +58,12 @@ def test_eow_on_thursday_translates_to_sunday_at_eight_in_the_evening(self): def test_eow_on_friday_translates_to_sunday_at_eight_in_the_evening(self): self.assertEqual( - delivery_date("2022-08-05T12:00:00", "EOW"), "2022-08-07T20:00:00" + delivery_date("2022-08-05T14:00:00", "EOW"), "2022-08-07T20:00:00" + ) + + def test_eow_translates_to_leap_day(self): + self.assertEqual( + delivery_date("2008-02-25T10:30:00", "EOW"), "2008-02-29T17:00:00" ) def test_2_m_before_the_second_month_of_this_year_translates_to_the_first_workday_of_the_second_month_of_this_year( From de6b58637350b951a8e3f277cfc158add887bc1b Mon Sep 17 00:00:00 2001 From: BethanyG Date: Mon, 3 Mar 2025 10:29:45 -0800 Subject: [PATCH 6/7] Updated config json to add exercise to track. --- config.json | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/config.json b/config.json index 5e76d618a8..9979973808 100644 --- a/config.json +++ b/config.json @@ -981,24 +981,6 @@ ], "difficulty": 3 }, - { - "slug": "swift-scheduling", - "name": "Swift Scheduling", - "uuid": "ebddfc37-a3fc-4524-bd62-9c70f979713c", - "practices": [], - "prerequisites": ["basics", - "bools", - "conditionals", - "lists", - "list-methods", - "loops", - "numbers", - "strings", - "string-methods" - ], - "difficulty": 3, - "status": "wip" - }, { "slug": "ocr-numbers", "name": "OCR Numbers", @@ -1345,6 +1327,23 @@ ], "difficulty": 4 }, + { + "slug": "swift-scheduling", + "name": "Swift Scheduling", + "uuid": "ebddfc37-a3fc-4524-bd62-9c70f979713c", + "practices": [], + "prerequisites": ["basics", + "bools", + "conditionals", + "lists", + "list-methods", + "loops", + "numbers", + "strings", + "string-methods" + ], + "difficulty": 4 + }, { "slug": "minesweeper", "name": "Minesweeper", From a81c03d9da3277e886494ab6a57e50acebeabfc4 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Mon, 3 Mar 2025 10:50:27 -0800 Subject: [PATCH 7/7] Rewrote example solution so CI would pass for python 3.7 - 3.9. --- .../swift-scheduling/.meta/example.py | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/exercises/practice/swift-scheduling/.meta/example.py b/exercises/practice/swift-scheduling/.meta/example.py index f3b4ec6b4e..9f411819ab 100644 --- a/exercises/practice/swift-scheduling/.meta/example.py +++ b/exercises/practice/swift-scheduling/.meta/example.py @@ -4,54 +4,54 @@ def delivery_date(start, description): start_date = datetime.fromisoformat(start) - match description: - case 'NOW': - due_date = start_date + timedelta(hours=2) - case 'ASAP': - if str(start_date.time()) < "13:00:00": - due_date = start_date.replace(hour=17, minute=0) - else: - due_date = ( - start_date.replace(hour=13, minute=0) + - timedelta(days=1) - ) - case 'EOW': - if start_date.isoweekday() < 4: - due_date = ( - start_date.replace(hour=17, minute=0) + - timedelta(days=5 - start_date.isoweekday()) - ) - else: - due_date = ( - start_date.replace(hour=20, minute=0) + - timedelta(days=7 - start_date.isoweekday()) - ) - case description if description.endswith('M'): - month = int(description[:-1]) - target = datetime(start_date.year, month, 1, 8, 0, 0) - - if start_date.month >= target.month: - target = target.replace(year=target.year + 1) - if target.isoweekday() not in (6,7) and target.day in range(1, 8): - due_date = target - else: - if target.isoweekday() == 6: - due_date = target + timedelta(days = 2) - if target.isoweekday() == 7: - due_date = target + timedelta(days = 1) - case description if description.startswith('Q'): - target = int(description[1:]) - current = ((start_date.month + 2) // 3) - month = {"Q1":4,"Q2": 7,"Q3": 10,"Q4": 1}[description] - rollover = 1 if (current > target or target == 4) else 0 - - due_date = start_date.replace( - start_date.year + rollover, month, 1, 8, 0, 0 - ) - timedelta(days=1) - - if due_date.isoweekday() == 6: - due_date -= timedelta(days=1) - if due_date.isoweekday() == 7: - due_date -= timedelta(days=2) - - return due_date.isoformat() \ No newline at end of file + + if description == 'NOW': + due_date = start_date + timedelta(hours=2) + + if description == 'ASAP': + if str(start_date.time()) < '13:00:00': + due_date = start_date.replace(hour=17, minute=0) + else: + due_date = ( + start_date.replace(hour=13, minute=0) + + timedelta(days=1) + ) + + if description =='EOW': + if start_date.isoweekday() < 4: + due_date = ( + start_date.replace(hour=17, minute=0) + + timedelta(days=5 - start_date.isoweekday()) + ) + else: + due_date = ( + start_date.replace(hour=20, minute=0) + + timedelta(days=7 - start_date.isoweekday()) + ) + + if description.endswith('M'): + month = int(description[:-1]) + target = datetime(start_date.year, month, 1, 8, 0, 0) + + if start_date.month >= target.month: + target = target.replace(year=target.year + 1) + if target.isoweekday() not in (6,7) and target.day in range(1, 8): + due_date = target + else: + if target.isoweekday() == 6: due_date = target + timedelta(days = 2) + if target.isoweekday() == 7: due_date = target + timedelta(days = 1) + + if description.startswith('Q'): + target = int(description[1:]) + current = ((start_date.month + 2) // 3) + month = {"Q1":4,"Q2": 7,"Q3": 10,"Q4": 1}[description] + rollover = 1 if (current > target or target == 4) else 0 + + due_date = start_date.replace( + start_date.year + rollover, month, 1, 8, 0, 0 + ) - timedelta(days=1) + + if due_date.isoweekday() == 6: due_date -= timedelta(days=1) + if due_date.isoweekday() == 7: due_date -= timedelta(days=2) + + return due_date.isoformat()