3 回答
TA贡献1813条经验 获得超2个赞
转换成字符串?
A = 130
def shuffle(A):
A = str(A)
if len(A) <= 2:
return int(A)
return int((A[0] + A[-1]) + str(shuffle(A[1:-1])))
TA贡献1829条经验 获得超9个赞
不转换为字符串:
def shuffle(x):
if x < 100:
return x
t = x
l = 0
while t > 0:
t //= 10
l += 1
a = x // 10 ** (l-1) * 10 ** (l-1)
b = (x % 10) * 10 ** (l-2)
return a + b + shuffle((x - a) // 10)
测试工作:
>>> shuffle(123456)
162534
>>> shuffle(310)
301
TA贡献1796条经验 获得超4个赞
string使用slices正负索引进行迭代和使用
def shuffle(phrase):
even = 0
odd = -1
result = ''
phrase = str(phrase)
for n in range(len(phrase)):
if int(n/2) == n/2:
result = result + phrase[even]
even += 1
else:
result = result + phrase[odd]
odd += -1
return result
添加回答
举报