Sobes.tech
Middle

Dilim monotonmu? Monoton funksiya — bu, bir o'zgaruvchili funksiya bo'lib, u haqiqiy sonlar to'plamida aniqlangan va u yoki har doim (o'zining aniqlash doirasida) kamaymaydi, yoki har doim oshmaydi. {1,7} - ha {1,1} - ha {3,3,1} - ha {9,5,1} - ha {23,5,23} - yo'q

sobes.tech Süni İntellekt

AI-dan cavab

Bir siyahının monoton olub olmadığını müəyyən etmək üçün, ya artmayan (hər növbəti element əvvəlkindən kiçik deyil) və ya azalmayan (hər növbəti element əvvəlkindən böyük deyil) olub olmadığını yoxlamaq lazımdır.

Go dilində tam ədədlər siyahısının monotonluğunu yoxlayan nümunə funksiyası:

func isMonotonic(nums []int) bool {
    if len(nums) <= 1 {
        return true
    }

    increasing := true
    decreasing := true

    for i := 1; i < len(nums); i++ {
        if nums[i] > nums[i-1] {
            decreasing = false
        }
        if nums[i] < nums[i-1] {
            increasing = false
        }
    }

    return increasing || decreasing
}

İstifadə nümunəsi:

fmt.Println(isMonotonic([]int{1,7}))       // true
fmt.Println(isMonotonic([]int{1,1}))       // true
fmt.Println(isMonotonic([]int{3,3,1}))     // true
fmt.Println(isMonotonic([]int{9,5,1}))     // true
fmt.Println(isMonotonic([]int{23,5,23}))   // false