Python BasesFacile

Rotation du tableau

Guide détaillé et implémentation de Python pour le problème 'Rotation des tableaux'.

Énoncé du problème

Facile

Écrivez une fonction rotate_array(arr, k) qui fait pivoter le tableau arr vers la gauche de k positions et renvoie le tableau pivoté. Les éléments qui vont au-delà du début se terminent jusqu'à la fin. Par exemple, faire tourner [1,2,3,4,5] vers la gauche de 2 donne [3,4,5,1,2].

Contraintes
  • 0 <= len(arr) <= 10^5
  • 0 <= k <= 10^6
  • -10^9 <= arr[i] <= 10^9

Exemples

Example 1
Input
arr = [1, 2, 3, 4, 5], k = 2
Output
[3, 4, 5, 1, 2]
Explanation

Rotate left by 2: first 2 elements [1,2] move to the end.

Example 2
Input
arr = [10, 20, 30, 40], k = 1
Output
[20, 30, 40, 10]
Explanation

Rotate left by 1: 10 moves to the end.

Example 3
Input
arr = [1, 2, 3], k = 3
Output
[1, 2, 3]
Explanation

Rotating by the array length returns the original array.

Need a Hint?
Pensez à utiliser des structures de données spécifiques aux tableaux, comme des ensembles ou des tas.
Edge Cases to Watch
  • Structures d'entrée vides
  • Entrées à élément unique
  • Grandes limites numériques

Prêt à résoudre ?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Ouvrir dans l'éditeur
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Ressources Python recommandées

Développez vos connaissances avec des didacticiels interactifs, des aide-mémoire et des comparaisons de codes associés.