Hướng dẫn python circular bit shift

Your attempted solution does not work because Python has unlimited size integers.

It works in C [for specific values of N, depending on the type used, typically something like 8 or 32], because the bits that are shifted out to the left are automatically truncated.

You need to do this explicitly in Python to get the same behaviour. Truncating a value to the lowest N bits can be done be using % [1 >> ISHFTC[57, 1, 6] 51 >>> bin[_] '0b110011'

I'm a beginner coder and I'm trying to find cyclic shifts of strings. A cyclic shift of a string is obtained by moving characters from the beginning of the string to the end of the string. For example, the cyclic shifts of ABCDE are: ABCDE, BCDEA, CDEAB, DEABC, EABCD. The input will consist of exactly two lines containing only uppercase letters. The first line will be the text T, and the second line will be the string S.

Output yes if the text, T, contains a cyclic shift of the string, S. Otherwise, output no.

Sample Input:

ABCCDEABAA
ABCDE

Output for Sample Input:

yes

Tom Karzes

20.4k2 gold badges17 silver badges37 bronze badges

3

So basically you can append the string S to itself and lets call this string S2

s = 'ABCDE'
s2 = s + s    # s2 = 'ABCDEABCDE'

Since the length of the string S is 5, you will have to check whether s2[i:i+5] exists in T or not.

Code

t = 'ABCCDEABAA'
s = 'ABCDE'
s2 = s + s
n = len[s]

for i in range[n]:
    flag = False
    if s2[i:i+n] in t:
        flag = True

    print[f"Checking for {s2[i:i+n]:s} in T = {flag}"]

Output

Checking for ABCDE in T = False
Checking for BCDEA in T = False
Checking for CDEAB in T = True
Checking for DEABC in T = False
Checking for EABCD in T = False

Time complexity : O[len[s]] * O[string_comparison]

answered Mar 19 at 8:31

DollarAkshayDollarAkshay

2,0741 gold badge19 silver badges38 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a string of size n, write functions to perform following operations on string.

    1. Left [Or anticlockwise] rotate the given string by d elements [where d

    Chủ Đề