Python: Test whether a given integer is pandigital number or not
Python Basic - 1: Exercise-106 with Solution
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:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/basic/python-basic-1-exercise-106.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics