Check if the integers in the array are sequential or sequential

Is there a better way to check if array elements are in sequential order?

For instance:

[1,2,3,4,5] // returns true 
[1,2,4,3,5] // returns false

I am currently implementing to distinguish between elements, and if diff is 1, then I say that it is in sequential order.

I am looking for any improved approach. I am thinking of adding an extension to Array, but not sure how to implement this.

+2
source share
4 answers

Given your array

let list = [1,2,3,4,5]

you can use the magic of functional programming

let consecutives = list.map { $0 - 1 }.dropFirst() == list.dropLast()
+4
source

, for-fine , . -, , , , , .

" ". -, " ?"

extension Sequence {
    func all(pass predicate: (Element) -> Bool) -> Bool {
        // If nothing is false, everything is true
        return !self.contains(where: { !predicate($0) })
    }
}

, .

: :

extension Collection {
    func passesForConsecutiveValues(_ predicate:(Element, Element) -> Bool) -> Bool {
        return zip(self, dropFirst()).all(pass: predicate)
    }
}

zip(x, x.dropFirst() " ", : " ?" :

// Are all elements one more than their predecessor?
[1,2,4,5].passesForConsecutiveValues { $1 == $0 + 1 }  // true

, , , Sequence Collection . ? zip(x, x.dropFirst()) . . , ; " " . BLEH. Scala TraversableOnce , .

, Sequence. zip(x, x.dropFirst()). pairwise, :

extension Sequence {
    func pairwise() -> AnyIterator<(Element, Element)> {
        var it = makeIterator()
        guard var last_value = it.next() else { return AnyIterator{ return nil } }

        return AnyIterator {
            guard let value = it.next() else { return nil }
            defer { last_value = value }
            return (last_value, value)
        }
    }
}

Sequence:

extension Sequence {
    func passesForConsecutiveValues(_ predicate:(Element, Element) -> Bool) -> Bool {
        return pairwise().all(pass: predicate)
    }
}
+2

" , , diff 1, , .

, , , .

: , . for ... in ... where, , - , where , . :.

extension Array where Element == Int {
    func numbersAreConsecutive() -> Bool {
        for (num, nextNum) in zip(self, dropFirst())
            where (nextNum - num) != 1 { return false }
        return true
    }
}

var arr = [1, 2, 3, 4, 5]
print(arr.numbersAreConsecutive()) // true

arr = [1, 2, 4, 5]
print(arr.numbersAreConsecutive()) // false

arr = [1]
print(arr.numbersAreConsecutive()) // true

arr = []
print(arr.numbersAreConsecutive()) // true

arr = [2, 1]
print(arr.numbersAreConsecutive()) // false

, Integer:

extension Array where Element: Integer {
    func numbersAreConsecutive() -> Bool {
        for (num, nextNum) in zip(self, dropFirst())
            where (nextNum - num) != 1 { return false }
        return true
    }
}
+1

true, , false

 1.Checking whether the array is sequence(Find the array is sequence)

    1.1 Sortedarray[0] + arraycount multiple with sequence (1,2,3, etc) and minus the sequence.
    1.2 compare the above calculated value with last value of sorted array. if it matche we could consider The array is sequence.

 2. Compare the source array and sorted array to confirm it is in order


 isSeq([4,5,6,7],sequence:1) **return True**
isSeq([100,102,104,106,108],sequence:2) **return True**
    isSeq([100,103,106,109,110],sequence:3) **return false**

    func isSeq(_ arrayValue:[Int],sequence:Int) ->Bool{

        let sortedValue = arrayValue.sorted()

        if(sortedValue[0] + (sortedValue.count * sequence) - sequence == sortedValue[sortedValue.count - 1]){
            if(arrayValue == sortedValue){
                return true
            }
        }

        return false;
    }
0

Source: https://habr.com/ru/post/1695308/


All Articles