Created
May 20, 2018 00:10
-
-
Save lopezm1/156cedd3b0a736eebac4d88b63c9380f to your computer and use it in GitHub Desktop.
Given an array of integers, write a function that returns true if there is a triplet (a, b, c) that satisfies a**2 + b**2 = c**2.
This file contains 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
from itertools import combinations | |
# Given an array of integers, write a function that returns true if there is a triplet (a, b, c) that satisfies a2 + b2 = c2. | |
""" | |
Input: arr[] = {3, 1, 4, 6, 5} | |
Output: True | |
There is a Pythagorean triplet (3, 4, 5). | |
""" | |
def sets_of_three(arr): | |
return list(combinations(arr, 3)) | |
def pythag(tup): | |
if (tup[0]**2) + (tup[1]**2) == tup[2]**2: | |
print(tup) | |
def pyTrip(arr): | |
allTriplets = sets_of_three(arr) | |
print(allTriplets) | |
for trip in allTriplets: | |
pythag(trip) | |
pyTrip([3, 1, 4, 6, 5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[(3, 1, 4), (3, 1, 6), (3, 1, 5), (3, 4, 6), (3, 4, 5), (3, 6, 5), (1, 4, 6), (1, 4, 5), (1, 6, 5), (4, 6, 5)]
(3, 4, 5) <- only one that satisfied