Write a Ruby program to check whether 7 appears as either the first or last element in a given array

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

Write a Ruby program to check whether 7 appears as either the first or last element in a given array. The array length must be 1 or more.

الأجوبة

def check_array(nums)
   return (nums[0] == 7 || nums[nums.length-1] == 7)
end
print check_array([1, 2, 7]),"\n"
print check_array([7, 1, 2, 3]),"\n"
print check_array([14, 7, 1, 2, 3])

Output:

true
true
false
هل كان المحتوى مفيد؟

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

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