Ctrl K

Reading a Zigzag by Rows

Medium
The text text is written as a snake across rows lines: the first letter goes on the top line, the next ones go down one line at a time to the bottom line, then diagonally back up to the top, down again, and so on. For example, SNOWFALL on three lines looks like this: S F N W A L O L Then the lines are read from top to bottom, each from left to right, and joined together. Return the resulting string. With rows = 1 the text does not change.
Example 1
text="SNOWFALL", rows=3 → 'SFNWALOL'

The three-row snake is S . . . F . . . / . N . W . A . L / . . O . . . L .. Reading row by row gives SF + NWAL + OL.

Example 2
text="abcdef", rows=2 → 'acebdf'

With two rows the letters simply alternate: even positions on top, odd ones below.

Example 3
text="hi", rows=5 → 'hi'

There are more rows than letters, so the text stays a single column and comes out unchanged.

Constraints
  • 1 ≤ len(text) ≤ 1000
  • text consists of English letters (lowercase and uppercase), , and .
  • 1 ≤ rows ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumLongest Mirror Fragment MediumLenient Number Reader MediumNumber in Roman Numerals MediumKeypad Words Medium
loading Python…
Input
text="SNOWFALL", rows=3
Expected
'SFNWALOL'

Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.