Ruby Array Exercises: Check whether a given array of integers contains 3 twice, or 5 twice
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.
Ruby Code:
def check_array(nums)
if(nums.length == 2)
if(nums[0] == 3 && nums[1] == 3)
return true
else
return (nums[0] == 5 && nums[1] == 5)
end
return false
end
end
print check_array([3, 3]),"\n"
print check_array([3, 6]),"\n"
print check_array([5, 9]),"\n"
print check_array([5, 5]),"\n"
print check_array([8, 9])
Output:
true false false true false
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check whether a given array of integers of length 2 does not contain a 6 or a 9.
Next: Write a Ruby program to set 5 to 1 if there is a 3 immediately followed by a 4 in a given array of integers (length 3).
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics