Skip to content

🐍 Program 136: Validate Pythagorean Triplet #159

@iamAntimPal

Description

@iamAntimPal

🐍 Program 136: Validate Pythagorean Triplet

📌 Description

Create a function that validates whether three given integers form a Pythagorean triplet.
The sum of the squares of the two smallest integers must equal the square of the largest number to be validated.


💡 Code Example

def is_triplet(a, b, c):
    # Sort the numbers to ensure that the largest is last
    x, y, z = sorted([a, b, c])
    return x**2 + y**2 == z**2

# Examples
print(is_triplet(3, 4, 5))  # ➞ True
# 3² + 4² = 25
# 5² = 25

print(is_triplet(13, 5, 12))  # ➞ True
# 5² + 12² = 169
# 13² = 169

print(is_triplet(1, 2, 3))  # ➞ False
# 1² + 2² = 5
# 3² = 9

✅ Output

True
True
False

🧠 Explanation

  • The function sorts the three integers so that the largest number is treated as the hypotenuse (the longest side of a right triangle).
  • It then checks if the sum of the squares of the two smaller numbers equals the square of the largest number.

Metadata

Metadata

Labels

PythonPython Interview QuestionsenhancementNew feature or requestquestionFurther information is requested

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions