BASHA TECH

[HackerRank][Python] Let's Review 본문

Activity/Coding Test

[HackerRank][Python] Let's Review

Basha 2023. 4. 17. 14:22
728x90

Task

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 (see the Sample below for more detail).

Note: 0 is considered to be an even index.

Example

s = abdecf

Print abc def

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 mathb S_j (where 0 < j < T - 1), print mathb S_j's even-indexed characters, followed by a space, followed by mathb S_j's odd-indexed characters.

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

if __name__ == '__main__':
    T = int(input().strip())

    for i in range(T):
        s = input().strip()
        even_chars = s[::2]
        odd_chars = s[1::2]
        print(even_chars, odd_chars)
  • First, we take the input integer T which represents the number of test cases.
  • Then, we loop through T number of times and take the input string s for each test case.
  • Using Python's slicing notation, we separate the even-indexed characters and odd-indexed characters of s into two separate strings even_chars and odd_chars.
  • Finally, we print even_chars followed by a space and then odd_chars.
728x90
반응형
Comments