简体   繁体   中英

reverse string order and concatenate

I got this question on a coding site. The contest is over now and the editorial isn't out yet.

Jack is awesome. His friends call him little Einstein. To test him, his friends gave him a string. They told him to add the string with its reverse string and follow these rules:

Every ith character of string will be added to every ith character of reverse string. Both string will contain only lower case alphabets(az). Eg:- a+a=b,a+c=d,z+a=a (Refer to sample test cases for more details) Input:

First line contains a value N denoting number of test cases. Next N lines contains string str.

Output:

For every string str output the string that Jack's friends wants from him.

Constraints

1 <= N <= 10

1 <= str <= 10^5

SAMPLE INPUT 4 hello codeapocalypse programming world SAMPLE OUTPUT wqxqw hhtdmqrrqmdthh wfxtebetxfw aajaa

My Code:

for _ in range(int(input())):
sr=input()
s=str.lower(sr)
revs=s[::-1]
mys=""
for i in range(len(s)):
    temp=ord(s[i])+ord(revs[i])-96
    if temp<122:
        mys+=chr(temp)
    else:
        mys+=chr(temp-26)
print(mys)

It passed all the sample test cases but didn't pass any of the private test cases. Where am I wrong?

for _ in range(int(input())):
    sr=input()
    s=str.lower(sr)
    revs=s[::-1]
mys=""
for i in range(len(s)):
    temp=ord(s[i])+ord(revs[i])-96
    if temp<122:
        mys+=chr(temp)
    else:
        mys+=chr(temp-26)
print(mys)

The "s" parameter, is the last word you type in, so that´s what yor recive encoded, try creating a list of all the words you type and then give it to the encoder part instead of the last word

listx = []
for _ in range(int(input("Introduce a number"))):
    string =input()
    s=str.lower(string)
    reverse = s[::-1]
    listx.append(s)
string2 = ""
for word in range(len(listx)):
    for i in range(len(listx[word])):
        temp = ord(s[i])+ord(reverse[i])-96
        if temp < 122:
            string2 += chr(temp)
        else:
            string2 += chr(temp-26)
    print(string2)

This is the result code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM