5 Best Ways to Check if Four Integers Make a Rectangle in Python

Rate this post

πŸ’‘ Problem Formulation: The task is to verify whether four given integers can represent the sides of a rectangle. In Python, this involves checking whether two pairs of these integers are equal, which would signify parallel sides of equal length – a characteristic of rectangles. For example, if the input is (10, 20, 10, 20), the desired output would be True, indicating that these sides can form a rectangle.

Method 1: Using Basic Conditional Statements

This straightforward method relies on comparing the sides two by two using conditional statements. If there are two unique values and each occurs exactly twice, the integers can be considered as representing the sides of a rectangle.

Here’s an example:

def is_rectangle(a, b, c, d):
    return (a == c and b == d) or (a == b and c == d) or (a == d and b == c)

# Example usage:
result = is_rectangle(10, 20, 10, 20)
print(result)

Output:

True

This code snippet defines a function is_rectangle() that compares pairs of sides and returns True if there are two pairs of equal sides. In the example, sides a and c as well as b and d are the same, indicating the possibility of forming a rectangle.

Method 2: Using a Set

With this approach, we utilize a Python set to identify unique side lengths. Since a rectangle has exactly two unique side lengths, a set created from the sides should have a size of two.

Here’s an example:

def is_rectangle(a, b, c, d):
    return len(set((a, b, c, d))) == 2

# Example usage:
result = is_rectangle(15, 15, 30, 30)
print(result)

Output:

True

In the function is_rectangle(), we convert the input into a set to filter out unique side lengths. The function then checks if the size of the set is exactly two, which is the case for a rectangle.

Method 3: Sorting the Sides

By sorting the integers, this method organizes the sides so that it’s easier to compare adjacent elements which, for a rectangle’s sides, should be equal.

Here’s an example:

def is_rectangle(a, b, c, d):
    sides = sorted([a, b, c, d])
    return sides[0] == sides[1] and sides[2] == sides[3]

# Example usage:
result = is_rectangle(25, 25, 40, 40)
print(result)

Output:

True

The function is_rectangle() sorts the side lengths and then directly compares the first pair and the second pair of the sorted list. The sorted list will facilitate the comparison of sides for equality.

Method 4: Using Collections Counter

The Counter class from the collections module counts the frequency of each integer. For sides to form a rectangle, there should be exactly two different numbers, each appearing twice.

Here’s an example:

from collections import Counter

def is_rectangle(a, b, c, d):
    return Counter([a, b, c, d]).most_common(1)[0][1] == 2

# Example usage:
result = is_rectangle(35, 35, 50, 50)
print(result)

Output:

True

The function is_rectangle() here leverages Counter to determine the most common occurrence’s frequency. Since we’re looking for a rectangle, the most common side length should have a frequency of two.

Bonus One-Liner Method 5: Using an All-Encompassing Lambda

A succinct and clever use of Python’s lambda function to encapsulate the check into a single, albeit dense, line of code.

Here’s an example:

is_rectangle = lambda a, b, c, d: len({a, b, c, d}) == 2

# Example usage:
result = is_rectangle(45, 45, 60, 60)
print(result)

Output:

True

This lambda function is an anonymous function assigned to is_rectangle that takes four sides and returns True if there are only two unique sides (indicative of a rectangle) when they are placed in a set.

Summary/Discussion

  • Method 1: Basic Conditional Statements. This method is simple and easy to understand. However, multiple conditions make it slightly less elegant than other methods.
  • Method 2: Using a Set. More Pythonic and concise, it leverages inherent set properties for uniqueness but offers slightly less readability for beginners.
  • Method 3: Sorting the Sides. Has a clear logic flow and uses a common approach in programming. On the downside, sorting adds unnecessary computational complexity.
  • Method 4: Using Collections Counter. This is a powerful method for frequency counting, which fits perfectly for this use case. However, it does depend on an external library.
  • Method 5: One-liner Lambda. It’s compact and Pythonic, showcasing the language’s capabilities for succinct expressions. May be less readable for those unfamiliar with lambdas or sets.