-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Created triplet_sum in Python/other #2362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
956c631
Add files via upload
Kush1101 f73cfb9
Update triplet_sum.py
Kush1101 bde930a
Update triplet_sum.py
Kush1101 ebbd4fb
Update other/triplet_sum.py
Kush1101 958066e
Update other/triplet_sum.py
Kush1101 f4bc29f
Update other/triplet_sum.py
Kush1101 4e480dc
Update other/triplet_sum.py
Kush1101 3d6a4fe
Update triplet_sum.py
Kush1101 9abe54d
Update other/triplet_sum.py
Kush1101 a64ee44
Update other/triplet_sum.py
Kush1101 3338c24
Update other/triplet_sum.py
Kush1101 eb60434
Update other/triplet_sum.py
Kush1101 10dd220
Update other/triplet_sum.py
Kush1101 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" | ||
Given an array of integers and another integer target, | ||
we are required to find a triplet from the array such that it's sum is equal to | ||
the target. | ||
""" | ||
from typing import List, Tuple | ||
from itertools import permutations | ||
from timeit import repeat | ||
from random import randint | ||
|
||
|
||
def make_dataset() -> Tuple[List[int], int]: | ||
arr = [randint(-1000, 1000) for i in range(10)] | ||
r = randint(-5000, 5000) | ||
return (arr, r) | ||
|
||
|
||
def triplet_sum1(arr: List[int], target: int) -> Tuple[int, int, int]: | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Returns a triplet in in 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] | ||
>>> summ = 11 | ||
>>> triplet_sum1(arr,summ) | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(0, 0, 0) | ||
""" | ||
for triplet in permutations(arr, 3): | ||
if sum(triplet) == target: | ||
return tuple(sorted(triplet)) | ||
return (0, 0, 0) | ||
|
||
|
||
def triplet_sum2(arr: List[int], target: int) -> Tuple[int, int, int]: | ||
""" | ||
Returns a triplet in in array with sum equal to target, | ||
else (0, 0, 0). | ||
>>> triplet_sum2([13, 29, 7, 23, 5], 35) | ||
(5, 7, 23) | ||
>>> triplet_sum2([37, 9, 19, 50, 44], 65) | ||
(9, 19, 37) | ||
>>> arr = [6, 47, 27, 1, 15] | ||
>>> summ = 11 | ||
>>> triplet_sum2(arr,summ) | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(0, 0, 0) | ||
""" | ||
arr.sort() | ||
n = len(arr) | ||
for i in range(n - 1): | ||
left, right = i + 1, n - 1 | ||
while left < right: | ||
if arr[i] + arr[left] + arr[right] == target: | ||
return (arr[i], arr[left], arr[right]) | ||
elif arr[i] + arr[left] + arr[right] < target: | ||
left += 1 | ||
elif arr[i] + arr[left] + arr[right] > target: | ||
right -= 1 | ||
else: | ||
return (0, 0, 0) | ||
|
||
|
||
def solution_times() -> Tuple[float, float]: | ||
setup_code = """ | ||
from __main__ import make_dataset, triplet_sum1, triplet_sum2 | ||
dataset = make_dataset() | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
test_code1 = """ | ||
triplet_sum1(*dataset) | ||
""" | ||
test_code2 = """ | ||
triplet_sum2(*dataset) | ||
""" | ||
times1 = repeat(setup=setup_code, stmt=test_code1, repeat=5, number=10000) | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
times2 = repeat(setup=setup_code, stmt=test_code2, repeat=5, number=10000) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return (min(times1), min(times2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() | ||
times = solution_times() | ||
print(f"The time for naive implementation is {times[0]}.") | ||
print(f"The time for optimized implementation is {times[1]}.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.