-7

I am currently partway through a school project which requires me to create a python program that can read in two non-negative integers, begin and end, and print out all of the palindromes which occur between begin and end (inclusive). The code we have been given is this:

begin = int(input('Enter begin: '))
end = int(input('Enter end: '))

palindromes = 0
# Add your code here. You will want to start with a "for x in range" style loop.

print('There are', palindromes, 'palindrome(s) between', begin, 'and', end)

Question: How would I calculate how many palindromes are in the range of the two numbers entered (and which numbers are palindromes)?

Research: I have tried having a look at pages, this one was a good one though I (being new to python) could not make sense of it when I put it into code: how to check for a palindrome using python logic

Community
  • 1
  • 1
W1n5t0n
  • 1
  • 5
  • 1
    What's your question? – Cyphase Aug 11 '15 at 10:34
  • How would I calculate how many palindromes are in the range of the two numbers entered (and which numbers are palindromes)? – W1n5t0n Aug 11 '15 at 10:46
  • 2
    Could you explain what part of the answer you cited you do not understand? You need just one loop and the code from the accepted answer. – Matthias Aug 11 '15 at 11:01
  • 1
    `palindromes = len([i for i in range(begin, end+1) if str(i) == str(i)[::-1]])`. Now go and explain this to your teacher. – Matthias Aug 11 '15 at 11:07
  • That is more or less what I had. The problem I was having was that the program then needs to print which numbers are palindromes. – W1n5t0n Aug 11 '15 at 21:33

5 Answers5

0

Palindrome number are written in the same way from right to left and from left to right : Exemple :

123 is not a palindrome number because its inverted representation is 321
121 is a palindrome number because its inverted representation is 121

and the simpliest way to do this in python is to convert the number into a string and compare it to its inverted representation (Let's say that n is a number and we want to know if it's a palindrome or not) :

if str(n) == str(n)[::-1]:
    print "It's a palindrome number"

so the solution for your problem is an if brunch inside in iteration like below :

print "Enter begin:"
begin = int(raw_input("> "))
print "Enter end:"
end = int(raw_input("> "))

palindromes = 0

for x in range(begin, end):
    if str(x) == str(x)[::-1]:
        palindromes += 1

print "There are %d palindrome(s) between %d and %d" % (palindromes, begin, end)
Ahmed Abdelkafi
  • 467
  • 2
  • 8
0

Since this is school work I don't see a huge benefit in writing code for you, but here's how to break down the problem.

As the comment in your example says, you're going to want to start with a for x in range loop. In this case I suggest you use the version of range() that takes both a start point and a stop point (begin, and end) -- note that this will iterate over numbers between start and stop INCLUDING start but NOT stop.

Example -- prints numbers 1 to 9 each on their own line:

for x in range(1, 10):
   print(x)

Inside your for loop you can then test to see if x is a palindrome. If it is you'll want to add it to a list of found palindromes. Later on you can set your palindromes variable to be the length of this list, and print out the contents as needed.

To find out if x is a palindrome, the answer you've found should help. Alternatively you can think about what a palindrome is and have a go at writing your own method. You'll want to convert x to a string before you start, and then it's just a case of comparing the first and last halves of the string, and there are a few interesting ways of doing this :)

Here are two options you could try:

1) Use slicing to split the string in half (ignoring the middle character if there is an odd number of characters). Reverse ONE half of the string, and then compare it with the other half; if they are the same then it's a palindrome.

2) Use another for loop with a start of 0 and a stop of half the length (rounded down to the nearest whole number). Inside the loop take slices of both ends of the string and compare them.

Example (where x is the loop counter and currently has a value of 0).

string = 'abcd' string[0+x] 'a' string[-(1+x)] 'd'

For both of these answers you'll want to look at how to slice a string if you're not already familiar with this. There's some helpful examples in this Python introduction.

Community
  • 1
  • 1
Saff
  • 501
  • 3
  • 13
0

A palindrome is a number which when written reversed is same as the original number. For example, 1234321 is a palindrome.

So, to check this, all you have to do is check if the reverse of the number is the same as original.

Here's the code.

begin = int(input('Enter begin:'))
end = int(input('Enter end:'))

palindromes = 0

for i in range(begin, end+1, 1):
    if str(i) == str(i)[::-1]:  #Here I convert the int to string and check if t matches the reverse
       palindromes += 1
       print i
print "Total number of palindromes = ",palindromes
Abhyudit Jain
  • 3,640
  • 2
  • 24
  • 32
0

Thank you everyone, the code I found to be the answer was this:

begin = int(input('Enter begin: '))
end = int(input('Enter end: '))

palindromes = palindromes = len([i for i in range(begin, end+1) if str(i) ==     str(i)[::-1]])
  for i in range(begin, end+1):
    if str(i) == str(i)[::-1]:
      print(i,'is a palindrome')

print('There are', palindromes, 'palindrome(s) between', begin, 'and', end)
W1n5t0n
  • 1
  • 5
0
# Palindrome Number Calculator

Palindrome = False
Test = 0
newInt = 0

myInt = int(input("Enter a number "))
myIntReversed = int(str(myInt)[::-1])

if myInt == myIntReversed:
    print("Your number is a palindrome")
    Palindrome = True
    exit()

while Palindrome == False:

    myInt += myIntReversed

    Test += 1

    myIntReversed = int(str(myInt)[::-1])

    if myInt == myIntReversed:
        print("Palindrome")
        print(myInt)
        Palindrome = True

exit()

If you use pycharm then you can use the debug feature to see what test number the computer is at. I am not printing this number so the program runs faster. Try to see if your program can see what the palindrome of 196 is :)

Kelton
  • 1