w3resource

Ruby Array Exercises: Check whether a given array of integers contains 3 twice, or 5 twice

Ruby Array: Exercise-19 with Solution

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 Array Exercises: Check whether a given array of integers contains 3 twice, or 5 twice

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:

Flowchart: Check whether a given array of integers contains 3 twice, or 5 twice

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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby-exercises/array/ruby-array-exercise-19.php