Question: Write a function called shifting(data, index) which takes a list of numbers and an index integer as parameters. The function checks where the element at
Write a function called shifting(data, index) which takes a list of numbers and an index integer as parameters. The function checks where the element at the index position has to be placed correctly (for a later insertion operation) and shifts all elements one place to the right until the index position is reached again. For example: consider the following numbers:
data = [20, 27, 69, 25, 76, 41] shifting(data, 3)
The element at index 3 is "25". The correct placement for "25" in the data list is at index 1. Therefore, the function will shift all elements from index 1 until index 3 (not including it) by one place to the right and the resulting list will be:
[20, 27, 27, 69, 76, 41]
Note: This function just handles the shifting to the right. You don't need to replace the element in the correct location in this question. Also, you can assume that the list is not empty. You may not use the sorted() function in this task.
For example:
Test:
numbers = [20, 27, 69, 25, 15, 41] shifting(numbers, 3) print(numbers)
Result: [20, 27, 27, 69, 15, 41]
Test: numbers = [10, 15, 20, 27, 69, 41] shifting(numbers, 5) print(numbers)
Result: [10, 15, 20, 27, 69, 69]
def shifting(data, index): value_to_insert = data[index]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
