Ctrl K

Flip Every Other Block

Easy
You are given a string text and a number k. Cut the string from left to right into blocks of k characters; the last block may be shorter. Reverse the first block, the third, the fifth and so on, that is, every odd-numbered block when counting from one. Leave the even-numbered blocks as they are. Return the resulting string.
Example 1
text='abcdefghij', k=3 → 'cbadefihgj'

Blocks of 3: abc, def, ghi, j. The first and third are reversed: cba + def + ihg + j. The last block j is the fourth one, so it stays.

Example 2
text='window', k=4 → 'dniwow'

The first block wind becomes dniw, and the second, short block ow stays: dniwow.

Example 3
text='sky', k=5 → 'yks'

The whole string is shorter than k, so it is the first block and gets reversed: yks.

Constraints
  • 0 ≤ text.length ≤ 10⁴
  • text consists of lowercase English letters
  • 1 ≤ k ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWhere the Fragment Starts EasyMirror Phrase Check EasySame When Flipped EasyFlip the Characters in Place EasyMirror the Vowels Easy
loading Python…
Input
text='abcdefghij', k=3
Expected
'cbadefihgj'

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