MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / triplet_sum1

Function triplet_sum1

maths/triplet_sum.py:23–39  ·  view source on GitHub ↗

Returns a triplet in the array with sum equal to target, else (0, 0, 0). >>> triplet_sum1([13, 29, 7, 23, 5], 35) (5, 7, 23) >>> triplet_sum1([37, 9, 19, 50, 44], 65) (9, 19, 37) >>> arr = [6, 47, 27, 1, 15] >>> target = 11 >>> triplet_sum1(arr, target) (0, 0

(arr: list[int], target: int)

Source from the content-addressed store, hash-verified

21
22
23def triplet_sum1(arr: list[int], target: int) -> tuple[int, ...]:
24 """
25 Returns a triplet in the array with sum equal to target,
26 else (0, 0, 0).
27 >>> triplet_sum1([13, 29, 7, 23, 5], 35)
28 (5, 7, 23)
29 >>> triplet_sum1([37, 9, 19, 50, 44], 65)
30 (9, 19, 37)
31 >>> arr = [6, 47, 27, 1, 15]
32 >>> target = 11
33 >>> triplet_sum1(arr, target)
34 (0, 0, 0)
35 """
36 for triplet in permutations(arr, 3):
37 if sum(triplet) == target:
38 return tuple(sorted(triplet))
39 return (0, 0, 0)
40
41
42def triplet_sum2(arr: list[int], target: int) -> tuple[int, int, int]:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected