livepython.net

213. Python Program to Rotate a List by K Places

MediumPhase 2 · Practice

This one takes a few steps. It uses list slices, + on lists and the % sign, and you know all three.

Overview

Rotating a list is a common interview question. Games use it to pass the turn around a table, and shop screens use it to move a row of pictures.

Problem Statement

Ines shows a row of pictures on a shop screen. At each step, every picture moves one place to the right, and the last picture jumps to the front.

  • Rotating right by 1 moves each number one place to the right. The last number moves to the front: 1 2 3 4 becomes 4 1 2 3.
  • Rotating by k does this k times.
  • k can be 0, and it can be bigger than the length of the list.
  • Print Rotated: <numbers>, with one space between the numbers.

Examples

Example 1. Input:

1 2 3 4 5
2

gives:

Rotated: 4 5 1 2 3

Why: rotating twice moves the last two numbers, 4 and 5, to the front.

Example 2. Input:

10 20 30
4

gives:

Rotated: 30 10 20

Why: rotating 3 times gives the same list back. So 4 times is the same as 1 time, and 4 % 3 is 1.

Input

Two lines. Line 1: whole numbers with one space between them (1 number or more). Line 2: the whole number k (0 or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line: Rotated:, a space, and the rotated numbers with one space between them. The tests try 8 different inputs, also k = 0, k equal to the length, and a very big k.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 12 · Working With Strings
solution.py
# write your code here

EXPECTED OUTPUT

1Rotated: 4 5 1 2 3

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.