Q:

Write a Ruby program to check whether a given array of integers contains two 6's next to each other,

0

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}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def check_array(nums)
   i = 0;
   while i < nums.length
        if(nums[i] == 6)
    		if(nums[i+1] == 6)
				return true
			elsif(i < nums.length - 2 && nums[i+2] == 6)
				return true
			end	
	    end
	i = i + 1
   end
   return false
end
print check_array([6, 3, 6, 5]),"\n"
print check_array([6, 6, 5, 9]),"\n"
print check_array([6, 4, 5, 6]),"\n

Output:

true
true
false

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now