编写需要一个位字符串作为输入的移位脚本。脚本右移在右边输入两个位置。
样本数据:"0111110"、"01110"、"00011111">
班次:"1001111"、"10011"、"11000111">
我试着访问&修改它的前两位数字&转换为b=str的最后两位数字(输入(然后找到b[0],b[1],b[-1],b[-2],但没有成功。请帮忙,非常感谢
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))
这应该有助于我猜测
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])