Objective
Today we’re expanding our knowledge of Strings and combining it with what we’ve already learned about loops. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given a string, S, of length 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 (see the Sample below for more detail).

Note: 0 is considered to be an even index.

Input Format

The first line contains an integer, T (the number of test cases).
Each line i of the T subsequent lines contain a String, S.

Constraints

  • 1<=T<=10
  • 2<=length of S<=10000

Output Format

For each String Sj (where 0<=j<=T-1 ), print Sj‘s even-indexed characters, followed by a space, followed by Sj‘s odd-indexed characters.

Sample Input

2

Hacker

Rank

Sample Output

Hce akr

Rn ak

Solution:

T =int(input().strip())
len_str=[]
for i in range(T):
    a=input().strip()
    len_str.append(a)
#print(T)
def evn_odd(len_str):
 
    for i in len_str:
        str1=[]
        str2=[]
        str =list(i)
        for j in range(len(str)):
            if j % 2 == 0 :
                str1.append(str[j])
            else:
                str2.append(str[j])
        print(''.join(str1),  ''.join(str2))            
evn_odd(len_str)
HackerRank 30 Days of Code Challenge

It’s a pretty simple code. First, we take input T i.e the number of test cases and then we apply a for loop for reading input for the subsequent number of test cases and put them in a list called len_str. Then we define a function evn_odd which takes len_str and prints out the even-indexed and odd-indexed characters as 2 space-separated strings on a single line. Inside the function evn_odd we initialize two string str1, str2 inside a for loop which iterates the list len_str. Since item in list len_str is a string, so we converted each iterable string from len_str into a list. Then we applied second for loop inside first for loop and applide if condition i.e if the index is even for a particular character append that character to str1 else to str2. Finallyprint both str1 and str2.

Leave a Reply

Your email address will not be published.