curriculum/challenges/english/blocks/daily-coding-challenges-python/68f6587287ad1f4ad39b0c81.md
Given an array and an integer representing how many positions to shift the array, return the shifted array.
For example, given [1, 2, 3] and 1, shift the array 1 to the left, returning [2, 3, 1].
shift_array([1, 2, 3], 1) should return [2, 3, 1].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(shift_array([1, 2, 3], 1), [2, 3, 1])`)
}})
shift_array([1, 2, 3], -1) should return [3, 1, 2].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(shift_array([1, 2, 3], -1), [3, 1, 2])`)
}})
shift_array(["alpha", "bravo", "charlie"], 5) should return ["charlie", "alpha", "bravo"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(shift_array(["alpha", "bravo", "charlie"], 5), ["charlie", "alpha", "bravo"])`)
}})
shift_array(["alpha", "bravo", "charlie"], -11) should return ["bravo", "charlie", "alpha"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(shift_array(["alpha", "bravo", "charlie"], -11), ["bravo", "charlie", "alpha"])`)
}})
shift_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 15) should return [5, 6, 7, 8, 9, 0, 1, 2, 3, 4].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(shift_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 15), [5, 6, 7, 8, 9, 0, 1, 2, 3, 4])`)
}})
def shift_array(arr, n):
return arr
def shift_array(arr, n):
length = len(arr)
n = n % length
if n < 0:
n += length
return arr[n:] + arr[:n]