|
| 1 | +""" |
| 2 | +Given an array of integers and another integer target, |
| 3 | +we are required to find a triplet from the array such that it's sum is equal to |
| 4 | +the target. |
| 5 | +""" |
| 6 | +from itertools import permutations |
| 7 | +from random import randint |
| 8 | +from timeit import repeat |
| 9 | +from typing import List, Tuple |
| 10 | + |
| 11 | + |
| 12 | +def make_dataset() -> Tuple[List[int], int]: |
| 13 | + arr = [randint(-1000, 1000) for i in range(10)] |
| 14 | + r = randint(-5000, 5000) |
| 15 | + return (arr, r) |
| 16 | + |
| 17 | + |
| 18 | +dataset = make_dataset() |
| 19 | + |
| 20 | + |
| 21 | +def triplet_sum1(arr: List[int], target: int) -> Tuple[int, int, int]: |
| 22 | + """ |
| 23 | + Returns a triplet in in array with sum equal to target, |
| 24 | + else (0, 0, 0). |
| 25 | + >>> triplet_sum1([13, 29, 7, 23, 5], 35) |
| 26 | + (5, 7, 23) |
| 27 | + >>> triplet_sum1([37, 9, 19, 50, 44], 65) |
| 28 | + (9, 19, 37) |
| 29 | + >>> arr = [6, 47, 27, 1, 15] |
| 30 | + >>> target = 11 |
| 31 | + >>> triplet_sum1(arr, target) |
| 32 | + (0, 0, 0) |
| 33 | + """ |
| 34 | + for triplet in permutations(arr, 3): |
| 35 | + if sum(triplet) == target: |
| 36 | + return tuple(sorted(triplet)) |
| 37 | + return (0, 0, 0) |
| 38 | + |
| 39 | + |
| 40 | +def triplet_sum2(arr: List[int], target: int) -> Tuple[int, int, int]: |
| 41 | + """ |
| 42 | + Returns a triplet in in array with sum equal to target, |
| 43 | + else (0, 0, 0). |
| 44 | + >>> triplet_sum2([13, 29, 7, 23, 5], 35) |
| 45 | + (5, 7, 23) |
| 46 | + >>> triplet_sum2([37, 9, 19, 50, 44], 65) |
| 47 | + (9, 19, 37) |
| 48 | + >>> arr = [6, 47, 27, 1, 15] |
| 49 | + >>> target = 11 |
| 50 | + >>> triplet_sum2(arr, target) |
| 51 | + (0, 0, 0) |
| 52 | + """ |
| 53 | + arr.sort() |
| 54 | + n = len(arr) |
| 55 | + for i in range(n - 1): |
| 56 | + left, right = i + 1, n - 1 |
| 57 | + while left < right: |
| 58 | + if arr[i] + arr[left] + arr[right] == target: |
| 59 | + return (arr[i], arr[left], arr[right]) |
| 60 | + elif arr[i] + arr[left] + arr[right] < target: |
| 61 | + left += 1 |
| 62 | + elif arr[i] + arr[left] + arr[right] > target: |
| 63 | + right -= 1 |
| 64 | + else: |
| 65 | + return (0, 0, 0) |
| 66 | + |
| 67 | + |
| 68 | +def solution_times() -> Tuple[float, float]: |
| 69 | + setup_code = """ |
| 70 | +from __main__ import dataset, triplet_sum1, triplet_sum2 |
| 71 | +""" |
| 72 | + test_code1 = """ |
| 73 | +triplet_sum1(*dataset) |
| 74 | +""" |
| 75 | + test_code2 = """ |
| 76 | +triplet_sum2(*dataset) |
| 77 | +""" |
| 78 | + times1 = repeat(setup=setup_code, stmt=test_code1, repeat=5, number=10000) |
| 79 | + times2 = repeat(setup=setup_code, stmt=test_code2, repeat=5, number=10000) |
| 80 | + return (min(times1), min(times2)) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + from doctest import testmod |
| 85 | + |
| 86 | + testmod() |
| 87 | + times = solution_times() |
| 88 | + print(f"The time for naive implementation is {times[0]}.") |
| 89 | + print(f"The time for optimized implementation is {times[1]}.") |
0 commit comments