5 Best Ways to Check If Number Can Be Displayed Using Seven-Segment LED in Python

Rate this post

πŸ’‘ Problem Formulation: Consider a scenario where you want to determine if a given number can be displayed using a typical seven-segment LED display. These displays represent numbers and some letters by illuminating certain segments. For instance, the input 5 should yield the output True, as it can be displayed, while the input 10 results in False as it has more digits than a single seven-segment display can represent.

Method 1: Using a Set of Valid Characters

This method involves creating a set of characters that represent the valid digits (0-9) and checking if the input number, once converted to a string, has all its characters in this set. It’s an effective way to filter out any input that cannot be represented on a seven-segment display owing to invalid digits.

Here’s an example:

def can_display(number):
    valid_digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
    return set(str(number)).issubset(valid_digits)

print(can_display(5))  # Example input

Output: True

This simple function turns the number into a string and transforms the string into a set of characters. By using the set method issubset, it verifies if each digit in the input number can be found in the set of valid digits for a seven-segment display.

Method 2: Regular Expression Matching

Regular expressions provide a powerful way to match patterns within text. By using a regular expression that matches any sequence of valid digits for a seven-segment display, this method can immediately verify if an input number is compliant.

Here’s an example:

import re

def can_display(number):
    return bool(re.match(r'^[0-9]+$', str(number)))

print(can_display(123))  # Example input

Output: True

The function casts the number to a string and then applies a regular expression match operation. The expression ^[0-9]+$ checks if the entire string consists solely of digits, which aligns with what a seven-segment display can represent.

Method 3: Testing Segment Representation

To ensure the number can be displayed, this method explicitly checks each digit against a predefined dictionary mapping digits to their corresponding seven-segment representation. It’s a bit overkill for simple digit checking, but demonstrates how each digit could be validated if, for instance, certain segments were inoperable.

Here’s an example:

def can_display(number):
    segment_map = {
        '0': 0b0111111,
        '1': 0b0000110,
        # ... includes all digit mappings
        '9': 0b0110111
    return all(char in segment_map for char in str(number))

print(can_display(789))  # Example input

Output: True

In this example, each digit has a corresponding binary representation aligned with segments of a seven-segment display. The all() function checks that every character in the input number string can be found in the segment_map dictionary, thus ensuring the number can indeed be displayed.

Method 4: Checking Length and Validity of Digits

This straightforward approach verifies the length of the input when converted to a string and checks whether all characters are digits, leveraging Python’s built-in isdigit() method which simplifies the process.

Here’s an example:

def can_display(number):
    num_str = str(number)
    return len(num_str) == 1 and num_str.isdigit()

print(can_display(8))  # Example input

Output: True

The function checks two conditions: whether the string representing the number is of length 1 (indicative of a single digit which is what a seven-segment display can handle) and if all characters in the string are digits using the isdigit() method.

Bonus One-Liner Method 5: Lambda Function

A concise and elegant one-liner method can be achieved by using a lambda function combined with a logical check to validate that the input converted to string consists of a single digit.

Here’s an example:

can_display = lambda number: str(number).isdigit() and len(str(number)) == 1

print(can_display(7))  # Example input

Output: True

This lambda function embodies the essence of the previous explicit function. It has the advantage of brevity and directly expresses the condition that the number, when converted to a string, must both represent a digit and consist of only one character.


  • Method 1: Set of Valid Characters. Simple and works well for single digits. Limited to basic integer numbers.
  • Method 2: Regular Expression Matching. Powerful for pattern checking and extensible to more complex input validation. Might be considered overkill for small tasks.
  • Method 3: Testing Segment Representation. Provides a direct mapping to LED segments; useful for intricate checks. Very specific and not as easy to grasp for beginners.
  • Method 4: Checking Length and Validity of Digits. Direct and uses built-in string methods for a clean solution. Limited to single-digit numbers.
  • Method 5: Lambda Function. Elegant and concise one-liner. It captures the simplicity of the problem but isn’t as explicit as a fully defined function.