Finding the "GAP" in node values ? or next?

94 views Asked by At

Let say I have a nodes with values a multiples of 10. I want to find the first GAP in the values. Here is how I would do it in numpy :

 > np.where(np.diff([11,21,31,51,61,71,91]) > 10)[0][0] + 2
 > 4  i.e. 41

How would I do this in Cypher... ?

 match (n) where n.val % 10 = 1
 with n.val
 order by val ....???

I'm using RedisGraph.

PS> if no GAP it should return the next value i.e. biggest + 10, if possible !

1

There are 1 answers

0
sentientcabbage On

I'm not sure if this is the most performant solution, but you can accomplish this using a combination of collect() and list comprehensions:

MATCH (n) WHERE n.val % 10 = 1 WITH n.val AS val ORDER BY n.val // collect ordered vals
WITH collect(val) AS vals // combine vals into array
WITH vals, [idx IN range(0, size(vals) + 1) WHERE vals[idx + 1] - vals[idx] > 10] AS gaps // find first index with diff > 10
RETURN vals[gaps[0]] + 10 // return missing value

To additionally return the next-biggest value if no gaps are found, change the RETURN clause to use a CASE statement:

RETURN CASE size(gaps) WHEN 0 THEN vals[-1] + 10 ELSE vals[gaps[0]] + 10 END