aboutsummaryrefslogtreecommitdiff
path: root/challenge-236/lubos-kolouch/python/ch-2.py
blob: ac3c40d8f83657a42205d5989e2ad5739198d658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Python implementation for "Array Loops" problem
from typing import List


def find_loops(arr: List[int]) -> int:
    visited = set()
    loop_count = 0

    for i in range(len(arr)):
        # Skip if this index has been visited
        if i in visited:
            continue

        # Initialize a new loop
        loop_count += 1
        current_index = i

        # Find members of this loop
        while current_index not in visited:
            visited.add(current_index)
            current_index = arr[current_index]

    return loop_count


# Test cases
test_cases = [
    ([4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10], 3),
    ([0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19], 6),
    ([9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17], 1),
]

for i, (arr, expected) in enumerate(test_cases):
    result = find_loops(arr)
    print(
        f"Test case {i+1}: {'Passed' if result == expected else 'Failed'} (Got: {result}, Expected: {expected})"
    )