Write a Ruby program to set 5 to 1 whether there is a 3 immediately followed by a 4 in a given array of integers (length 3)

  • برمجة
  • برمجة روبي

Write a Ruby program to set 5 to 1 whether there is a 3 immediately followed by a 4 in a given array of integers (length 3).

الأجوبة

def check_array(nums)
     num1 = nums[0], nums[1], nums[2]
	if(nums[0] == 3 && nums[1] == 5)
			num1[1] = 1;
	end		
	if(nums[1] == 3 && nums[2] == 5)
			num1[2] = 1;
	end		
	return num1;
end
print check_array([1, 3, 5]),"\n" 
print check_array([3, 5, 6]),"\n" 
print check_array([3, 9, 5]) 
Output:
[1, 3, 1]
[3, 1, 6]
[3, 9, 5]
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...