How to Define and Work with Digits in Python?

  • Context: Python 
  • Thread starter Thread starter Avatrin
  • Start date Start date
  • Tags Tags
    Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
10 replies · 4K views
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
 
Physics 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
 
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..
 
Consider converting your integer to a string:
s = str(x)

Then you can deal with the characters in the string-- i.e., digits.
 
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.
 
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...