Skip to content

Commit 7632b1d

Browse files
committed
Add swift-scheduling exercice
1 parent f00be4a commit 7632b1d

8 files changed

Lines changed: 516 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,14 @@
489489
"floating_point_numbers"
490490
]
491491
},
492+
{
493+
"slug": "swift-scheduling",
494+
"name": "Swift Scheduling",
495+
"uuid": "d080d214-1d41-4347-a494-1e05db9f614a",
496+
"practices": [],
497+
"prerequisites": [],
498+
"difficulty": 1
499+
},
492500
{
493501
"slug": "transpose",
494502
"name": "Transpose",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Instructions
2+
3+
Your task is to convert delivery date descriptions to _actual_ delivery dates, based on when the meeting started.
4+
5+
There are two types of delivery date descriptions:
6+
7+
1. Fixed: a predefined set of words.
8+
2. Variable: words that have a variable component, but follow a predefined set of patterns.
9+
10+
## Fixed delivery date descriptions
11+
12+
There are three fixed delivery date descriptions:
13+
14+
- `"NOW"`
15+
- `"ASAP"` (As Soon As Possible)
16+
- `"EOW"` (End Of Week)
17+
18+
The following table shows how to translate them:
19+
20+
| Description | Meeting start | Delivery date |
21+
| ----------- | ----------------------------- | ----------------------------------- |
22+
| `"NOW"` | - | Two hours after the meeting started |
23+
| `"ASAP"` | Before 13:00 | Today at 17:00 |
24+
| `"ASAP"` | After or at 13:00 | Tomorrow at 13:00 |
25+
| `"EOW"` | Monday, Tuesday, or Wednesday | Friday at 17:00 |
26+
| `"EOW"` | Thursday or Friday | Sunday at 20:00 |
27+
28+
## Variable delivery date descriptions
29+
30+
There are two variable delivery date description patterns:
31+
32+
- `"<N>M"` (N-th month)
33+
- `"Q<N>"` (N-th quarter)
34+
35+
| Description | Meeting start | Delivery date |
36+
| ----------- | ------------------------- | --------------------------------------------------------- |
37+
| `"<N>M"` | Before N-th month | At 8:00 on the _first_ workday of this year's N-th month |
38+
| `"<N>M"` | After or in N-th month | At 8:00 on the _first_ workday of next year's N-th month |
39+
| `"Q<N>"` | Before or in N-th quarter | At 8:00 on the _last_ workday of this year's N-th quarter |
40+
| `"Q<N>"` | After N-th quarter | At 8:00 on the _last_ workday of next year's N-th quarter |
41+
42+
~~~~exercism/note
43+
A workday is a Monday, Tuesday, Wednesday, Thursday, or Friday.
44+
45+
A year has four quarters, each with three months:
46+
1. January/February/March
47+
2. April/May/June
48+
3. July/August/September
49+
4. October/November/December.
50+
~~~~
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
This week, it is your turn to take notes in the department's planning meeting.
4+
In this meeting, your boss will set delivery dates for all open work items.
5+
Annoyingly, instead of specifying the _actual_ delivery dates, your boss will only _describe them_ in an abbreviated format.
6+
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.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"resu-xuniL"
4+
],
5+
"files": {
6+
"solution": [
7+
"SwiftScheduling.php"
8+
],
9+
"test": [
10+
"SwiftSchedulingTest.php"
11+
],
12+
"example": [
13+
".meta/example.php"
14+
]
15+
},
16+
"blurb": "Convert delivery date descriptions to actual delivery dates.",
17+
"source": "Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/pull/2536"
19+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class SwiftScheduling
6+
{
7+
private const QUARTER = [
8+
1 => 4,
9+
2 => 7,
10+
3 => 10,
11+
4 => 13
12+
];
13+
14+
public function deliveryDate(DateTime $meetingStart, string $description): ?DateTime
15+
{
16+
switch ($description) {
17+
case 'NOW':
18+
return $this->convertNow($meetingStart);
19+
case 'ASAP':
20+
return $this->convertAsap($meetingStart);
21+
case 'EOW':
22+
return $this->convertEow($meetingStart);
23+
default:
24+
if (str_ends_with($description, "M")) {
25+
return $this->convertVariableM($meetingStart, (int) substr($description, 0, -1));
26+
} elseif (str_starts_with($description, "Q")) {
27+
return $this->convertVariableQ($meetingStart, (int) substr($description, 1,));
28+
} else {
29+
return null;
30+
}
31+
}
32+
}
33+
34+
private function convertNow(DateTime $meetingStart): DateTime
35+
{
36+
return $meetingStart->modify('+2 hours');
37+
}
38+
39+
private function convertAsap(DateTime $meetingStart): DateTime
40+
{
41+
$meetingHour = (int) $meetingStart->format('H');
42+
43+
return $meetingHour < 13
44+
? $meetingStart->setTime(17, 0)
45+
: $meetingStart->setTime(13, 0)
46+
->modify('+1 day');
47+
}
48+
49+
private function convertEow(DateTime $meetingStart): DateTime
50+
{
51+
$meetingDay = (int) $meetingStart->format('w');
52+
53+
return $meetingDay === 1 || $meetingDay === 2 || $meetingDay === 3
54+
? $meetingStart->modify("friday")
55+
->setTime(17, 0)
56+
: $meetingStart->modify("sunday")
57+
->setTime(20, 0);
58+
}
59+
60+
private function convertVariableM(DateTime $meetingStart, int $dueMonth): DateTime
61+
{
62+
$meetingMonth = (int) $meetingStart->format('m');
63+
64+
$meetingMonth < $dueMonth
65+
? $meetingStart->setDate(
66+
(int) $meetingStart->format('Y'),
67+
$dueMonth,
68+
1
69+
)
70+
: $meetingStart->setDate(
71+
(int) $meetingStart->format('Y') + 1,
72+
$dueMonth,
73+
1
74+
);
75+
76+
$meetingDay = (int) $meetingStart->format('w');
77+
78+
return $meetingDay === 0 || $meetingDay === 6
79+
? $meetingStart->modify("first weekday")
80+
->setTime(8, 0)
81+
: $meetingStart->setTime(8, 0);
82+
}
83+
84+
private function convertVariableQ(DateTime $meetingStart, int $dueQuarter): DateTime
85+
{
86+
$meetingMonth = (int) $meetingStart->format('m');
87+
$meetingQuarter = round(($meetingMonth + 2) / 3);
88+
89+
$meetingStart->setDate(
90+
(int) $meetingStart->format('Y'),
91+
self::QUARTER[$dueQuarter],
92+
1
93+
);
94+
95+
if ($meetingQuarter > $dueQuarter) {
96+
$meetingStart->modify('+1 year');
97+
}
98+
99+
$meetingStart->modify("last weekday")
100+
->setTime(8, 0);
101+
102+
return $meetingStart;
103+
}
104+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[1d0e6e72-f370-408c-bc64-5dafa9c6da73]
13+
description = "NOW translates to two hours later"
14+
15+
[93325e7b-677d-4d96-b017-2582af879dc2]
16+
description = "ASAP before one in the afternoon translates to today at five in the afternoon"
17+
18+
[cb4252a3-c4c1-41f6-8b8c-e7269733cef8]
19+
description = "ASAP at one in the afternoon translates to tomorrow at one in the afternoon"
20+
21+
[6fddc1ea-2fe9-4c60-81f7-9220d2f45537]
22+
description = "ASAP after one in the afternoon translates to tomorrow at one in the afternoon"
23+
24+
[25f46bf9-6d2a-4e95-8edd-f62dd6bc8a6e]
25+
description = "EOW on Monday translates to Friday at five in the afternoon"
26+
27+
[0b375df5-d198-489e-acee-fd538a768616]
28+
description = "EOW on Tuesday translates to Friday at five in the afternoon"
29+
30+
[4afbb881-0b5c-46be-94e1-992cdc2a8ca4]
31+
description = "EOW on Wednesday translates to Friday at five in the afternoon"
32+
33+
[e1341c2b-5e1b-4702-a95c-a01e8e96e510]
34+
description = "EOW on Thursday translates to Sunday at eight in the evening"
35+
36+
[bbffccf7-97f7-4244-888d-bdd64348fa2e]
37+
description = "EOW on Friday translates to Sunday at eight in the evening"
38+
39+
[d651fcf4-290e-407c-8107-36b9076f39b2]
40+
description = "EOW translates to leap day"
41+
42+
[439bf09f-3a0e-44e7-bad5-b7b6d0c4505a]
43+
description = "2M before the second month of this year translates to the first workday of the second month of this year"
44+
45+
[86d82e83-c481-4fb4-9264-625de7521340]
46+
description = "11M in the eleventh month translates to the first workday of the eleventh month of next year"
47+
48+
[0d0b8f6a-1915-46f5-a630-1ff06af9da08]
49+
description = "4M in the ninth month translates to the first workday of the fourth month of next year"
50+
51+
[06d401e3-8461-438f-afae-8d26aa0289e0]
52+
description = "Q1 in the first quarter translates to the last workday of the first quarter of this year"
53+
54+
[eebd5f32-b16d-4ecd-91a0-584b0364b7ed]
55+
description = "Q4 in the second quarter translates to the last workday of the fourth quarter of this year"
56+
57+
[c920886c-44ad-4d34-a156-dc4176186581]
58+
description = "Q3 in the fourth quarter translates to the last workday of the third quarter of next year"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class SwiftScheduling
28+
{
29+
public function deliveryDate(DateTime $start, string $description): DateTime
30+
{
31+
throw new \BadMethodCallException("Implement the deliveryDate method");
32+
}
33+
}

0 commit comments

Comments
 (0)