Back to Practice
#0220

Zigzag String Rows

MediumPython25 min20 XP

Problem

Place characters into num_rows in zigzag movement and return the rows as a list of strings.

Why This Matters

This is a practical simulation pattern: keep row state and direction instead of trying to derive every index formula at once.

Function Signature

def zigzag_rows(text, num_rows):

Examples

Example 1
Inputtext = "PAYPAL", num_rows = 3
Output["PA", "APL", "Y"]

Characters move down rows 0,1,2 and then back up row 1.

Constraints

  • Return the exact requested value.
  • Handle small edge cases cleanly.
  • Prefer readable Python over clever shortcuts.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputtext = "PAYPAL", num_rows = 3
Expected["PA", "APL", "Y"]

Characters move down rows 0,1,2 and then back up row 1.

Hidden Test Categories
num_rows = 1text shorter than rowslonger string