如何打印字符串的evendexed和odddexed字符?

2024-05-29 02:02:16 发布

您现在位置:Python中文网/ 问答频道 /正文

手头的任务:

Given a string, S, of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line.

The test cases are written such that The first line contains an integer, N (the number of test cases). Each line i of the N subsequent lines contain a String.

这是我的代码:

N = int(raw_input())

for i in range(0,N):
    string = raw_input()

evenlist = []
oddlist = []

for item, char in enumerate(strg):
    if item % 2 == 0:
        evenlist.append(char)
    else:
        oddlist.append(char)

print ''.join(evenlist), ''.join(oddlist)

样本运行:

The first input is:
2
Hacker
Rank

Expected output is:
Hce akr
Rn ak

但我得到:

HceRn akrak

这里有一个指向assignment的链接,可以更好地解释这个问题。


Tags: ofthetestinputstringthatisline

热门问题