Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Ruby Array Exercises: Create an array with the elements "rotated left" of a given array of ints length 3

Ruby Array: Exercise-11 with Solution

Write a Ruby program to create an array with the elements "rotated left" of a given array of integers length 3.

Ruby Array Exercises: Create an array with the elements 'rotated left' of an given array of ints length 3

Ruby Code:

def check_array(nums)
    rotated = nums[1], nums[2], nums[0];
	return rotated;
end
print check_array([1, 2, 5]),"\n" 
print check_array([1, 2, 3]),"\n" 
print check_array([1, 2, 4]) 

Output:

[2, 5, 1]
[2, 3, 1]
[2, 4, 1]

Flowchart:

Flowchart: Create an array with the elements 'rotated left' of a given array of integers length 3

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to split a delimited string into an array.
Next: Write a Ruby program to create a new array with the elements in reverse order from a given array of integers length 3.

What is the difficulty level of this exercise?