Skip to main content

Command Palette

Search for a command to run...

Day 7 of My 30-Day Coding Challenge on HackerRank

Updated
2 min read

Day 7 was all about strings and loops—combining two important concepts to solve a character-based problem.

Today’s challenge focused on separating the even-indexed and odd-indexed characters of a string.


Problem Overview

Today’s challenge focused on:

  • Understanding string indexing

  • Using loops with strings

  • Separating characters based on position

  • Handling multiple test cases

We were given a string s and asked to print:

  • All characters at even indices

  • All characters at odd indices

Both outputs had to be printed on the same line separated by a space.

Note: Indexing starts from 0, so index 0 is even.


My Solution

t = int(input())

for _ in range(t):
    s = input()
    
    even = s[::2]
    odd = s[1::2]
    
    print(even, odd)

What I Did Today

I learned how to:

  • Access string characters using indexes

  • Use slicing in Python

  • Handle multiple inputs using loops

  • Separate data based on conditions

Here’s the logic I followed:

1. Read number of test cases
2. For each string:
3. Get characters at even indexes
4. Get characters at odd indexes
5. Print both separated by space

🧠 What I Learned

Today’s key takeaways:

  • Strings can be accessed like arrays using indexes

  • Python slicing makes problems easier

  • s[::2] gets even-indexed characters

  • s[1::2] gets odd-indexed characters

  • Combining loops + strings is powerful


Challenges Faced

  • Understanding index positions clearly

  • Remembering that indexing starts at 0

  • Formatting output exactly as required


Why This Problem Matters

This problem helps in understanding:

  • String traversal techniques

  • Data filtering based on position

  • Efficient use of slicing in Python

These concepts are useful in text processing and coding interviews.


Sample Run

Input:

2
Hacker
Rank

Output:

Hce akr
Rn ak

Plan for Day 8

  • Improve problem-solving speed

💬 Final Thoughts

Day 7 was a fun challenge that showed how useful string indexing can be.

Simple concepts like loops and slicing can solve problems efficiently when used correctly.

Let’s keep learning and improving