w3resource

Ruby Array: Exercises, Practice, Solution

Ruby Array Exercises[48 exercises with solution]

1. Write a Ruby program to check whether a value exists in an array. Go to the editor

Sample Output:

Original array:
["Red", "Green", "Blue", "White"]
Check if 'Green' in color array!
true
Check if 'Pink' in color array!
false

Click me to see the solution

2. Write a Ruby program to check whether 7 appears as either the first or last element in a given array. The array length must be 1 or more. Go to the editor

Sample Output:

true
true
false

Click me to see the solution

3. Write a Ruby program to pick number of random elements from a given array. Go to the editor

Sample Output:

Original array:
[12, 34, 23, 56]
 2 random elements from the array.
[34, 12]
 3 random elements from the array.
[56, 12, 34]

Click me to see the solution

4. Write a Ruby program to check whether first and the last element are the same of a given array of integers. The array length must be 1 or more. Go to the editor

Sample Output:

false
true
false

Click me to see the solution

5. Write a Ruby program to compute the sum of elements in a given array. Go to the editor

Sample Output:

  
Original array:
[12, 34, 23, 56]
Sum of the values of the above array:
125
  

Click me to see the solution

6. Write a Ruby program to remove duplicate elements from a given array. Go to the editor
Sample Output:

Original array:
[1, 2, 3, 4, 1, 2, 2, 3, 5, 6]
 Array with unique elements:
[1, 2, 3, 4, 5, 6]

Click me to see the solution

7. Write a Ruby program to check two given arrays of integers and test whether they have the same first element or they have the same last element. Both arrays length must be 1 or more. Go to the editor
Sample Output:

true
false
true

Click me to see the solution

8. Write a Ruby program to remove blank elements from a given array. Go to the editor
Sample Output:

Original array:
["Red", "Green", "", "Blue", "White"]
Remove blank element from the above array:
["Red", "Green", "Blue", "White"]

Click me to see the solution

9. Write a Ruby program to compute the sum of all the elements. The array length must be 3 or more. Go to the editor
Sample Output:

8
6
7

Click me to see the solution

10. Write a Ruby program to split a delimited string into an array. Go to the editor
Sample Output:

Original delimited string:
Red, Green, Blue, White1, 3, 4, 5, 7String to array:
["Red", " Green", " Blue", " White"]
[1, 3, 4, 5, 7]

Click me to see the solution

11. Write a Ruby program to create an array with the elements "rotated left" of a given array of ints length 3. Go to the editor
Sample Output:

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

Click me to see the solution

12. Write a Ruby program to create a new array with the elements in reverse order from a given an array of integers length 3. Go to the editor
Sample Output:

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

Click me to see the solution

13. Write a Ruby program to find the larger between the first and last elements of a given array of integers of length 3. Replace all the other values to be that value. Return the changed array. Go to the editor
Sample Output:

[5, 5, 5]
[3, 3, 3]
[4, 4, 4]

Click me to see the solution

14. Write a Ruby program to compute the sum of the first 2 elements of a given array of integers. If the array length is less than 2, just sum up the elements that exist, returning 0 if the length of the array is 0. Go to the editor
Sample Output:

3
6
1

Click me to see the solution

15. Write a Ruby program to create an array of length 2 containing their middle elements from two given arrays of integers of length 3. Go to the editor
Sample Output:

[2, 5]
[5, 8]
[2, 14]

Click me to see the solution

16. Write a Ruby program to concatenate array of arrays into one. Go to the editor
Sample Output:

Input your age: You are a minor

Click me to see the solution

17. Write a Ruby program to check whether a given array of integers of length 2 contains a 4 or a 7. Go to the editor
Sample Output:

true
true
false

Click me to see the solution

18. Write a Ruby program to check whether a given array of integers of length 2 does not contain a 6 or a 9. Go to the editor
Sample Output:

true
false
false

Click me to see the solution

19. Write a Ruby program to check whether a given array of integers contains 3 twice, or 5 twice. The array will be length 0, 1, or 2. Go to the editor
Sample Output:

false
false
false
true
true

Click me to see the solution

20. Write a Ruby program to set 5 to 1 whether there is a 3 immediately followed by a 4 in a given array of integers (length 3). Go to the editor
Sample Output:

[1, 3, 1]
[3, 1, 6]
[3, 9, 5]

Click me to see the solution

21. Write a Ruby program to compute the sum of two arrays (length 3) and return the array which has the largest sum. Go to the editor
Sample Output:

[2, 4, 4]
[11, 3, 5]

Click me to see the solution

22. Write a Ruby program to create a new array of length 2 containing the middle two elements from a given array of integers of even length 2 or more. Go to the editor
Sample Output:

[3, 5]
[5, 21]

Click me to see the solution

23. Write a Ruby program to create a new array of length 4 containing all their elements from two array of integers, length 2. Go to the editor
Sample Output:

[1, 3, 5, 4]
[11, 3, 5, 21]

Click me to see the solution

24. Write a Ruby program to swap the first and last elements of a given array of integers, length will be at least 1. Return the modified array. Go to the editor
Sample Output:

[1]
[3, 1]
[5, 3, 1]
[21, 3, 5, 11]

Click me to see the solution

25. Write a Ruby program to create a new array of length 3 containing the elements from the middle of a given array of integers of odd length (at least 3). Go to the editor
Sample Output:

[1, 3, 4]
[2, 3, 7]

Click me to see the solution

26. Write a Ruby program to find the largest value from a given array of integers of odd length. The array length will be a least 1. Go to the editor
Sample Output:

4
7
2

Click me to see the solution

27. Write a Ruby program to create a new array using first three elements of a given array of integers. If the length of the given array is less than three return the original array. Go to the editor
Sample Output:

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

Click me to see the solution

28. Write a Ruby program to create a new array with the first element of two arrays. If length of any array is 0, ignore that array. Go to the editor
Sample Output:

[3, 7]
[3, 6]
[3]

Click me to see the solution

29. Write a Ruby program to get the number of even integers in a given array. Go to the editor
Sample Output:

2
1
1

Click me to see the solution

30. Write a Ruby program to find the difference between the largest and smallest values of a given array of integers and length 1 or more. Go to the editor
Sample Output:

3
2
1

Click me to see the solution

31. Write a Ruby program to compute the average values of a given array of except the largest and smallest values. The array length must be 3 or more. Go to the editor
Sample Output:

6
12
5
9

Click me to see the solution

32. Write a Ruby program to compute the sum of the numbers of a given array except the number 17 and numbers that come immediately after a 17. Return 0 for an empty array. Go to the editor
Sample Output:

8
9
10

Click me to see the solution

33. Write a Ruby program to check whether the sum of all the 3's of a given array of integers is exactly 9. Go to the editor
Sample Output:

true
false
false

Click me to see the solution

34. Write a Ruby program to check whether the number of 2's is greater than the number of 5's of a given array of integers. Go to the editor
Sample Output:

false
true
false

Click me to see the solution

35. Write a Ruby program to check whether every element is a 3 or a 5 in a given array of integers. Go to the editor
Sample Output:

true
false
true

Click me to see the solution

36. Write a Ruby program to check whether it contains no 3 or it contains no 5. Go to the editor
Sample Output:

true
[true, true]
false

Click me to see the solution

37. Write a Ruby program to check whether a given value appears everywhere in a given array. A value is "everywhere" in an array if it presents for every pair of adjacent elements in the array. Go to the editor
Sample Output:

true
false
false

Click me to see the solution

38. Write a Ruby program to check whether a given array contains a 3 next to a 3 or a 5 next to a 5, but not both. Go to the editor
Sample Output:

true
false
true

Click me to see the solution

39. Write a Ruby program to check whether a given array of integers contains two 6's next to each other, or there are two 6's separated by one element, such as {6, 2, 6}. Go to the editor
Sample Output:

true
true
false

Click me to see the solution

40. Write a Ruby program to check whether there is a 2 in the array with a 3 somewhere later in a given array of integers. Go to the editor
Sample Output:

true
true
true
false
false

Click me to see the solution

41. Write a Ruby program to check whether the value 2 appears in a given array of integers exactly 2 times, and no 2's are next to each other. Go to the editor
Sample Output:

true
true
false

Click me to see the solution

42. Write a Ruby program to convert an array into an index hash. Go to the editor
Sample Output:

Original array:
[10, 20, 30, 40]
Index Hash:
{10=>nil, 20=>nil, 30=>nil, 40=>nil}

Click me to see the solution

43. Write a Ruby program to find most occurred item in a given array. Go to the editor
Sample Output:

Original array:
[10, 20, 30, 40, 10, 10, 20]
Frequency of numbers:
{10=>3, 20=>2, 30=>1, 40=>1}

Click me to see the solution

44. Write a Ruby program to check whether all items are identical in a given array. Go to the editor
Sample Output:

Original array:
[10, 20, 30, 40, 10, 10, 20]
If all items are identical?
false
Original array:
[10, 10, 10]
If all items are identical?
true

Click me to see the solution

45. Write a Ruby program to search items start with specified string of a given array. Go to the editor
Sample Output:

Original array:
["abcde", "abdf", "adeab", "abdgse", "bdefa", "bacdef"]
Search items start with 'ab':
["abcde", "abdf", "abdgse"]
Search items start with 'b':
["bdefa", "bacdef"]

Click me to see the solution

46. Write a Ruby program to iterate an array starting from the last element. Go to the editor
Sample Output:

Original array:
[10, 20, 30, 40, 10, 10, 20]
Reverse array:
20
10
10
40
30
20
10

Click me to see the solution

47. Write a Ruby program to iterate over the first n elements of a given array. Go to the editor
Sample Output:

Original array:
["abcde", "abdf", "adeab", "abdgse", "bdefa", "bacdef"]
First 3 elements:
["abcde", "abdf", "adeab"]

Click me to see the solution

48. Write a Ruby program to sort a given array of strings by length. Go to the editor
Sample Output:

Original array:
["abcde", "abdf", "adeab", "abdgeee", "bdefa", "abc", "ab", "a", "bacdef"]
Sorted array of strings by length
["a", "ab", "abc", "abdf", "abcde", "adeab", "bdefa", "bacdef", "abdgeee"]

Click me to see the solution

Ruby Code Editor: