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}
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer