Suppose we have an alphanumeric string s that contains lowercase English letters in its even positions and digits in its odd positions. Consider an operation shift(c, x), where c is any character and x is a number (digit), this will find the xth character after c. So, for example, shift('p', 5) = 'u' and shift('a', 0) = 'a'. Now for every odd index i, we want to replace the digit s[i] with shift(s[i-1], s[i]). We have to find s after replacing all digits.
So, if the input is like s = "a2b1d4f3h2", then the output will be "acbcdhfihj" because
shift('a', 2) = 'c'
shift('b', 1) = 'c'
shift('d', 4) = 'h'
shift('f', 3) = 'i'
shift('h', 2) = 'j'
To solve this, we will follow these steps −
res:= blank string
for i in range 0 to size of s, do
if s[i] is a digit, then
res := res concatenate character from (ASCII s[i] + ASCII of s[i-1])
res := res concatenate s[i]
Let us see the following implementation to get better understanding −
def solve(s): res="" for i in range(len(s)): if s[i].isdigit(): res+= chr(int(s[i])+ord(s[i-1])) else: res+=s[i] return res s = "a2b1d4f3h2" print(solve(s))