DEV Community

M__
M__

Posted on

DAY 6: REVIEW

Hi there, I hope you’re doing good.

I’m doing well as well and we are still pushing through the challenge and today was a review of some of the concepts learnt so far such as strings, input/output, conditionals and loops.

The challenge:*
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.
For the input format: the first line contains an integer, T (the number of test cases) and each line i of the T test cases contains a string S.

From the given challenge we can break it down to the steps we need to carry out to complete it:
• Accept an input for the number of test cases one will need.
• Loop through the number of test cases and accept input for the strings
• Loop through each string and determine which characters are even-indexed and which are odd-indexed and store them in a variable.
• Print the final output as expected.

My Solution:

#integer input for the number of test cases
N = int(input())

#variables to hold the even and odd indexed characters; I used lists.
index_even = []
index_odd = []

#looping through each test case
for i in range(0,N):
    #input for the string
    string = input() 

    #empty the lists if they hold any strings 
    index_even.clear()
    index_odd.clear()

    #loop through the string; item(index value) and char(character value)
    #enumerate() method eases looping through the string
    for item , char in enumerate(string):

        #conditional to check if the index is odd or even.
        #if even, the character is added to the index_even list respectively
        if item % 2 == 0:
            index_even.append(char)
        else:
            index_odd.append(char)

    #transform the characters in the list to form a single word.
    print(''.join(index_even), ''.join(index_odd))

''' 
Sample Input:
2
Hacker
Rank

Sample Output:
Hce akr
Rn ak
'''
Enter fullscreen mode Exit fullscreen mode

Every time one is learning something new, things tend to look foreign but with the help of Google you can be able to pick up a thing two and get yourself unstuck (There is nothing wrong with going to google for help).

Top comments (2)

Collapse
 
muhimen123 profile image
Muhimen

Python is well known for its simplicity. This will work the same.

even = ''.join([string[i] for i in range(0, len(string), 2)])
odd = ''.join([string[i] for i in range(1, len(string), 2)])
Collapse
 
idimaimuna profile image
M__

That's really nice. Thanks