【hackerrank】-Day 6: Let's Review

30-review-loop
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, , of length that is indexed from to , print its even-indexed and odd-indexed characters as space-separated strings on a single line (see the Sample below for more detail).

Note: is considered to be an even index.

Input Format

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

Constraints

Output Format

For each String (where ), print ‘s even-indexed characters, followed by a space, followed by ‘s odd-indexed characters.

Sample Input

2
Hacker
Rank
Sample Output

Hce akr
Rn ak
Explanation

Test Case 0:

The even indices are , , and , and the odd indices are , , and . We then print a single line of space-separated strings; the first string contains the ordered characters from ‘s even indices (), and the second string contains the ordered characters from ‘s odd indices ().

Test Case 1:

The even indices are and , and the odd indices are and . We then print a single line of space-separated strings; the first string contains the ordered characters from ‘s even indices (), and the second string contains the ordered characters from ‘s odd indices ().


def ss_print(string):
    s_odd = s_even = ''
    for i in range(0,len(string)):
        if i%2 != 0 :
            s_odd = s_odd + string[i]
        else:
            s_even = s_even + string[i]
        i = i + 1
    print(s_even + " " + s_odd)

ii = int(input())
s1 = input()
ss_print(s1)
s2 = input()
ss_print(s2)

Congratulations!

You have passed the sample test cases. Click the submit button to run your code against all the test cases.

Testcase 0
Input (stdin)
2
Hacker
Rank
Your Output (stdout)
Hce akr
Rn ak
Expected Output
Hce akr
Rn ak
AuthorAllisonP
DifficultyEasy
Max Score30
Submitted By155208
NEED HELP?
View tutorial
View discussions
View editorial
View top submissions
RATE THIS CHALLENGE

MORE DETAILS
Download problem statement
Download sample test cases
Suggest Edits

猜你喜欢

转载自blog.csdn.net/langhailove_2008/article/details/81176897
今日推荐