Hướng dẫn circular shift in python

Let’s say that we have a Python List (or 1-D numpy array) and we want to shift it by n positions. Let’s see some ways of how we can do it.

Using pop

Assume that the list x is [1,2,3,4,5] let’s see how we can shift it by one position ahead.

x = [1,2,3,4,5] x.append(x.pop(0)) x [2, 3, 4, 5, 1]

Using Collection deque

This method allows us to shift by n elements ahead at once, using both directions, forward and backward. We just need to use the rotate method on the deque object. Note, that you can easily convert a deque object to a list like list(x) where x is a deque object.

import collections x = collections.deque([1, 2, 3, 4, 5]) x.rotate(1) x deque([5, 1, 2, 3, 4])

Note, that we also rotate it backwards by 2 steps:

x = collections.deque([1, 2, 3, 4, 5]) x.rotate(-2) x deque([3, 4, 5, 1, 2])

Using numpy roll

Finally, we can use the roll method to the numpy arrays. It also supports both directions and n steps. For example:

import numpy x=numpy.arange(1,6) numpy.roll(x,1) array([5, 1, 2, 3, 4])

Or, if we want to get 2 steps backwards:

x=numpy.arange(1,6) numpy.roll(x,-2) array([3, 4, 5, 1, 2])

Last update on August 19 2022 21:50:46 (UTC/GMT +8 hours)

Python Programming Puzzles: Exercise-65 with Solution

Write a Python program to shift the decimal digits n places to the left, wrapping the extra digits around. If shift > the number of digits of n, reverse the string.

Input: n = 12345 and shift = 1 Output: Result = 23451 Input: n = 12345 and shift = 2 Output: Result = 34512 Input: n = 12345 and shift = 3 Output: Result = 45123 Input: n = 12345 and shift = 5 Output: Result = 12345 Input: n = 12345 and shift = 6 Output: Result = 54321

Pictorial Presentation:



Sample Solution-1:

Python Code:

#License: //bit.ly/3oLErEI def test(n, shift): s = str(n) if shift > len(s): return s[::-1] return s[shift:] + s[:shift] print("Shift the decimal digits n places to the left. If shift > the number of digits of n, reverse the string.:") n = 12345 shift = 1 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift)) n = 12345 shift = 2 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift)) n = 12345 shift = 3 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift)) n = 12345 shift = 5 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift)) n = 12345 shift = 6 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift))

Sample Output:

Shift the decimal digits n places to the left. If shift > the number of digits of n, reverse the string.: n = 12345 and shift = 1 Result = 23451 n = 12345 and shift = 2 Result = 34512 n = 12345 and shift = 3 Result = 45123 n = 12345 and shift = 5 Result = 12345 n = 12345 and shift = 6 Result = 54321

Flowchart:


Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

#License: //bit.ly/3oLErEI def test(n, shift): shifted_digits = [int(x) for x in str(n)] for i in range(shift): shifted_digits.append(shifted_digits.pop(0)) if shift > len(shifted_digits): return str(n)[::-1] else: return ''.join(str(x) for x in shifted_digits) print("Shift the decimal digits n places to the left. If shift > the number of digits of n, reverse the string.:") n = 12345 shift = 1 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift)) n = 12345 shift = 2 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift)) n = 12345 shift = 3 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift)) n = 12345 shift = 5 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift)) n = 12345 shift = 6 print("\nn =",n," and shift =",shift) print("Result = ",test(n, shift))

Sample Output:

Shift the decimal digits n places to the left. If shift > the number of digits of n, reverse the string.: n = 12345 and shift = 1 Result = 23451 n = 12345 and shift = 2 Result = 34512 n = 12345 and shift = 3 Result = 45123 n = 12345 and shift = 5 Result = 12345 n = 12345 and shift = 6 Result = 54321

Flowchart:


Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Find the string consisting of all the words whose lengths are prime numbers.
Next: Find the indices of the closest pair from given a list of numbers.

Chủ đề