Character Shifting – Infytq 2019 Solve

Problem: Given a dictionary type value, we have to find the sum of the squares of the given integer value. If it turns out to be even the shift the last two character of the key to front. If it’s odd then shift the first character of the key to last.

Sample Input:

abcd:1234,bcdgfhf:127836,sdjks:1245

Sample Output:

cdab cdgfhfb kssdj

Explanation: Here the first key is “abcd” and it’s value is “1234”. The sum of 1^2+2^2+3^2+4^2 = 30 which is an even number, hence last two characters of the key is shifted to the front (abcd —> cdab).

“bcdgfhf” has it’s value “127836”. The sum of squares of all the integers is an odd number. Hence, first character of the key is shifted  to the end (bcdgfhf —> cdgfhfb)

Solution: We strongly recommend you to try the problem first before moving to the solution.

Python

# Code to understand the character shifting problem in Python
# www.codewindow.in

dictionary=input().split(',')
for ob in dictionary:
    str_ob=ob.split(':')
    string=str_ob[0]
    num=str_ob[1]
    length=len(string)
    sum=0
    for digit in num:
        sum += (int(digit)**2)
    if sum%2==0:
        s=string[length-2 : length]
        print(s+string[0 : length-2], end=' ')
    else:
        s=string[0]
        print(string[1:length]+s, end=' ')

Input:

abcd:1234,bcdgfhf:127836,sdjks:1245

Output:

cdab cdgfhfb kssdj 

Follow Us

You Missed


Leave a Comment

Your email address will not be published. Required fields are marked *