1. String rotation in special manner
You are provided two or more strings, where each string is associated with the number (seperated by :).
If sum of square of digits is even then rotate the string right by one position, and if sum of square of digits is odd then rotate the string left by two position.
Input :
Accept multiple values in the form of String:Integer seperated by ','
Output :
Rotated Strings.
Sample Testcases :
I/P 1 :
abcde:234,pqrs:246
O/P 1 :
cdeab
spqr
Explanation :
For first teststring, 'abcde' associated integer is 234, squaring 4+9+16=29, which is odd, so we rotate string left by two positions.
For second teststring, 'spqr' associated integer is 246, squaring 4+16+36=56, which is even, so we rotate string right by one position.
Solution :
s=input().split(",")
sa=[]
num=[]
for i in s:
s1,n=i.split(":")
sa.append(s1)
num.append(n)
def rotate(s2,n):
n=list(str(n))
s=0
for i in n:
s+=int(i)**2
if s%2==0:
return s2[-1:]+s2[:-1] #right rotate
else:
return s2[2:]+s2[:2] #left rotate
for i in range(len(num)):
print(rotate(sa[i],num[i]))
You are provided two or more strings, where each string is associated with the number (seperated by :).
If sum of square of digits is even then rotate the string right by one position, and if sum of square of digits is odd then rotate the string left by two position.
Input :
Accept multiple values in the form of String:Integer seperated by ','
Output :
Rotated Strings.
Sample Testcases :
I/P 1 :
abcde:234,pqrs:246
O/P 1 :
cdeab
spqr
Explanation :
For first teststring, 'abcde' associated integer is 234, squaring 4+9+16=29, which is odd, so we rotate string left by two positions.
For second teststring, 'spqr' associated integer is 246, squaring 4+16+36=56, which is even, so we rotate string right by one position.
Solution :
s=input().split(",")
sa=[]
num=[]
for i in s:
s1,n=i.split(":")
sa.append(s1)
num.append(n)
def rotate(s2,n):
n=list(str(n))
s=0
for i in n:
s+=int(i)**2
if s%2==0:
return s2[-1:]+s2[:-1] #right rotate
else:
return s2[2:]+s2[:2] #left rotate
for i in range(len(num)):
print(rotate(sa[i],num[i]))

Comments
Post a Comment