Write a Ruby program to check whether the sequence of numbers 10, 20, 30 appears anywhere in a given array of integers

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

Write a Ruby program to check whether the sequence of numbers 10, 20, 30 appears anywhere in a given array of integers.

الأجوبة

def array102030(nums)
    idx = 0
    while idx < nums.length-2
        if nums[idx..idx+2] == [10,20,30]
            return true
        end
        idx += 1
    end
    return false
end
print array102030([10, 20, 30, 40, 50]),"\n"
print array102030([0, 10, 20, 30, 90]),"\n"
print array102030([10, 20, 50, 30, 70])

Output:

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

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

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