Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,18 @@
],
"difficulty": 4
},
{
"slug": "swift-scheduling",
"name": "Swift Scheduling",
"uuid": "7f5388dc-ce0e-40d4-98d1-7a00aeae018d",
"practices": [],
"prerequisites": [
"if-else-statements",
"datetime",
"strings"
],
"difficulty": 4
},
{
"slug": "triangle",
"name": "Triangle",
Expand Down
50 changes: 50 additions & 0 deletions exercises/practice/swift-scheduling/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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 13:00 | Today at 17:00 |
| `"ASAP"` | After or at 13:00 | Tomorrow at 13: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:

- `"<N>M"` (N-th month)
- `"Q<N>"` (N-th quarter)

| Description | Meeting start | Delivery date |
| ----------- | ------------------------- | --------------------------------------------------------- |
| `"<N>M"` | Before N-th month | At 8:00 on the _first_ workday of this year's N-th month |
| `"<N>M"` | After or in N-th month | At 8:00 on the _first_ workday of next year's N-th month |
| `"Q<N>"` | Before or in N-th quarter | At 8:00 on the _last_ workday of this year's N-th quarter |
| `"Q<N>"` | After N-th quarter | At 8:00 on the _last_ workday of next year's N-th quarter |

~~~~exercism/note
A workday is a Monday, Tuesday, Wednesday, Thursday, or Friday.

A year has four quarters, each with three months:
1. January/February/March
2. April/May/June
3. July/August/September
4. October/November/December.
~~~~
6 changes: 6 additions & 0 deletions exercises/practice/swift-scheduling/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions exercises/practice/swift-scheduling/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"zamora-carlos"
],
"files": {
"solution": [
"src/main/java/SwiftScheduling.java"
],
"test": [
"src/test/java/SwiftSchedulingTest.java"
],
"example": [
".meta/src/reference/java/SwiftScheduling.java"
]
},
"blurb": "Convert delivery date descriptions to actual delivery dates.",
"source": "Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/pull/2536"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import java.time.DayOfWeek;
import java.time.LocalDateTime;

import static java.time.DayOfWeek.*;

public class SwiftScheduling {
public static LocalDateTime convertToDeliveryDate(LocalDateTime meetingStart, String description) {
if ("NOW".equals(description)) {
return meetingStart.plusHours(2);
}

if ("ASAP".equals(description)) {
LocalDateTime sameDayAt1pm = toStartOfDay(meetingStart).withHour(13);

if (meetingStart.isBefore(sameDayAt1pm)) {
return toStartOfDay(meetingStart).withHour(17);
} else {
return toStartOfDay(meetingStart).plusDays(1).withHour(13);
}
}

if ("EOW".equals(description)) {
DayOfWeek day = meetingStart.getDayOfWeek();
LocalDateTime deliveryDate = toStartOfDay(meetingStart);

if (day == MONDAY || day == TUESDAY || day == WEDNESDAY) {
deliveryDate = deliveryDate.withHour(17);
while (deliveryDate.getDayOfWeek() != FRIDAY) {
deliveryDate = deliveryDate.plusDays(1);
}
} else if (day == THURSDAY || day == FRIDAY) {
deliveryDate = deliveryDate.withHour(20);
while (deliveryDate.getDayOfWeek() != SUNDAY) {
deliveryDate = deliveryDate.plusDays(1);
}
} else {
throw new IllegalArgumentException("Invalid day of week");
}

return deliveryDate;
}

if (description.matches("\\d+M")) {
int month = Integer.parseInt(description.substring(0, description.length() - 1));
LocalDateTime targetMonth = toStartOfDay(meetingStart)
.withMonth(month)
.withDayOfMonth(1);

if (!meetingStart.isBefore(targetMonth)) {
targetMonth = targetMonth.plusYears(1);
}

LocalDateTime deliveryDate = targetMonth.withHour(8);
while (isWeekend(deliveryDate)) {
deliveryDate = deliveryDate.plusDays(1);
}

return deliveryDate;
}

if (description.matches("Q\\d")) {
int quarter = Integer.parseInt(description.substring(1));
LocalDateTime lastDayOfQuarter = getLastDayOfQuarter(meetingStart, quarter);

if (!meetingStart.isBefore(lastDayOfQuarter.plusDays(1))) {
lastDayOfQuarter = lastDayOfQuarter.plusYears(1);
}

LocalDateTime deliveryDate = lastDayOfQuarter.withHour(8);
while (isWeekend(deliveryDate)) {
deliveryDate = deliveryDate.minusDays(1);
}

return deliveryDate;
}

throw new IllegalArgumentException("Invalid description");
}

private static LocalDateTime toStartOfDay(LocalDateTime dateTime) {
return dateTime.toLocalDate().atStartOfDay();
}

private static LocalDateTime getLastDayOfQuarter(LocalDateTime dateTime, int quarter) {
int lastMonthOfQuarter = quarter * 3;
return toStartOfDay(dateTime)
.withMonth(lastMonthOfQuarter)
.withDayOfMonth(1)
.plusMonths(1)
.minusDays(1);
}

private static boolean isWeekend(LocalDateTime date) {
DayOfWeek dayOfWeek = date.getDayOfWeek();
return dayOfWeek == SATURDAY || dayOfWeek == SUNDAY;
}
}
58 changes: 58 additions & 0 deletions exercises/practice/swift-scheduling/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via 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"

[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"

[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"
25 changes: 25 additions & 0 deletions exercises/practice/swift-scheduling/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id "java"
}

repositories {
mavenCentral()
}

dependencies {
testImplementation platform("org.junit:junit-bom:5.10.0")
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.assertj:assertj-core:3.25.1"

testRuntimeOnly "org.junit.platform:junit-platform-launcher"
}

test {
useJUnitPlatform()

testLogging {
exceptionFormat = "full"
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading