修改字符串的一个问题
比如说我现在的数字是1234,每个数字向左平移一位,变成4123,3412,2341应该怎么实现? 固定数字长度吗昨非 发表于 2021-5-1 16:24
固定数字长度吗
对,他的数字是给定的 >>> f(2134, 3412, 'L')
2134 plus 3412 equals 5546
1342 plus 4123 equals 5465
3421 plus 1234 equals 4655
4213 plus 2341 equals 6554
>>> f(2134, 3412, 'R')
2134 plus 3412 equals 5546
4213 plus 2341 equals 6554
3421 plus 1234 equals 4655
1342 plus 4123 equals 5465
题目大概是这个样子 转成字符串,还不是想咋玩就咋玩 wp231957 发表于 2021-5-1 16:27
转成字符串,还不是想咋玩就咋玩
教一下好兄弟,在考试有点头晕 是要求写个f函数然后吗
不是字符串反转吗?怎么还是求和啊 昨非 发表于 2021-5-1 16:42
是要求写个f函数然后吗
不是字符串反转吗?怎么还是求和啊
函数已经给了,我发的是测试结果,然后我现在就是不会去字符串平移 # On each line of the form "x plus y equals z", x, y and z are
# possibly padded with 0s on the left; in particular, the number of
# digits in both x and y is the maximum of the number of digits in
# the first input, m, and the number of digits in the second input, n.
#
# The third argument, direction, is one of 'L' or 'R',
# for left or right rotation.
def f(m, n, direction):
'''
>>> f(0, 0, 'L')
0 plus 0 equals 0
>>> f(0, 0, 'R')
0 plus 0 equals 0
>>> f(1234, 0, 'L')
1234 plus 0000 equals 1234
2341 plus 0000 equals 2341
3412 plus 0000 equals 3412
4123 plus 0000 equals 4123
>>> f(1234, 0, 'R')
1234 plus 0000 equals 1234
4123 plus 0000 equals 4123
3412 plus 0000 equals 3412
2341 plus 0000 equals 2341
>>> f(2134, 3412, 'L')
2134 plus 3412 equals 5546
1342 plus 4123 equals 5465
3421 plus 1234 equals 4655
4213 plus 2341 equals 6554
>>> f(2134, 3412, 'R')
2134 plus 3412 equals 5546
4213 plus 2341 equals 6554
3421 plus 1234 equals 4655
1342 plus 4123 equals 5465
>>> f(213287, 3166, 'L')
213287 plus 003166 equals 0216453
132872 plus 031660 equals 0164532
328721 plus 316600 equals 0645321
287213 plus 166003 equals 0453216
872132 plus 660031 equals 1532163
721328 plus 600316 equals 1321644
>>> f(8901, 3419306, 'R')
0008901 plus 3419306 equals 03428207
1000890 plus 6341930 equals 07342820
0100089 plus 0634193 equals 00734282
9010008 plus 3063419 equals 12073427
8901000 plus 9306341 equals 18207341
0890100 plus 1930634 equals 02820734
0089010 plus 4193063 equals 04282073
>>> f(800095, 900003, 'L')
800095 plus 900003 equals 1700098
000958 plus 000039 equals 0000997
009580 plus 000390 equals 0009970
095800 plus 003900 equals 0099700
958000 plus 039000 equals 0997000
580009 plus 390000 equals 0970009
'''
# INSERT YOUR CODE HERE
if __name__ == '__main__':
import doctest
doctest.testmod()
{:10_277:} 本帖最后由 柿子饼同学 于 2021-5-1 17:07 编辑
柿子饼同学 发表于 2021-5-1 16:48
def a(s):
s = list(str(s))
for i in range(len(s)):
s.insert(0, s.pop())
print(''.join(s))
试试,{:10_256:}
这是所有的位移方式
但你发的好像是加法运算的函数我就有点蒙 本帖最后由 qq1151985918 于 2021-5-1 20:55 编辑
def f(m: int, n: int, direction: "str in ['L','R']"):
if direction not in ["L", "l", "R", "r"]:
raise Exception
length = len(str(m)) if len(str(m)) > len(str(n)) else len(str(n))
str_m, str_n = str(m).zfill(length), str(n).zfill(length)
if != :
raise TypeError
list_m, list_n, list_sum = , ,
data = range(1, length) if direction in ["L", "l"] else range(length - 1, 0, -1)
for i in data:
new_m = str_m + str_m[:i]
new_n = str_n + str_n[:i]
list_m.append(new_m)
list_n.append(new_n)
list_sum.append(int(new_m) + int(new_n))
length_max_sum = len(str(max(list_sum)))
for m, n, s in zip(list_m, list_n, list_sum):
print(m, "plus", n, "equals", str(s).zfill(length_max_sum))
f(800095, 900003, 'L')
f(8901, 3419306, 'R')
def array(num):
return eval(str(num)[-1]+str(num)[:(len(str(num))-1)])
num = 123456789
for i in range(len(str(num))):
num = array(num)
print(num)
912345678
891234567
789123456
678912345
567891234
456789123
345678912
234567891
123456789
页:
[1]