HackerRank Day 6 Solution in Python: Let’s review

Today we will see the HackerRank Day 6 Solution in Python. The problem is named Let’s review which is part of 30 Days of code on HackerRank. Let’s get started!

Day 6: Let’s review Problem statement

We are given a string, S, of length N, that is indexed from 0 to N-1. Our task is to print its even-indexed and odd-indexed characters as two space-separated strings on a single line

Sample Input

1
Hackerrank

Sample Output

Hcern akrak

Explanation: The first string contains the ordered characters from even indices is Hcern, and the second string contains the ordered characters from odd indices is akrak.

You can solve the problem here.

HackerRank Day 6 Solution in Python

n=int(input())

for i in range(n):
    #Get Input string
    string=input()
    #Variable to store characters in even index
    even=''
    #Variable to store characters in even index
    odd=''
    for j in range(len(string)):
        #if index is even
        if j%2==0:
            even+=string[j]
        #if index is odd
        else:
            odd+=string[j]
    print("{} {}".format(even,odd)) 

Code Explanation

  • First, get the input string and create two variables to store characters in even index and odd index
  • Then iterate through the string and check the index
  • If the index is even append the character to the even variable
  • If the index is odd append the character to the odd variable
  • Finally, print the values stored in the even and odd strings

Also Read:

Share:

Author: Keerthana Buvaneshwaran