def __init__
def __init__

Reputation: 1066

Python script to rotates the bits in its input two places to the right

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

Answers (2)

Sai
Sai

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

AKX
AKX

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

Related Questions