Shivkumar Shivkumar - 15 days ago 7
Python Question

Need better logic to add Every ith character of string with every ith character of its reverse string

I am a newbie for python coding and building some basic logic's in some hard coding way.

The problem statement is simple and easy.

Lets say I have word :

hello
and its reverse is
olleh


Adding value of each ith char ie h = 8 and o = 15 will be 23 and is back converted to string, ie 23 = w ( for a = 1, b=2, so on)

Finally ouput will be :
wqxqw


I have below code :

given = list(raw_input())

# forward & reversing the list
forward_str = given
reverse_str = given[::-1]

# converting letters(a-z) to number(1-26)
forward_nums = []
for f in forward_str:
forward_nums.append(ord(f) - ord('a') + 1)

reverse_nums = []
for r in reverse_str:
reverse_nums.append(ord(r) - ord('a') + 1)


# Addtion of element-wise from both lists

# from operator import add
# map(add, list1, list2)

addition = [sum(x) for x in (zip(forward_nums,reverse_nums))]


# For letters a-z ,numbers should be in range 1-26,so i > 26 is added back
valid_nums = []
for i in addition:
if i <= 26:
valid_nums.append(i)
elif i > 26:
x = i %26
valid_nums.append(x)


# converting number(1-26) to letters(a-z)
import string
num2alpha = dict(zip(range(1, 27), string.ascii_lowercase))

chars =[]
for i in valid_nums:
chars.append(num2alpha[i])

#converting list to string
output = "".join(chars)


print "forward_str : ",forward_str
print "reverse_str : ",reverse_str,"\n -----------------------------------------"
print "forward_nums : ",forward_nums
print "reverse_nums : ",reverse_nums,"\n----------------------------------------"
print "additon : ",addition
print "valid_nums : ",valid_nums,"\n---------------------------------------------"
print "converted_list : ",chars
print "final output : ",output


For input :
helloworld


output is :

forward_str : ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
reverse_str : ['d', 'l', 'r', 'o', 'w', 'o', 'l', 'l', 'e', 'h']
-----------------------------------------
forward_nums : [8, 5, 12, 12, 15, 23, 15, 18, 12, 4]
reverse_nums : [4, 12, 18, 15, 23, 15, 12, 12, 5, 8]
----------------------------------------
additon : [12, 17, 30, 27, 38, 38, 27, 30, 17, 12]
valid_nums : [12, 17, 4, 1, 12, 12, 1, 4, 17, 12]
---------------------------------------------
converted_list : ['l', 'q', 'd', 'a', 'l', 'l', 'a', 'd', 'q', 'l']
final output : lqdalladql


consider given string is in lowercase.

I am looking for some easier and more pythonic solution for this.

Thanks in Advance

Answer

simplify to below using list comprehension

import string
given = list(raw_input())

# forward & reversing the list
forward_str = given
reverse_str = given[::-1]

forward_nums = [ord(i)-ord('a')+1 for i in forward_str]
reverse_nums = [ord(i)-ord('a')+1 for i in reverse_str]

addition = [sum(x) for x in (zip(forward_nums,reverse_nums))]
valid_nums = [i % 26 for i in addition]

chars = [string.ascii_lowercase[i-1] for i in valid_nums]
output = ''.join(chars)

it can be further simplify to below if you don't need the intermediate result

import string
given = list(raw_input())

valid_nums = [ (ord(i)+ord(j)-ord('a')-ord('a')+2) % 26 for i,j in zip(given,given[::-1]) ]
output = ''.join([string.ascii_lowercase[i-1] for i in valid_nums])

OR

from string import ascii_lowercase as al
given = list(raw_input())

valid_nums = [ (al.index(i) + al.index(j)+2) % 26 for i,j in zip(given,given[::-1]) ]
output = ''.join([al[i-1] for i in valid_nums])