Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd46ce6

Browse files
Kyu 8 | A Wolf in sheep's clothing
1 parent baffe69 commit bd46ce6

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

‎src/main/java/kyu8/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Kata Found realized
1616

1717
- [A Strange Trip to the Market](aStrangeTripToTheMarket)
1818

19+
- [A wolf in sheep's clothing](aWolfInSheepClothing)
20+
1921
- [Find Nearest square number](findNearestSquareNumber)
2022

2123

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# A wolf in sheep's clothing
2+
3+
Wolves have been reintroduced to Great Britain. You are a sheep farmer, and are now plagued by wolves which pretend to
4+
be sheep. Fortunately, you are good at spotting them.
5+
6+
Warn the sheep in front of the wolf that it is about to be eaten. Remember that you are standing at the front of the
7+
queue which is at the end of the array:
8+
9+
```java
10+
[sheep, sheep, sheep, sheep, sheep, wolf, sheep, sheep] (YOU ARE HERE AT THE FRONT OF THE QUEUE)
11+
7 6 5 4 3 2 1
12+
```
13+
14+
If the wolf is the closest animal to you, return ```"Pls go away and stop eating my sheep".``` Otherwise, return "Oi!
15+
Sheep number N! You are about to be eaten by a wolf!" where ```N``` is the sheep's position in the queue.
16+
17+
Note: there will always be exactly one wolf in the array.
18+
19+
**Examples**
20+
21+
Input: ```["sheep", "sheep", "sheep", "wolf", "sheep"]```
22+
23+
Output: ```"Oi! Sheep number 1! You are about to be eaten by a wolf!"```
24+
25+
Input: ```["sheep", "sheep", "wolf"]```
26+
27+
Output: ```"Pls go away and stop eating my sheep"```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package kyu8.aWolfInSheepClothing;
2+
3+
public class ZywOo {
4+
public static String warnTheSheep(String[] array) {
5+
int wolf = 0;
6+
7+
for (int i = 0; i < array.length; i++) {
8+
if (array[i] == "wolf") {
9+
wolf = i;
10+
}
11+
}
12+
13+
if (wolf == (array.length - 1)) {
14+
return "Pls go away and stop eating my sheep";
15+
} else {
16+
return "Oi! Sheep number " + (array.length - wolf - 1) + "! You are about to be eaten by a wolf!";
17+
}
18+
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package kyu8.aWolfInSheepClothing;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class testZywOo {
8+
@Test
9+
public void testSomething() {
10+
assertEquals("Oi! Sheep number 2! You are about to be eaten by a wolf!", ZywOo.warnTheSheep(new String[]{"sheep", "sheep", "sheep", "sheep", "sheep", "wolf", "sheep", "sheep"}));
11+
assertEquals("Oi! Sheep number 5! You are about to be eaten by a wolf!", ZywOo.warnTheSheep(new String[]{"sheep", "wolf", "sheep", "sheep", "sheep", "sheep", "sheep"}));
12+
assertEquals("Oi! Sheep number 6! You are about to be eaten by a wolf!", ZywOo.warnTheSheep(new String[]{"wolf", "sheep", "sheep", "sheep", "sheep", "sheep", "sheep"}));
13+
assertEquals("Oi! Sheep number 1! You are about to be eaten by a wolf!", ZywOo.warnTheSheep(new String[]{"sheep", "wolf", "sheep"}));
14+
assertEquals("Pls go away and stop eating my sheep", ZywOo.warnTheSheep(new String[]{"sheep", "sheep", "wolf"}));
15+
}
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /