1

This code takes a float as input, for instance a number like 1.01, then it multiplies it by a constant.

Subsequently it seeks out the closest numerical palindrome to that number.

However

At this point it only searches in one direction, it only increments the number-- what I'm trying to do is to get it to search in the positive direction and the negative direction simultaneously ((or at least in quick succession).

Checkout the way it works now, maybe a bit naive:

import sys

# This method determines whether or not the number is a Palindrome
def isPalindrome(x):
    x = str(x).replace('.','')
    a, z = 0, len(x) - 1
    while a < z:
        if x[a] != x[z]:
            return False
        a += 1
        z -= 1
    return True

if '__main__' == __name__:

    trial = float(sys.argv[1])

    candidrome = trial + (trial * 0.15)

    print(candidrome)
    candidrome = round(candidrome, 2)


    # check whether we have a Palindrome
    while not isPalindrome(candidrome):
        candidrome += 0.01
        candidrome = round(candidrome, 2)
        print(candidrome)

    if isPalindrome(candidrome):
        print( "It's a Palindrome! " + str(candidrome) )

I've been attempting to insert these functions somewhere into my loop:

def incrementer(candidrome):
    candidrome += 0.01
    candidrome = round(candidrome, 2)
    return candidrome

def decrementer(candidrome):
    candidrome -= 0.01
    candidrome = round(candidrome, 2)
    return candidrome

But I've not been able to pin it down yet. How to exhaustively search the positive and negative space for the closest (in numberline distance) numerical palindrome?

halfer
  • 18,701
  • 13
  • 79
  • 158
smatthewenglish
  • 2,595
  • 3
  • 29
  • 64
  • The code is the same [as in your prior question](https://stackoverflow.com/questions/46374858/restrict-number-of-decimal-places-to-two-in-python-float-operations). Is this a duplicate? It seems that they are essentially trying to solve the same problem. – halfer Oct 05 '17 at 18:41

0 Answers0