Check sequential numbers

I have an array of m integers. I am looking for a method to check if elements of m sequential. Is there a way to check consecutive numbers?

I came up with this code designed to work when the length of the array is four:

 m.count == 4 && (m.max-m.min) == 3 

which incorrectly returns true for [1,1,1,4] or [0,0,0,3] .

+5
source share
5 answers

Enumerable has a really handy method called each_cons , which works like this:

 [1,2,3,4].each_cons(2).to_a # => [ [1, 2], [2, 3], [3, 4] ] 

That is, it gives each consecutive set of n elements. In our case, n is 2.

Of course, as the name implies, it returns an Enumerator, so can we associate it with other Enumerable methods such as all? :

 def four_consecutive?(arr) return false unless arr.size == 4 arr.each_cons(2).all? {|a, b| b == a + 1 } end four_consecutive?([2,3,4,5]) # => true four_consecutive?([2,2,2,5]) # => false four_consecutive?([1,2,3,4,5]) # => false 

This method has an advantage over others, because since all? closes the chain, as soon as the block returns false, it will only check numbers until it finds a pair that does not meet the condition ( b == a + 1 ). Of course, with just four elements, that doesn't really matter - unless you call this method thousands of times in a situation where performance matters.

+4
source

You can try the following:

 a == (a.min..a.max).to_a && a.count == 4 

This only works when the array is in ascending order. [3, 4, 5, 6] , but [4, 3, 5, 6] will not.

+4
source

If a is an array and n is the required size:

 def size_and_consecutive?(a, n) a == (a.first..(a.first+n-1)).to_a end size_and_consecutive? [3,4,5,6], 4 #=> true size_and_consecutive? [4,3,5,6], 4 #=> false size_and_consecutive? [3,4,5], 4 #=> false 
+3
source

The answer is based on a math problem for Sum of consecutive integers

 Sum = nāˆ—(n+1)/2 

code:

 def check_sum_match?(arr) m = arr.min - 1 n = arr.max sum1 = arr.inject{|sum, x| sum = sum + x} sum2 = (n*(n+1) - m*(m+1))/2 sum1 == sum2 end arr = [5,6,7,8] if arr.count == 4 && check_sum_match?(arr) puts 'Yes condition matches' else puts 'Invalid Array' end # valid arrays are # [4,6,5,7], [4,5,6,7], etc 

Indicative explanation:

enter image description here

+2
source

The compact solution I could come up with is as follows:

 def consec(arr) is_of_proper_length = (arr.size == 4) if(is_of_proper_length) are_consec = true arr.each_cons(2) {|x,y| are_consec = false unless ((y - x) == 1)} end is_of_proper_length && are_consec end 

Output:

 consec([1,2,3,4]) => true 2.2.0 :051 > consec([0,0,0,0]) => false 2.2.0 :052 > consec([4,6,5,7]) => true 2.2.0 :053 > consec([4,5,6,7]) => true 2.2.0 :054 > consec([5,6,7,8]) => true 2.2.0 :055 > consec([2,2,2,5]) => false 2.2.0 :056 > consec([2,3,4,5]) => true 2.2.0 :057 > consec([1,2,3,4,5]) => false 
0
source

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


All Articles