
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to get final string after shifting characters with given number of positions in Python
Suppose we have a lowercase string s and another list of integers called shifts whose length is same as the length of s. Here each element in shifts[i] indicates it to shift the first i + 1 letters of s by shifts[i] positions. If shifting crosses 'z' it will be wrap up to 'a'. We have to find the resulting string after applying shifts to s.
So, if the input is like s = "tomato" shifts = [2, 5, 2, 3, 7, 4], then the output will be "qjcoes" so, after shifting first character 2 places, it will be 't' to 'v', so string is "vomato", After that first two characters 5 places. the string now will be "atmato" like that finally the string will be "qjcoes".
To solve this, we will follow these steps −
- start := ASCII of "a"
- res := a list of the ASCII of (i - start) for each i in s
- for i in range size of shifts - 2 to 0, decrease by 1, do
- shifts[i] := shifts[i] + shifts[i + 1]
- for i in range 0 to size of s - 1, do
- c :=(res[i] + shifts[i]) mod 26
- res[i] := character with ASCII (c + start)
- join the letters res into a string and return
Example
Let us see the following implementation to get better understanding −
def solve(s, shifts): start = ord("a") res = [ord(i) - start for i in s] for i in range(len(shifts) - 2, -1, -1): shifts[i] += shifts[i + 1] for i in range(len(s)): c = (res[i] + shifts[i]) % 26 res[i] = chr(c + start) return "".join(res) s = "tomato" shifts = [2, 5, 2, 3, 7, 4] print(solve(s, shifts))
Input
[2, 1], 3, 2
Output
qjcoes
- Related Articles
- C++ program to count final number of characters are there after typing n characters in a crazy writer
- Program to check one string can be converted to other by shifting characters clockwise in Python
- Final string after performing given operations in C++
- Program to find string after removing consecutive duplicate characters in Python
- Final state of the string after modification in Python
- Python Program to Count Number of Lowercase Characters in a String
- Program to find string after deleting k consecutive duplicate characters in python
- Program to find final states of rockets after collision in python
- Program to remove duplicate characters from a given string in Python
- Count Number of Lowercase Characters in a String in Python Program
- C++ Program to count number of characters to be removed to get good string
- Python Program to Extract Strings with at least given number of characters from other list
- Concatenated string with uncommon characters in Python program
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Switching positions of selected characters in a string in JavaScript
