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
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | 48 | 439 | 603 | 226 | 56 | 82 |
| 0 | 1 | 2 | 26 | 48 | 440 | 603 | 226 | 56 | 82 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
64 changes: 64 additions & 0 deletions kata/6-kyu/death-by-coffee/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# [Death by Coffee](https://www.codewars.com/kata/death-by-coffee "https://www.codewars.com/kata/57db78d3b43dfab59c001abe")

## Background

I drink too much coffee. Eventually it will probably kill me.

*Or will it...?*

Anyway, there's no way to know.

*Or is there...?*

## The Discovery of the Formula

I proudly announce my discovery of a formula for measuring the life-span of coffee drinkers!

For

* ```h``` is a health number assigned to each person (8 digit date of birth YYYYMMDD)
* ```CAFE``` is a cup of *regular* coffee
* ```DECAF``` is a cup of *decaffeinated* coffee

To determine the life-time coffee limits:

* Drink cups of coffee (i.e. add to ```h```) until any part of the health number includes `DEAD`
* If the test subject can survive drinking <u>five thousand</u> cups without being ```DEAD``` then coffee has no ill effect on them

# Kata Task

Given the test subject's date of birth, return an array describing their life-time coffee limits

```[ regular limit , decaffeinated limit ]```

## Notes

* The limits are ```0``` if the subject is unaffected as described above

* At least 1 cup must be consumed (Just thinking about coffee cannot kill you!)

# Examples

* <b>John</b> was born 19/Jan/1950 so ```h=19500119```. His coffee limits are ```[2645, 1162]``` which is only about 1 cup per week. You
better cut back John...How are you feeling? You don't look so well.


* <b>Susan</b> (11/Dec/1965) is unaffected by decaffeinated coffee, but regular coffee is very bad for her ```[111, 0]```. Just stick to the
decaf Susan.


* <b>Elizabeth</b> (28/Nov/1964) is allergic to decaffeinated coffee. Dead after only 11 cups ```[0, 11]```. Read the label carefully Lizzy!
Is it worth the risk?


* <b>Peter</b> (4/Sep/1965) can drink as much coffee as he likes ```[0, 0]```. You're a legend Peter!!

<hr>

Hint: https://en.wikipedia.org/wiki/Hexadecimal

<hr>

<div style='color:red'>
*Note: A life-span prediction formula this accurate has got to be worth a lot of money to somebody! I am willing to sell my copyright to the highest bidder. Contact me via the discourse section of this Kata.*
</div>
14 changes: 14 additions & 0 deletions kata/6-kyu/death-by-coffee/main/Dinglemouse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import static java.util.stream.IntStream.range;

import java.util.function.ToIntBiFunction;

interface Dinglemouse {
static int[] coffeeLimits(int year, int month, int day) {
ToIntBiFunction<int[], Integer> limit = (h, c) -> range(1, 5000)
.filter(i -> Integer.toHexString(h[0] += c).contains("dead")).findFirst().orElse(0);
return new int[]{
limit.applyAsInt(new int[]{10000 * year + 100 * month + day}, 0xCAFE),
limit.applyAsInt(new int[]{10000 * year + 100 * month + day}, 0xDECAF)
};
}
}
18 changes: 18 additions & 0 deletions kata/6-kyu/death-by-coffee/test/DinglemouseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class DinglemouseTest {
@ParameterizedTest
@CsvSource(textBlock = """
1965, 9, 4, 0, 0
1964, 11, 28, 0, 11
1965, 12, 11, 111, 0
1950, 1, 19, 2645, 1162
1880, 3, 1, 0, 3909
""")
void sample(int y, int m, int d, int cafe, int decaf) {
assertArrayEquals(new int[]{cafe, decaf}, Dinglemouse.coffeeLimits(y, m, d));
}
}
1 change: 1 addition & 0 deletions kata/6-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
- [Data compression using run-length encoding](data-compression-using-run-length-encoding)
- [Data Reverse](data-reverse)
- [Dead Ants](dead-ants)
- [Death by Coffee](death-by-coffee)
- [Decode the Morse code](decode-the-morse-code)
- [Decode the woofs!](decode-the-woofs)
- [Delete occurrences of an element if it occurs more than n times](delete-occurrences-of-an-element-if-it-occurs-more-than-n-times)
Expand Down