규도자 개발 블로그

[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 6: Let's Review 본문

알고리즘/풀이

[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 6: Let's Review

규도자 (gyudoza) 2019. 3. 21. 20:57

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 ().

풀이

# Enter your code here. Read input from STDIN. Print output to STDOUT

test_case_count = int(input())
test_case_input = []
for test_case_index in range(test_case_count):
    current_word = input()
    odd_string = ''
    even_string = ''
    for string_index in range(len(current_word)):
        if not string_index % 2 == 0:
            even_string = even_string + current_word[string_index]
        else:
            odd_string = odd_string + current_word[string_index]
    print(odd_string + ' ' + even_string)

설명

0은 짝수로 치고 홀수번째 글자와 짝수번째 글자를 따로 분류하여 출력하는 문제이다.

Comments