String Rotation Odd and Even Positions
Coding Problem Keys
String Rotation Odd and Even Positions
Problem Statement
A string S and two integers M and N are passed as input. The program must rotate the characters present in the odd positions of the string M times and rotate characters present in the even positions of the string N times.Boundary Condition(s)
2 <= Length of the string <= 1000.1 <= M, N <= 1000.
Input Format
The first line contains the values of S, M and N separated by space(s).Output Format
The first line contains the rotated string.Example Input/Output 1
Input
technology 2 3Output
logotycenh
Explanation
The characters present in the odd positions are 'tcnlg'. When rotated 2 positions, the value is 'lgtcn'.
The characters present in the even position are 'ehooy', When rotated 3 positions, the value is 'ooyeh'.
Hence the rotated string value is 'logotycenh'.
Example Input/Output 2
Input
abcde 1 1Output
edabcExplanation
The characters in odd positions are 'a', 'c' and 'e' which are rotated once to become 'eac'.
The characters in even positions are 'b' and 'd' which are rotated once to become 'db'.
Hence the rotated string value is 'edabc'.
Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: Python 3 Language
s,n,m=map(str,input().split())
n,m=int(n),int(m)
a=[];b=[]
for i in range(len(s)):
if(i%2==0):
a.append(s[i])
else:
b.append(s[i])
while n:
a.insert(0,a.pop(-1))
n-=1
while m:
b.insert(0,b.pop(-1))
m-=1
for i in range(len(s)):
if i%2==0:
print(a.pop(0),end="")
else:
print(b.pop(0),end="")
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments