Skip to content

Instantly share code, notes, and snippets.

@lopezm1
Created May 20, 2018 00:10
Show Gist options
  • Save lopezm1/156cedd3b0a736eebac4d88b63c9380f to your computer and use it in GitHub Desktop.
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.
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])
@lopezm1
Copy link
Author

lopezm1 commented May 20, 2018

[(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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment