Hướng dẫn circular shift python

Last update on August 19 2022 21:51:39 [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.

Nội dung chính

  • Python Programming Puzzles: Exercise-65 with Solution
  • Visualize Python code execution:
  • Visualize Python code execution:
  • Python: Tips of the Day
  • What is circular right shift?
  • How do you shift a circle to the left in Python?
  • How do you shift a right element in Python?
  • What is circular shift give an example?

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.

Python: Tips of the Day

Combining Lists Using Zip:

  • Takes multiple collections and returns a new collection.
  • The new collection contains items where each item contains one element from each input collection.
  • It allows us to transverse multiple collections at the same time.
name = 'abcdef'
suffix = [1,2,3,4,5,6]
zip[name, suffix]
--> returns [a,1],[b,2],[c,3],[d,4],[e,5],[f,6]

What is circular right shift?

The circular shift can be of two types: Left circular shift [moving the final bit to the first position while shifting all other bits to the next position]. Right circular shift [moving the first bit to the last position while shifting all other bits to the previous position].

How do you shift a circle to the left in Python?

Given a list of N numbers, write a function to shift the numbers circularly by some integer k [where k < N]. The function should take the list and k as arguments and return the shifted list. a] Write a function that assumes the shifting is to the left. It should not print anything.

How do you shift a right element in Python?

How to shift elements in a list in Python.

a_list = collections. deque[[1, 2, 3, 4, 5]].

a_list. rotate[2] Shift `a_list` 2 places to the right..

shifted_list = list[a_list].

print[shifted_list].

What is circular shift give an example?

Chủ Đề