Ctrl K

Rotate a String by Commands

Easy
You get a word word and a list of commands moves, where each command is a pair [d, k]: - d = 0 rotates left by k: move the first letter to the end, k times; - d = 1 rotates right by k: move the last letter to the front, k times. Apply all commands in order and return the resulting string.
Example 1
word="rotor", moves=[[0, 2]] → 'torro'

Move the first letter to the end twice: "rotor" → "otorr" → "torro".

Example 2
word="python", moves=[[1, 1], [0, 3], [1, 8]] → 'python'

Right 1, left 3, right 8 add up to right 6, a full turn for a 6-letter word, so it stays the same.

Example 3
word="queue", moves=[[1, 2]] → 'ueque'
Constraints
  • 1 ≤ len(word) ≤ 100
  • word consists of lowercase English letters
  • 1 ≤ len(moves) ≤ 100
  • moves[i] = [d, k], where d is 0 or 1 and 0 ≤ k ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsClosest Clock Readings MediumNumbers From Working Keys HardRound Prices to a Total MediumLetter Sum Cipher HardGrading the Arithmetic Quiz Hard
loading Python…
Input
word="rotor", moves=[[0, 2]]
Expected
'torro'

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