/* Duplicate neighbours in Picat. https://twitter.com/madeofmistak3/status/1263932176946479106 """ Friday code challenge: write the shortest/most elegant/most clever function that identifies if a list has two repeated, neighboring elements. e.g. yes: [1,7,3,4,4,0] and [0,0,0,0,0] no: [1], [1,2,6,9], [1,2,1,2,1,2,1] use any language you want. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp,util. main => go. go ?=> tests(Pos,Neg), Preds = [check1,check2,check3,check4], foreach(P in Pos) println(p=P), foreach(Pred in Preds) if call(Pred,P) then println(Pred=ok) else println(Pred=not_ok) end end, nl end, nl, foreach(N in Neg) println(n=N), foreach(Pred in Preds) if not call(Pred,N) then println(Pred=ok) else println(Pred=not_ok) end end, nl end, nl. go => true. tests(Pos,Neg) => Pos = [[1,7,3,4,4,0], [0,0,0,0,0],[1,2,3,4,4,5]], Neg = [[1], [1,2,6,9], [1,2,1,2,1,2,1]]. check1(L) => nextto(X,X,L). check2(L) => once(append(_,[X,X],_,L)). check3(L) => drop(L,1)=L2,take(L,L.len-1)=L3,[1:{I,I}in zip(L2,L3)].len>0. check4(L) => sum([1:I in 2..L.len,L[I]=L[I-1]])>0.