aboutsummaryrefslogtreecommitdiff
path: root/challenge-056/lubos-kolouch/python/ch-1.py
blob: 1ed9d35cdf8acd902805afcca5536b5b04479b1d (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
#!/usr/bin/env python
"""
    https://perlweeklychallenge.org/blog/perl-weekly-challenge-056/
    Task 1

    You are given an array @N of positive integers (sorted) and another non negative integer k.
    Write a script to find if there exists 2 indices i and j such that A[i] - A[j] = k and i != j.
    It should print the pairs of indices, if any such pairs exist.
"""


def get_valid_pairs(k: int, arr: list):
    """ get all valid pairs"""

    answer = ''

    for i in range(0, len(arr)-1):
        j = i + 1

        while (arr[j] - arr[i] < k) and (j < len(arr) - 1):
            j += 1

        if arr[j] - arr[i] == k:
            answer += f"({j},{i})"

    return answer


# TESTS
assert get_valid_pairs(2, (2, 7, 9)) == "(2,1)"
assert get_valid_pairs(2, (2, 4, 6)) == "(1,0)(2,1)"
assert get_valid_pairs(2, (2, 8, 9)) == ''