/
ch-2.py
executable file
·35 lines (30 loc) · 971 Bytes
/
ch-2.py
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
#!/usr/bin/env python
def findTriplets(ints):
solutions = []
for i in range(0, len(ints) - 3 ):
for j in range(i + 1, len(ints) - 2):
for k in range(j + 1, len(ints) - 1):
if (ints[i] != ints[j] and
ints[j] != ints[k] and
ints[i] != ints[k]):
solutions.append([i, j, k])
return solutions
def solution(ints):
intlist = ", ".join([ str(i) for i in ints ])
print(f'Input: @ints = ({intlist})')
solutions = findTriplets(ints)
print(f'Output: {len(solutions)}')
if solutions:
print("")
for triplet in solutions:
i, j, k = triplet
print(
f"({i}, {j}, {k}) because " +
f"{ints[i]} != {ints[j]} != {ints[k]}"
)
print("Example 1:")
solution([4, 4, 2, 4, 3])
print("\nExample 2:")
solution([1, 1, 1, 1, 1])
print("\nExample 3:")
solution([4, 7, 1, 10, 7, 4, 1, 1])