如何在不使用 Python3 将整数转换为字符串的情况下找到整数的中间值?
How to find the middle value of an integer without converting it to a string using Python3?
在解决下一个 smallest palindrome problem 时,我必须将整数转换为字符串,以便反转并找到中间值。必须有一个解决方案 - 如何在不使用 Python3 将其转换为字符串的情况下找到整数的中间值?感谢您的协助。谢谢。
示例:
- 输入=78653,结果=6
- 输入=7564,结果=5
这是一个开始。
它需要一些调整:
import math
def mid_digit(n):
# num of digits
a = math.trunc(math.log(n,10)+1)
# moving half of digits to the right of decimal point
b = n / 10 ** round(a/2 + 0.5)
# getting the left most decimal digit
c = math.trunc(math.modf(b)[0] * 10)
return c
在解决下一个 smallest palindrome problem 时,我必须将整数转换为字符串,以便反转并找到中间值。必须有一个解决方案 - 如何在不使用 Python3 将其转换为字符串的情况下找到整数的中间值?感谢您的协助。谢谢。
示例:
- 输入=78653,结果=6
- 输入=7564,结果=5
这是一个开始。
它需要一些调整:
import math
def mid_digit(n):
# num of digits
a = math.trunc(math.log(n,10)+1)
# moving half of digits to the right of decimal point
b = n / 10 ** round(a/2 + 0.5)
# getting the left most decimal digit
c = math.trunc(math.modf(b)[0] * 10)
return c