Reputation: 1066
Write shift script that expect a bit string as an input. The script shift Right rotates the bits in its input two places to the right.
sample data: ‘0111110’, '01110', '00011111'
Shift : ‘1001111’, '10011', '11000111'
I tried like to access & modify it's first two digit & last two digit by converting into b=str(input) then finding b[0], b[1], b[-1],b[-2], but it didn't work. Please help, thanks a lot
Upvotes: 1
Views: 1278
Reputation: 1700
This should help I guess
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])
Upvotes: 2
Reputation: 169184
def shift_string(s):
return s[-1] + s[:-1]
s = "01110"
for x in range(5):
s = shift_string(s)
print(x, s)
prints out
0 00111
1 10011
2 11001
3 11100
4 01110
so to shift s
twice,
s = shift_string(shift_string(s))
Upvotes: 2