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: kata/how-many-streets #677

Merged
merged 3 commits into from
Oct 12, 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
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.8%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed)
[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.9%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 | 48 | 431 | 588 | 218 | 56 | 79 |
| 0 | 1 | 2 | 26 | 48 | 431 | 590 | 218 | 56 | 79 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
32 changes: 32 additions & 0 deletions kata/7-kyu/how-many-streets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# [How Many Streets?](https://www.codewars.com/kata/how-many-streets "https://www.codewars.com/kata/66fc9ca2e6d1d0e9cc2e4a4c")

The longest street in the world, MAX_STREET, is crossed by many other streets and driven by many drivers. Determine how many streets each
driver crosses.

#### Inputs

1. A list (or array, depending on language) of streets that intersect MAX_STREET.
2. A list (or array, depending on language) of drivers. Each driver is represented by a pair of streets. The first element of the pair is
the street where they enter MAX_STREET; the second is the street they exit. The driver crosses all the streets between those two streets.

#### Output

A list (or array, depending on language) showing how many streets each driver crosses.

#### Example

```
count_streets(
["first", "second", "third", "fourth", "fifth", "sixth", "seventh"],
[("first", "second"), ("second", "seventh"), ("sixth", "fourth")]
)
```

should return `[0,4,1]`.

#### Details:

1. Each street name is a non-empty word of no more than 10 letters. There are no duplicate street names.
2. The entry and exit streets for each driver are distinct. They are guaranteed to come from the list of streets.
3. The number of streets `n` satisfies <code>2 ≤ n ≤ 10<sup>5</sup></code>. The number of drivers `d`
satisfies <code>1 ≤ d ≤ 10<sup>5</sup>.</code> So efficiency is important.
10 changes: 10 additions & 0 deletions kata/7-kyu/how-many-streets/main/HowManyStreets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.IntStream.range;

interface HowManyStreets {
static int[] countStreets(String[] streets, String[][] drivers) {
var map = range(0, streets.length).boxed().collect(toMap(i -> streets[i], i -> i));
return stream(drivers).mapToInt(d -> Math.abs(map.get(d[1]) - map.get(d[0])) - 1).toArray();
}
}
24 changes: 24 additions & 0 deletions kata/7-kyu/how-many-streets/test/HowManyStreetsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import org.junit.jupiter.api.Test;

class HowManyStreetsTest {
@Test
void sample() {
assertArrayEquals(new int[]{0, 4, 1}, HowManyStreets.countStreets(
new String[]{"first", "second", "third", "fourth", "fifth", "sixth", "seven"},
new String[][]{{"first", "second"}, {"second", "seven"}, {"sixth", "fourth"}}));

assertArrayEquals(new int[]{8, 6, 0, 4, 1, 7, 0, 8, 3, 2, 5}, HowManyStreets.countStreets(
new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
new String[][]{{"j", "a"}, {"b", "i"}, {"c", "d"}, {"e", "j"}, {"i", "g"}, {"a", "i"}, {"f", "e"}, {"a", "j"}, {"e", "a"}, {"e", "h"}, {"h", "b"}}));

assertArrayEquals(new int[]{0}, HowManyStreets.countStreets(
new String[]{"first", "second"},
new String[][]{{"second", "first"}}));

assertArrayEquals(new int[]{0, 1, 0, 0, 1}, HowManyStreets.countStreets(
new String[]{"Drive", "DrivE", "carefully"},
new String[][]{{"Drive", "DrivE"}, {"Drive", "carefully"}, {"DrivE", "Drive"}, {"Drive", "DrivE"}, {"carefully", "Drive"}}));
}
}
1 change: 1 addition & 0 deletions kata/7-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
- [How many arguments](how-many-arguments)
- [How many consecutive numbers are needed?](how-many-consecutive-numbers-are-needed)
- [How many days are we represented in a foreign country?](how-many-days-are-we-represented-in-a-foreign-country)
- [How Many Streets?](how-many-streets)
- [How many urinals are free?](how-many-urinals-are-free)
- [Hungarian Vowel Harmony (easy)](hungarian-vowel-harmony-easy)
- [Hëävÿ Mëtäl Ümläüts](heavy-metal-umlauts)
Expand Down
Loading