Part of the series

Several example codes

~2 min read • Updated Oct 8, 2025

Program Overview

This Python program receives a list of numbers and checks all possible 3-number combinations.
It determines which combinations can form a valid triangle using the triangle inequality rule:
For any three numbers a, b, c, they form a triangle if:
\[ a + b > c,\quad a + c > b,\quad b + c > a \]


Python Code:


from itertools import combinations

def is_valid_triangle(a, b, c):
    return a + b > c and a + c > b and b + c > a

def find_valid_sets(numbers):
    valid = []
    for combo in combinations(numbers, 3):
        if is_valid_triangle(*combo):
            valid.append(combo)
    return valid

# Run the program
nums = list(map(int, input("Enter numbers separated by space: ").split()))
valid_sets = find_valid_sets(nums)

print(f"Number of valid sets: {len(valid_sets)}")
for s in valid_sets:
    print(f"Valid set: {s}")

Sample Output:


Enter numbers separated by space: 3 4 5 6 10

Number of valid sets: 4  
Valid set: (3, 4, 5)  
Valid set: (3, 5, 6)  
Valid set: (4, 5, 6)  
Valid set: (5, 6, 10)

Step-by-Step Explanation:

- All 3-number combinations are generated using itertools.combinations
- Each combination is checked against the triangle inequality condition
- Valid sets are collected and displayed


Written & researched by Dr. Shahin Siami