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
- برمجة
- برمجة روبي
- 2021-08-31
- mhanasmh00489829403
الأجوبة
def check_array(nums)
no3pair = 1
no5pair = 1
i = 0;
while i < nums.length && (no3pair + no5pair != 0)
if(nums[i] == 3 && nums[i+1] == 3)
no3pair = 0
elsif(nums[i] == 5 && nums[i+1] == 5)
no5pair = 0
end
i = i + 1
end
return ((no3pair ^ no5pair) == 1)
end
print check_array([3, 3, 7, 5]),"\n"
print check_array([3, 8, 5, 9]),"\n"
print check_array([3, 7, 5, 5]),"\n"
Output:
true false true
القوائم الدراسية التي ينتمي لها السؤال
