w3resource

Python: Test whether a given integer is pandigital number or not


Pandigital Number Checker

From Wikipedia,
In mathematics, a Pandigital number is an integer that in a given base has among its significant digits each digit used in the base at least once.
For example, 1223334444555556666667777777888888889999999990 is a Pandigital number in base 10. The first few Pandigital base 10 numbers are given by 1023456789, 1023456798, 1023456879, 1023456897, 1023456978, 1023456987, 1023457689.

Write a Python program to test whether a given integer is a Pandigital number or not.

Sample Solution:

Python Code:

# Define a function named is_pandigital_num that checks if a number (n) is a Pandigital number.
def is_pandigital_num(n):
    # Convert the number to a string, create a set from its digits, and check if the set has exactly 10 unique digits.
    return len(set(str(n))) == 10

# Test the function with different numbers and print the results.

# Test case 1
n = 1023456897
print("Original number:", n)
print("Check the said number is Pandigital number or not?")
print(is_pandigital_num(n))

# Test case 2
n = 1023456798
print("\nOriginal number:", n)
print("Check the said number is Pandigital number or not?")
print(is_pandigital_num(n))

# Test case 3
n = 1023457689
print("\nOriginal number:", n)
print("Check the said number is Pandigital number or not?")
print(is_pandigital_num(n))

# Test case 4
n = 1023456789
print("\nOriginal number:", n)
print("Check the said number is Pandigital number or not?")
print(is_pandigital_num(n))

# Test case 5
n = 102345679
print("\nOriginal number:", n)
print("Check the said number is Pandigital number or not?")
print(is_pandigital_num(n))

Sample Output:

Original number: 1023456897
Check the said number is Pandigital number or not?
True
Original number: 1023456798
Check the said number is Pandigital number or not?
True
Original number: 1023457689
Check the said number is Pandigital number or not?
True
Original number: 1023456789
Check the said number is Pandigital number or not?
True
Original number: 102345679
Check the said number is Pandigital number or not?
False

Explanation:

Here is a breakdown of the above Python code:

  • Function definition:
    • The code defines a function named "is_pandigital_num()" that checks if a given number (n) is a Pandigital number.
  • Conversion to String and Set Creation:
    • The function converts the number to a string, creates a set from its digits, and checks if the set has exactly 10 unique digits.

Flowchart:

Flowchart: Python - Test whether a given integer is pandigital number or not.

For more Practice: Solve these Related Problems:

  • Write a Python program to check if a number is pandigital in base 10 by verifying it contains every digit from 0 to 9 at least once.
  • Write a Python program to determine if a given integer is pandigital by comparing its sorted digit string to a reference string.
  • Write a Python program to validate pandigital status by converting the number to a set and comparing with the set of all digits.
  • Write a Python program to test if a number is pandigital by checking the length of its unique digit set against 10.

Go to:


Previous: Write a Python program to check whether a given sequence is linear, quadratic or cubic.
Next: Write a Python program to check whether a given number is Oddish or Evenish.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.