Skip to content

Commit aa90b5e

Browse files
* docs: kata description * feat: kata/loop-detector * refactor: cast one of the operands of this addition operation to a "long" --------- Co-authored-by: ParanoidUser <[email protected]>
1 parent 943c134 commit aa90b5e

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
- [London CityHacker](london-cityhacker "5bce125d3bb2adff0d000245")
321321
- [Longest vowel chain](longest-vowel-chain "59c5f4e9d751df43cf000035")
322322
- [Looking for a benefactor](looking-for-a-benefactor "569b5cec755dd3534d00000f")
323+
- [Loop Detector](loop-detector "68851563123e161332d2a84b")
323324
- [Lost number in number sequence](lost-number-in-number-sequence "595aa94353e43a8746000120")
324325
- [Love vs friendship](love-vs-friendship "59706036f6e5d1e22d000016")
325326
- [lucky number](lucky-number "55afed09237df73343000042")

kata/7-kyu/loop-detector/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# [Loop Detector](https://www.codewars.com/kata/loop-detector "https://www.codewars.com/kata/68851563123e161332d2a84b")
2+
3+
In this kata, you'll simulate traversing a one-way pointer chain, similar to a singly linked list. Each element in the input list represents
4+
the index of the next element to move to.
5+
6+
Your task is to determine whether this chain eventually enters a loop, or whether it terminates by stepping out of bounds.
7+
8+
## Function Signature
9+
10+
* ```arr``` is a list or array of non-negative integers.
11+
12+
* Return ```True``` (or your language's equivalent) if the traversal enters a loop.
13+
14+
* Return ```False``` (or your language's equivalent) if the traversal terminates.
15+
16+
## How Traversal Works
17+
18+
* Begin at index ```0```.
19+
20+
* Read the value at the current index to get the next index.
21+
22+
* Continue stepping through the array.
23+
24+
* If you encounter an index you've already visited, you've found a loop — return ```True``` (or your language's equivalent).
25+
26+
* If a step takes you to an index outside the array, the traversal ends — return ```False``` (or your language's equivalent).
27+
28+
## Examples
29+
30+
```
31+
Input: 1, 2, 3, 4, 2 ➞ True
32+
Path: 0 -> 1 -> 2 -> 3 -> 4 -> 2 -> ... (loop detected)
33+
34+
Input: 1, 2, 3, 4, 5 ➞ False
35+
Path: 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> (out of bounds)
36+
37+
Input: 0 ➞ True
38+
Path: 0 -> 0 -> 0 -> ... (self-loop)
39+
40+
Input: 3, 2, 1, 6 ➞ False
41+
Path: 0 -> 3 -> 6 -> (out of bounds)
42+
43+
Input: 1, 0 ➞ True
44+
Path: 0 -> 1 -> 0 -> 1 -> ... (cycle of two)
45+
```
46+
47+
## Notes
48+
49+
* The input may be empty. In that case, return ```False``` (or your language's equivalent).
50+
51+
* All elements are guaranteed to be non-negative integers.
52+
53+
* You must detect any kind of loop, whether it be a:
54+
55+
LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts Self-loop (e.g., ```[0]```)
56+
57+
LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts Two-node loop (e.g., ```[1, 0]```)
58+
59+
LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts Larger cycle (e.g., ```[1, 2, 0]```)
60+
61+
* An index is considered out of bounds if it is greater than or equal to ```len(arr)```.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import static java.util.stream.IntStream.iterate;
2+
3+
interface Kata {
4+
static boolean hasLoop(int[] arr) {
5+
return iterate(0, i -> arr[i]).limit(arr.length + 1L).allMatch(i -> i < arr.length);
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import static org.junit.jupiter.api.Assertions.assertFalse;
2+
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
class SampleTests {
7+
@Test
8+
void cyclic() {
9+
assertTrue(Kata.hasLoop(new int[]{0}));
10+
assertTrue(Kata.hasLoop(new int[]{1, 0}));
11+
assertTrue(Kata.hasLoop(new int[]{2, 0, 1, 5}));
12+
assertTrue(Kata.hasLoop(new int[]{1, 2, 3, 4, 2}));
13+
}
14+
15+
@Test
16+
void acyclic() {
17+
assertFalse(Kata.hasLoop(new int[0]));
18+
assertFalse(Kata.hasLoop(new int[]{3, 2, 1, 4}));
19+
assertFalse(Kata.hasLoop(new int[]{1, 2, 3, 4, 5}));
20+
}
21+
}

0 commit comments

Comments
 (0)