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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import List, Tuple
def find_unique_triplets(target: int, lst: List[int]) -> List[Tuple[int, int, int]]:
lst.sort()
triplets = []
length = len(lst)
for i in range(length - 2):
left = i + 1
right = length - 1
while left < right:
s = lst[i] + lst[left] + lst[right]
if s == target:
triplets.append((lst[i], lst[left], lst[right]))
left += 1
right -= 1
elif s < target:
left += 1
else:
right -= 1
return triplets
if __name__ == "__main__":
L = [-25, -10, -7, -3, 2, 4, 8, 10]
target = 0
triplets = find_unique_triplets(target, L)
for triplet in triplets:
print(triplet)
|