Write a Ruby program to compute the sum of two arrays (length 3) and return the array which has the largest sum

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

Write a Ruby program to compute the sum of two arrays (length 3) and return the array which has the largest sum.

الأجوبة

def check_array(a, b)
    sum = (a[0]+a[1]+a[2])-(b[0]+b[1]+b[2])
	if(sum >= 0)
		return a
	end
	return b
end
print check_array([1, 3, 5], [2, 4, 4]),"\n"
print check_array([11, 3, 5], [21, 0, -4]),"\n"  

Output:

[2, 4, 4]
[11, 3, 5]
هل كان المحتوى مفيد؟

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

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