Python How to Define and Work with Digits in Python?

  • Thread starter Thread starter Avatrin
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion focuses on how to define and identify specific types of numbers in Python, such as pandigital numbers, palindromes, and circular primes, based on their individual digits. Key points include the definitions of these numbers: a pandigital number uses all digits from 1 to n exactly once, while a palindromic number reads the same forwards and backwards. Participants emphasize the importance of understanding these definitions before attempting to write code. Suggestions include using algorithms to check for palindromes by comparing digits and testing for pandigital numbers by checking the presence of digits in a range. The use of string conversion is recommended for easier manipulation of individual digits, allowing for more straightforward checks. The conversation highlights the need for clarity in problem-solving and coding approaches when dealing with digit-based problems in programming challenges.
Avatrin
Messages
242
Reaction score
6

Homework Statement


This is not about one problem, it is rather about a type of problem:

How do I define, in Python, a number which is defined by its individual digits? For instance:
Pandigital numbers..
Palindromes..
Circular primes..

What commands do I need to work with to find these kinds of numbers?

Homework Equations


An n-digit number is pandigital if it makes use of all the digits 1 to n exactly once.

A palindromic number reads the same both ways.

The Attempt at a Solution


I don't know what commands to use.

def palindrome(n):
if n is ??:
return True
else:
return False
 
Technology news on Phys.org
Avatrin said:

Homework Statement


This is not about one problem, it is rather about a type of problem:

How do I define, in Python, a number which is defined by its individual digits? For instance:
Pandigital numbers..
Palindromes..
Circular primes..

What commands do I need to work with to find these kinds of numbers?

Homework Equations


An n-digit number is pandigital if it makes use of all the digits 1 to n exactly once.

A palindromic number reads the same both ways.

The Attempt at a Solution


I don't know what commands to use.

def palindrome(n):
if n is ??:
return True
else:
return False

There are no "commands" to use. You need to write code that will test a number to do these things. Before you start writing code, you need to start with the definitions of these terms, and see what you can put together as an algorithm. For example, what does it mean that a "palindromic number reads the same both ways"? If I told you that I was thinking of a 3-digit number, how would you determine whether it was palindromic, without me telling you the number?
 
Hey Avatrin and welcome to the forums.

Following on from Mark44's advice, can you define for us the things which are trying to check or convert to?

Even if its not in code, doing this will help you actually get to that stage (and you need to do this before you can even write the code anyway). Just try and write it in english and then make it more formal so we can see what's going on in your head.
 
Well, a 3-digit palindrome has the form xyx (x can be equal to y). For example:
909, 414 and 333

So, if I am testing a 3-digit palindrome:
if first digit = third digit:
Return True

If I am testing a 4-digit palindrome:
if first digit = fourth digit and second digit = third digit:
Return True

The reason I asked for commands is because I don't know how to isolate individual digits. I don't even know if the pseudo-code above is how the actual code will look like.

Testing pandigital numbers:
length(number) = n
for i in range(n):
|| if i+1 is not in number:
|||| return False
|||| break
else:
|| return True
 
Whats the data type of the numbers? Integer? Long? How many bits allocated?
 
You can peel off the 1's digit by using the modulus operator, &, working in modulo 10.

For example, 931 & 10 == 1

You can shift all of the digits to the right by doing integer division by 10.

For example, 931 / 10 == 93
 
chiro said:
Whats the data type of the numbers? Integer? Long? How many bits allocated?

Well, I am trying to solve various problems on Project Euler that involve digits in numbers (problems 4, 8, 16, 20 etc). So, yes, I am talking about integers. The largest integer we are talking about has 1000 digits (problem 8).

Mark44 said:
You can peel off the 1's digit by using the modulus operator, &, working in modulo 10.

For example, 931 & 10 == 1
I think you mean 931%10..
 
Thanks - yes I meant % but somehow typed &.
 
Consider converting your integer to a string:
s = str(x)

Then you can deal with the characters in the string-- i.e., digits.
 
  • #10
awkward said:
Consider converting your integer to a string:
s = str(x)

Then you can deal with the characters in the string-- i.e., digits.

Now that's a excellent suggestion.
 
  • #11
awkward said:
Consider converting your integer to a string:
s = str(x)

Then you can deal with the characters in the string-- i.e., digits.
Thanks! That's exactly what I was looking for...
 

Similar threads

Back
Top