Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(5-kyu): kata/a-man-and-his-umbrellas #492

Merged
merged 3 commits into from
Jan 11, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/progress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
ref: ${{ github.event.workflow_run.head_branch }}

- name: Update README progress
run: |
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Codewars Handbook ☕️🚀

[![Views statistics +1 👀](https://img.shields.io/badge/dynamic/xml?color=success&label=views&query=//*[name()=%27text%27][3]&url=https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FParanoidUser%2Fcodewars-handbook)](https://hits.seeyoufarm.com/api/count/graph/dailyhits.svg?url=https://github.com/ParanoidUser/codewars-handbook)
[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.5%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed)
[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.6%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed)
[![CI pipeline 🛠](https://img.shields.io/github/actions/workflow/status/ParanoidUser/codewars-handbook/build.yml?branch=main)](https://github.com/ParanoidUser/codewars-handbook/actions/workflows/build.yml)
[![Quality gate 🔎](https://img.shields.io/sonar/alert_status/codewars-handbook?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=codewars-handbook)
[![Let's have a chat! 📞](https://img.shields.io/gitter/room/ParanoidUser/codewars-handbook?color=49c39e)](https://gitter.im/ParanoidUser/codewars-handbook)
Expand All @@ -25,7 +25,7 @@ slug.

| [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
| 0 | 1 | 2 | 26 | 43 | 416 | 559 | 206 | 55 | 79 |
| 0 | 1 | 2 | 26 | 44 | 416 | 559 | 206 | 55 | 79 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
72 changes: 72 additions & 0 deletions kata/5-kyu/a-man-and-his-umbrellas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# [A Man and his Umbrellas](https://www.codewars.com/kata/a-man-and-his-umbrellas "https://www.codewars.com/kata/58298e19c983caf4ba000c8d")

Each morning a man walks to work, and each afternoon he walks back home.

If it is raining in the morning, and he has an umbrella at home, he takes an umbrella for the journey, so he doesn't get
wet, and stores it at work. Likewise, if it is raining in the afternoon, and he has an umbrella at work, he takes an
umbrella for the journey home.

----------------------

Given an array of the weather conditions, **your task** is to work out the **minimum number of umbrellas** he needs to
start with in order that he never gets wet. He can start with some umbrellas at home and some at work, but the **output
** is a single integer, the minimum total number.

The **input** is an array/list of consecutive half-day weather forecasts. So, e.g. the first value is the 1st day's
morning weather and the second value is the 1st day's afternoon weather. The options are:

Without umbrella:

* "clear",
* "sunny",
* "cloudy",
* "overcast",
* "windy".

With umbrella:

* "rainy",
* "thunderstorms".

e.g. for three days, 6 values:

```
weather = ["rainy", "cloudy", "sunny", "sunny", "cloudy", "thunderstorms"]
```

**N.B.** He never takes an umbrella if it is not raining.

#### Examples:

* ```
minUmbrellas(["rainy", "clear", "rainy", "cloudy"])
```

should return 2

Because on the first morning, he needs an umbrella to take, and he leaves it at work.
So on the second morning, he needs a second umbrella.

* ```
minUmbrellas(["sunny", "windy", "sunny", "clear"])
```
should return 0

Because it doesn't rain at all

* ```
minUmbrellas(["rainy", "rainy", "rainy", "rainy", "thunderstorms", "rainy"])
```
should return 1

Because he only needs 1 umbrella which he takes on every journey.

#### Note for Java users

The following enum is preloaded for you:

```
public enum Weather {
CLEAR, SUNNY, CLOUDY, RAINY, OVERCAST, WINDY, THUNDERSTORMS;
}
```
13 changes: 13 additions & 0 deletions kata/5-kyu/a-man-and-his-umbrellas/main/Umbrella.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface Umbrella {
static int minUmbrellas(Weather... forecast) {
int atHome = 0;
int atWork = 0;
for (Weather weather : forecast) {
int umbrella = weather == Weather.RAINY || weather == Weather.THUNDERSTORMS ? 1 : 0;
int journey = Math.max(atWork - umbrella, 0);
atWork = atHome + umbrella;
atHome = journey;
}
return atHome + atWork;
}
}
3 changes: 3 additions & 0 deletions kata/5-kyu/a-man-and-his-umbrellas/main/Weather.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum Weather {
CLEAR, SUNNY, CLOUDY, RAINY, OVERCAST, WINDY, THUNDERSTORMS;
}
12 changes: 12 additions & 0 deletions kata/5-kyu/a-man-and-his-umbrellas/test/UmbrellaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class UmbrellaTest {
@Test
void sample() {
assertEquals(0, Umbrella.minUmbrellas(Weather.CLOUDY, Weather.WINDY, Weather.SUNNY));
assertEquals(1, Umbrella.minUmbrellas(Weather.RAINY, Weather.RAINY, Weather.RAINY, Weather.RAINY));
assertEquals(2, Umbrella.minUmbrellas(Weather.OVERCAST, Weather.RAINY, Weather.CLEAR, Weather.THUNDERSTORMS));
}
}
1 change: 1 addition & 0 deletions kata/5-kyu/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# A
- [A Man and his Umbrellas](a-man-and-his-umbrellas)
- [A Simple Music Encoder](a-simple-music-encoder)
- [Airport Arrivals/Departures - #1](airport-arrivals-slash-departures-number-1)
# B
Expand Down