Python – Reverse Shift characters by K
Last Updated :
26 Apr, 2023
Given a String, reverse shift each character according to its alphabetic position by K, including cyclic shift.
Input : test_str = ‘bccd’, K = 1
Output : abbc
Explanation : 1 alphabet before b is ‘a’ and so on.
Input : test_str = ‘bccd’, K = 2
Output : zaab
Explanation : 2 alphabets before b is ‘z’ (rounded) and so on.
Method : Using maketrans() + upper() + list comprehension + translate() + slicing
In this, we make translation table to each character to its K shifted version using maketrans() and slicing. The upper() is used to handle all the upper case characters, translate() is used to perform translation according to lookup translation table created by maketrans().
Python3
test_str = 'GeeksForGeeks'
print ( "The original string is : " + str (test_str))
K = 10
alpha_chars = 'abcdefghijklmnopqrstuvwxyz'
alpha_chars2 = alpha_chars.upper()
lower_trans = str .maketrans(alpha_chars, alpha_chars[ - K:] + alpha_chars[ : - K])
upper_trans = str .maketrans(alpha_chars2, alpha_chars2[ - K:] + alpha_chars2[ : - K])
lower_trans.update(upper_trans)
res = test_str.translate(lower_trans)
print ( "The converted String : " + str (res))
|
Output
The original string is : GeeksForGeeks
The converted String : WuuaiVehWuuai
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Using ASCII values and modulo arithmetic in python:
Approach: We can use the ASCII values of the characters to shift them by K positions. We can subtract K from the ASCII value of each character and get the corresponding character. If the resulting ASCII value is less than the ASCII value of ‘a’, we can add 26 to it to get the correct character.
- Initialize an empty string result to store the shifted characters.
- Loop through each character char in the input string test_str.
- Convert the character char to its ASCII value using the ord function.
- Subtract the shift value K from the ASCII value. This will give the ASCII value of the shifted character.
- If the resulting ASCII value is less than the ASCII value of the character ‘a’, add 26 to it. This is because we want to wrap around to the end of the alphabet if we have shifted past the beginning of the alphabet.
- Convert the shifted ASCII value back to a character using the chr function.
- Append the shifted character to the result string.
- Return the result string containing the shifted characters.
Python3
def reverse_shift_ascii(test_str, K):
result = ""
for char in test_str:
ascii_val = ord (char) - K
if ascii_val < ord ( 'a' ):
ascii_val + = 26
result + = chr (ascii_val)
return result
test_str_1 = 'bccd'
K_1 = 1
test_str_2 = 'bccd'
K_2 = 2
output_1 = 'abbc'
output_2 = 'zaab'
print ( "Input: test_str = '{}', K = {}" . format (test_str_1, K_1))
print ( "Output:" , reverse_shift_ascii(test_str_1, K_1))
print ( "Expected output:" , output_1)
print ( "Input: test_str = '{}', K = {}" . format (test_str_2, K_2))
print ( "Output:" , reverse_shift_ascii(test_str_2, K_2))
print ( "Expected output:" , output_2)
|
Output
Input: test_str = 'bccd', K = 1
Output: abbc
Expected output: abbc
Input: test_str = 'bccd', K = 2
Output: zaab
Expected output: zaab
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n), since we are creating a new string of length n.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...