N
N
numb72016-02-07 01:05:44
Python
numb7, 2016-02-07 01:05:44

How to perform a cyclic shift by n steps?

Hey!
unable to rotate n steps
Example:

s = 2
a = 'hello'
print (a[s:] + a[:-s])

The answer should be like this: lohel Tell me
, please, where is the mistake?
Found the answer:
print (a[-s::]+a[:-s])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sly_tom_cat ., 2016-02-07
@numb7

print (a[len(a)-s:] + a[:-s]) or
just
print (a[-s:] + a[:-s])
the element that will be the first, and the final position - the element that will be FOR the last is indicated.
What would work with any step is useful offset normalize modulo length:
s %= len(a)
print (a[-s:] + a[:-s])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question