Swift Array Programming Exercises: Create a new array that is left shifted from a given array of integers
Write a Swift program to create a new array that is left shifted from a given array of integers. So [11, 15, 13, 10, 45, 20] will be [15, 13, 10, 45, 20, 11].
Sample Solution:
Swift Code:
func left_shift(array_nums: [Int]) -> [Int] {
var new_array = array_nums
for x in 1..<array_nums.count {
new_array[x-1] = array_nums[x]
}
new_array[new_array.count-1] = array_nums[0]
return new_array
}
print(left_shift(array_nums: [11, 15, 13, 10, 45, 20]))
print(left_shift(array_nums: [0, 1, 2]))
print(left_shift(array_nums: [1, 2]))
print(left_shift(array_nums: [1]))
Sample Output:
[15, 13, 10, 45, 20, 11] [1, 2, 0] [2, 1] [1]
Swift Programming Code Editor:
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics