Python 脚本将其输入中的位向右旋转两个位置
Python script to rotates the bits in its input two places to the right
编写期望位字符串作为输入的移位脚本。脚本 shift Right 旋转它的位
向右输入两位
示例数据:“0111110”、“01110”、“00011111”
班次:“1001111”、“10011”、“11000111”
我试图通过转换为 b=str(input) 来访问和修改它的前两位数和后两位数
然后找到 b[0]、b[1]、b[-1]、b[-2],但没有用。
请帮忙,非常感谢
我想这应该有帮助
b = str(input())
# shift : how many bits you want to rotate
shift = 2
if shift == len(b):
print(b)
else:
print(b[-shift:] + b[:-shift])
def shift_string(s):
return s[-1] + s[:-1]
s = "01110"
for x in range(5):
s = shift_string(s)
print(x, s)
打印出来
0 00111
1 10011
2 11001
3 11100
4 01110
所以要移动 s
两次,
s = shift_string(shift_string(s))
编写期望位字符串作为输入的移位脚本。脚本 shift Right 旋转它的位 向右输入两位
示例数据:“0111110”、“01110”、“00011111”
班次:“1001111”、“10011”、“11000111”
我试图通过转换为 b=str(input) 来访问和修改它的前两位数和后两位数 然后找到 b[0]、b[1]、b[-1]、b[-2],但没有用。 请帮忙,非常感谢
我想这应该有帮助
b = str(input())
# shift : how many bits you want to rotate
shift = 2
if shift == len(b):
print(b)
else:
print(b[-shift:] + b[:-shift])
def shift_string(s):
return s[-1] + s[:-1]
s = "01110"
for x in range(5):
s = shift_string(s)
print(x, s)
打印出来
0 00111
1 10011
2 11001
3 11100
4 01110
所以要移动 s
两次,
s = shift_string(shift_string(s))