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
1 change: 1 addition & 0 deletions kata/7-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
- [London CityHacker](london-cityhacker "5bce125d3bb2adff0d000245")
- [Longest vowel chain](longest-vowel-chain "59c5f4e9d751df43cf000035")
- [Looking for a benefactor](looking-for-a-benefactor "569b5cec755dd3534d00000f")
- [Loop Detector](loop-detector "68851563123e161332d2a84b")
- [Lost number in number sequence](lost-number-in-number-sequence "595aa94353e43a8746000120")
- [Love vs friendship](love-vs-friendship "59706036f6e5d1e22d000016")
- [lucky number](lucky-number "55afed09237df73343000042")
Expand Down
61 changes: 61 additions & 0 deletions kata/7-kyu/loop-detector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# [Loop Detector](https://www.codewars.com/kata/loop-detector "https://www.codewars.com/kata/68851563123e161332d2a84b")

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
the index of the next element to move to.

Your task is to determine whether this chain eventually enters a loop, or whether it terminates by stepping out of bounds.

## Function Signature

* ```arr``` is a list or array of non-negative integers.

* Return ```True``` (or your language's equivalent) if the traversal enters a loop.

* Return ```False``` (or your language's equivalent) if the traversal terminates.

## How Traversal Works

* Begin at index ```0```.

* Read the value at the current index to get the next index.

* Continue stepping through the array.

* If you encounter an index you've already visited, you've found a loop — return ```True``` (or your language's equivalent).

* If a step takes you to an index outside the array, the traversal ends — return ```False``` (or your language's equivalent).

## Examples

```
Input: 1, 2, 3, 4, 2 ➞ True
Path: 0 -> 1 -> 2 -> 3 -> 4 -> 2 -> ... (loop detected)

Input: 1, 2, 3, 4, 5 ➞ False
Path: 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> (out of bounds)

Input: 0 ➞ True
Path: 0 -> 0 -> 0 -> ... (self-loop)

Input: 3, 2, 1, 6 ➞ False
Path: 0 -> 3 -> 6 -> (out of bounds)

Input: 1, 0 ➞ True
Path: 0 -> 1 -> 0 -> 1 -> ... (cycle of two)
```

## Notes

* The input may be empty. In that case, return ```False``` (or your language's equivalent).

* All elements are guaranteed to be non-negative integers.

* You must detect any kind of loop, whether it be a:

LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts Self-loop (e.g., ```[0]```)

LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts Two-node loop (e.g., ```[1, 0]```)

LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts Larger cycle (e.g., ```[1, 2, 0]```)

* An index is considered out of bounds if it is greater than or equal to ```len(arr)```.
7 changes: 7 additions & 0 deletions kata/7-kyu/loop-detector/main/Kata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import static java.util.stream.IntStream.iterate;

interface Kata {
static boolean hasLoop(int[] arr) {
return iterate(0, i -> arr[i]).limit(arr.length + 1L).allMatch(i -> i < arr.length);
}
}
21 changes: 21 additions & 0 deletions kata/7-kyu/loop-detector/test/SampleTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class SampleTests {
@Test
void cyclic() {
assertTrue(Kata.hasLoop(new int[]{0}));
assertTrue(Kata.hasLoop(new int[]{1, 0}));
assertTrue(Kata.hasLoop(new int[]{2, 0, 1, 5}));
assertTrue(Kata.hasLoop(new int[]{1, 2, 3, 4, 2}));
}

@Test
void acyclic() {
assertFalse(Kata.hasLoop(new int[0]));
assertFalse(Kata.hasLoop(new int[]{3, 2, 1, 4}));
assertFalse(Kata.hasLoop(new int[]{1, 2, 3, 4, 5}));
}
}