在Python中搜索第二个小数点浮点回文操作的正空格和负空格
此代码将浮点作为输入,例如像在Python中搜索第二个小数点浮点回文操作的正空格和负空格,python,floating-point,palindrome,Python,Floating Point,Palindrome,此代码将浮点作为输入,例如像1.01这样的数字,然后将其乘以常数 随后,它寻找与该数字最接近的数字回文 但是 在这一点上,它只在一个方向上搜索,它只会增加数字——我要做的是让它同时在正方向和负方向上搜索(或者至少快速地连续搜索) 按现在的工作方式结账,可能有点幼稚: import sys # This method determines whether or not the number is a Palindrome def isPalindrome(x): x = str(x).r
1.01
这样的数字,然后将其乘以常数
随后,它寻找与该数字最接近的数字回文
但是
在这一点上,它只在一个方向上搜索,它只会增加数字——我要做的是让它同时在正方向和负方向上搜索(或者至少快速地连续搜索)
按现在的工作方式结账,可能有点幼稚:
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) )
但是我还不能确定。如何穷尽性地搜索最接近的正负空间(在数字线距离内)数字回文?代码是相同的。这是重复的吗?看起来他们基本上是在试图解决相同的问题。代码是相同的。这是重复的吗?看起来他们基本上是在试图解决相同的问题。
def incrementer(candidrome):
candidrome += 0.01
candidrome = round(candidrome, 2)
return candidrome
def decrementer(candidrome):
candidrome -= 0.01
candidrome = round(candidrome, 2)
return candidrome