An Array of Challenges #1: Alternating Arrays

41

2

Alternating Arrays

An alternating array is a list of any length in which two (not necessarily different) values are alternating. That is to say, all even-indexed items are equal, and all odd-indexed items are equal.

Your task is to write a program or function which, when given a list of positive integers, outputs/returns truthy if it is alternating and falsy otherwise.

This is , so the shortest code (in bytes) wins!

Edge Cases:

[]      ->  True
[1]     ->  True
[1,1]   ->  True
[1,2,1] ->  True

Other Test Cases:

[1,2,1,2]      -> True
[3,4,3]        -> True
[10,5,10,5,10] -> True
[10,11]        -> True
[9,9,9,9,9]    -> True

[5,4,3,5,4,3]   -> False
[3,2,1,2,1,2]   -> False
[1,2,1,2,1,1,2] -> False
[2,2,3,3]       -> False
[2,3,3,2]       -> False

Example

Here is an example you can test your solution against, written in Python 3 (not golfed):

def is_alternating(array):
    for i in range(len(array)):
        if array[i] != array[i%2]:
            return False
    return True

FlipTack

Posted 2016-12-18T17:51:06.027

Reputation: 13 242

What are the possible values of the elements of the array? – Robert Hickman – 2016-12-19T21:06:31.787

@RobertHickman a list of positive integers, within your language's standard int size – FlipTack – 2016-12-19T21:12:16.943

oh I see that in the question now. Oops and thanks. – Robert Hickman – 2016-12-19T21:12:54.620

Answers

27

Jelly, 4 bytes

ḣ2ṁ⁼

Try it online!

How it works

ḣ2ṁ⁼  Main link. Argument: A (array)

ḣ2    Head 2; truncate A after its second element. If A has two or less elements,
      this returns A itself.
  ṁ   Mold; cyclically repeat the elements of the previous result to create an
      array that has the same shape/length as A.
   ⁼  Test the result for equality with A.

Dennis

Posted 2016-12-18T17:51:06.027

Reputation: 196 637

7Damn. And changing the 2 to other numbers immediately generalizes the challenge! – Greg Martin – 2016-12-18T19:27:13.553

3 bytes, but Ɲ didn't exist when the challenge was posted. – caird coinheringaahing – 2018-04-14T19:54:20.487

14

brainfuck, 34 bytes

,>,>+>,
[
  [<+<<->>>-]
  +<[-<<]
  >[>]
  ,
]
<.

Takes the array as byte values in a string, and outputs \x00 for false and \x01 for true.

Try it online.

This maintains the structure

a b 1 c

on the tape, where c is the current character, b is the previous character, and a is the previous previous character, as long as the array is alternating. If a mismatch is found, the pointer is moved to the left such that a, b, and the 1 flag all become zero, and this situation will continue until all the input is consumed.

Mitch Schwartz

Posted 2016-12-18T17:51:06.027

Reputation: 4 899

13

R, 24 23 bytes

all((a=scan())==a[1:2])

Reads a vector into STDIN, takes the first two elements of that vector, and checks equality. If the lengths of a[1:2] and a don't match, R will loop through a[1:2] to match the length of a. It will give a warning about doing so, but it will work.

Surprisingly this even works for empty input, not sure why, but I'll roll with it.

Saved 1 byte thanks to @MickyT

JAD

Posted 2016-12-18T17:51:06.027

Reputation: 2 898

you can save yourself a byte with all((a=scan())==a[1:2]) – MickyT – 2016-12-18T20:36:25.907

How do you input the data, as vector, list or just single numbers? I've tried typing single numbers on the console but I get the warning: "Warning message: In scan() == a[1:2] : longer object length is not a multiple of shorter object length". Though it works. – skan – 2016-12-19T00:43:11.153

By typing single numbers indeed. It will throw a warning if the input length is odd, but it will still give the correct output. – JAD – 2016-12-19T08:37:27.377

10

MATL, 7 6 bytes

2YCs&=

For alternating arrays this outputs a non-empty matrix of ones, which is truthy. For non-alternating arrays the matrix contains at least one zero, and is thus falsy (see here).

Try it online! Or verify all test cases.

Explanation

Let's take [1 2 1 2] as example input.

2YC   % Implicit input. Build matrix whose columns are overlapping blocks of 
      % length 2. If input has size less than 2 this gives an empty array
      % STACK: [1 2 1;
                2 1 2]
s     % Sum of each column. For an empty array this gives 0
      % STACK: [3 3 3]
&=    % Matrix of all pairwise equality comparisons. Implicit display
      % STACK: [1 1 1;
                1 1 1;
                1 1 1]

Luis Mendo

Posted 2016-12-18T17:51:06.027

Reputation: 87 464

2Nice algorithm! This would make a mean Jelly answer. – Dennis – 2016-12-18T20:11:52.240

@Dennis Thanks! It was partly inspired by your Jelly approach – Luis Mendo – 2016-12-18T20:53:43.923

9

JavaScript (ES6), 27 bytes

a=>!a.some((v,i)=>a[i&1]-v)

Test cases

let f =

a=>!a.some((v,i)=>a[i&1]-v)

console.log(f([]));              // -> True
console.log(f([1]));             // -> True
console.log(f([1,1]));           // -> True
console.log(f([1,2,1]));         // -> True
console.log(f([1,2,1,2]));       // -> True
console.log(f([10,5,10,5,10]));  // -> True
console.log(f([10,11]));         // -> True
console.log(f([9,9,9,9,9]));     // -> True
console.log(f([5,4,3,5,4,3]));   // -> False
console.log(f([3,2,1,2,1,2]));   // -> False
console.log(f([1,2,1,2,1,1,2])); // -> False
console.log(f([2,2,3,3]));       // -> False
console.log(f([2,3,3,2]));       // -> False

Arnauld

Posted 2016-12-18T17:51:06.027

Reputation: 111 334

8

Retina, 25 bytes

M`\b(\d+),\d+,(?!\1\b)
^0

Try it online!

Instead of matching an input with alternating values (which leads to some annoying edge effects in a regex), I'm matching inputs that aren't valid and then negate the result afterwards.

The benefit of matching an invalid input is that this is a property can be checked locally, and it doesn't need to treat empty or short input specially: any input is invalid if it contains two distinct values that are one position apart.

So the first stage counts the number of matches of \b(\d+),\d+,(?!\1\b) which matches and captures one value, then matches the next value, and then asserts that the third value in sequence is different. This gives zero for valid inputs and something positive for invalid values.

The second stage simply counts the number of matches of ^0 which is 1 if the first stage returned 0 and 1 otherwise.

Martin Ender

Posted 2016-12-18T17:51:06.027

Reputation: 184 808

7

Mathematica, 29 bytes

#=={}||Equal@@(Most@#+Rest@#)&

A port of Luis Mendo's MATL algorithm. Unnamed function taking a list of numbers (or even more general objects) and returning True or False. Tests whether sums of consecutive elements are all equal. Unfortunately Most and Rest choke on the empty list, so that has to be tested separately.

Mathematica, 33 bytes

Differences[#,1,2]~MatchQ~{0...}&

Unnamed function taking a list of numbers (or even more general objects) and returning True or False. The function Differences[#,1,2] takes the differences, not of consecutive pairs of integers, but pairs of integers at distance two apart. Then we just check whether the resulting list has nothing other than zeros in it.

As a bonus, for one more byte (change the 2 to #2), we get a function that inputs a list of integers and another positive integer #2, and checks whether the input list is the result of interleaving #2 constant sequences periodically with one another. For example,

Differences[#,1,#2]~MatchQ~{0...}&[{1,2,3,4,5,1,2,3,4,5,1,2},5]

evaluates to True.

Greg Martin

Posted 2016-12-18T17:51:06.027

Reputation: 13 940

7

Retina, 39 32 28 bytes

^(\d*)((,\d+)(,\1(\3|$))*)?$

Try it online!

Saved 7 bytes thanks to Martin! Saved another 3 thanks to Kobi! And to Kritixi for an idea for another 1.

We optionally match a number that occupies the entire input, any pair of numbers, or any pair of numbers followed by the same pair any number of times and optionally not including the second number at the very end. Could save 2 bytes if the input was in unary.

FryAmTheEggman

Posted 2016-12-18T17:51:06.027

Reputation: 16 206

1

Another ^(\d+)?(.\d+)?(.\1\2)*(.\1)?$ 29 byte alternative. This does not match ,1,,1.

– user41805 – 2016-12-19T11:51:39.153

1@Kobi Great idea, thanks! I used some of Kritixi's answer (the addition of the comma to the second capture group) to save another 1! – FryAmTheEggman – 2016-12-19T18:52:20.530

7

Haskell, 27 26 bytes

and.(zipWith(==)=<<drop 2)

This evaluates to an anonymous function that solves the challenge. The idea is to drop the first two numbers from the list, zip with the original list using equality, and check that the result only contains Trues. Try it online!

Thanks to nimi for 1 byte!

Zgarb

Posted 2016-12-18T17:51:06.027

Reputation: 39 083

@Flp.Tkc It just needs a type hint

– Zgarb – 2016-12-18T21:24:16.603

1Nice. and.(zipWith(==)=<<drop 2) saves a byte. – nimi – 2016-12-18T21:39:05.857

6

Pyth, 9 bytes

q<*<Q2lQl

Explanation

q<*<Q2lQlQQ   Implicitly add enough Qs to make the code run

   <Q2        Take the first two elements of the input
  *   lQ      Repeat those len(input) times
 <      lQ    Take the first len(input) elements
q         Q   Check if those are equal to the input

user48543

Posted 2016-12-18T17:51:06.027

Reputation:

you might want to update the code in the explanation (it's different atm) – FlipTack – 2016-12-19T22:07:02.500

@Flp.Tkc Pyth implicitly adds Qs to the code. I added them in the explanation to make it clearer what was going on, but they aren't really in the code. – None – 2016-12-19T22:09:34.433

5

Brachylog, 15 bytes

:{~c#Tbh#Co}f#=

Try it online!

Explanation

:{         }f       Find all results of the predicate below
             #=     They must all be equal

  ~c#T              Deconcatenate the input into three lists
      bh#C          The middle list has two elements
        #Co         Order that couple of elements as the output

Fatalize

Posted 2016-12-18T17:51:06.027

Reputation: 32 976

5

Perl 6,  49 43  42 bytes

{!grep {![==] @_},roundrobin |.rotor: 2,:partial}

Try it

{!.grep: ->\a,\b=$_[1] {sum .[0,1]Z!==a,b}}

Try it

{!.grep: ->\a,\b=.[1] {sum .[0,1]Z!==a,b}}

Try it

Expanded:

{
  !              # invert

  .grep:         # find any mismatches

  ->
    \a,
    \b = .[1]   # optional second parameter with default of second value from input
  {
    sum          # count up the values that don't match

    .[ 0, 1 ]    # the first two values from the input
    Z[![==]]     # zip not equal
    a, b         # the current two values under test.
  }
}

Brad Gilbert b2gills

Posted 2016-12-18T17:51:06.027

Reputation: 12 713

$_[1] can be one byte shorter as .[1]. The body of the inner lambda can be one byte shorter as {.[0]!=a||.[1]!=b}. – smls – 2016-12-19T18:19:29.257

1@smls I have no idea why I didn't see the .[1]. Also != doesn't seem to work if it isn't followed by a space. I think something like $_!=3 is being parsed as if it was written as !( $_ = 3 ) – Brad Gilbert b2gills – 2016-12-20T01:46:21.107

Ah. Looks like it's a Rakudo bug.

– smls – 2016-12-20T02:02:50.233

5

APL, 7 bytes

⊢≡⍴⍴2⍴⊢

Explanation:

  • 2⍴⊢: reshape the input array by 2
  • ⍴⍴: reshape the result by the original size of the input, repeating elements
  • ⊢≡: see if the result of that is equal to the original input

Test cases:

      true←(1 2 1 2)(10 5 10 5 10)(10 11)(9 9 9 9 9)
      false←(5 4 3 5 4 3)(3 2 1 2 1 2)(1 2 1 2 1 1 2)(2 2 3 3)(2 3 3 2)
      ( ⊢≡⍴⍴2⍴⊢ ) ¨ true
1 1 1 1
      ( ⊢≡⍴⍴2⍴⊢ ) ¨ false
0 0 0 0 0

marinus

Posted 2016-12-18T17:51:06.027

Reputation: 30 224

5

Java 8, 63 bytes

i->{int r=0,x=1;for(;++x<i.length;)r|=i[x]-i[x-2];return r==0;}

This is a lambda expression for a Predicate< int[ ] >

Explanation: initialize the result to 0. For each element, Biteise OR the result with the difference between the current element and the element 2 indicies earlier. return true if the result equals 0. Otherwise return false

Jack Ammo

Posted 2016-12-18T17:51:06.027

Reputation: 430

4

Python 2, 35 bytes

lambda x:(x[:2]*len(x))[:len(x)]==x

Try it online!

Dennis

Posted 2016-12-18T17:51:06.027

Reputation: 196 637

3

Haskell, 33 32 bytes

f(a:x@(_:b:_))=a==b&&f x
f a=1<3

Try it online! or Verify the testcases. -1 byte thanks to Zgarb.

Laikoni

Posted 2016-12-18T17:51:06.027

Reputation: 23 676

@Dennis The function works for [], but for some reason ghc cannot infer the correct type for []. It works if tested together with the other test case, see Verify the testcases.

– Laikoni – 2016-12-18T20:34:07.820

Right, I don't know Haskell that well. – Dennis – 2016-12-18T20:37:04.600

Save a byte with f(a:x@(_:b:_))=a==b&&f x – Zgarb – 2016-12-18T21:05:51.063

3

bash, 56 54 38 bytes

[ -z $3 ]||((($1==$3))&&(shift;$0 $*))

Save this as a script, and pass the list of numbers as arguments (for an n-element list, you'll pass n arguments). The output is the exit code: 0 (for true) if the list is alternating, and 1 (for false) otherwise.

(Returning output in the exit code is allowed in the PPCG standard I/O methods.)

This works recursively:

  • If the list has fewer than 3 elements, then exit with return code 0;
  • else if the 1st element != the 3rd element, then exit with return code 1;
  • else run the program recursively on the list with the first element removed.

Mitchell Spector

Posted 2016-12-18T17:51:06.027

Reputation: 3 392

3

J, 8 bytes

-:$$2&{.

Explanation

-:$$2&{.  input: (y)
    2&{.  the first two elements of y
   $      shaped like
  $       the shape of y
-:        and check if they match

Test cases

   f =: -:$$2&{.
   ]true =: '' ; 1 ; 1 1 ; 1 2 1 ; 1 2 1 2 ; 10 5 10 5 10 ; 10 11 ; 9 9 9 9 9
++-+---+-----+-------+------------+-----+---------+
||1|1 1|1 2 1|1 2 1 2|10 5 10 5 10|10 11|9 9 9 9 9|
++-+---+-----+-------+------------+-----+---------+
   f each true
+-+-+-+-+-+-+-+-+
|1|1|1|1|1|1|1|1|
+-+-+-+-+-+-+-+-+
   ]false =: 5 4 3 5 4 3 ; 3 2 1 2 1 2 ; 1 2 1 2 1 1 2 ; 2 2 3 3 ; 2 3 3 2
+-----------+-----------+-------------+-------+-------+
|5 4 3 5 4 3|3 2 1 2 1 2|1 2 1 2 1 1 2|2 2 3 3|2 3 3 2|
+-----------+-----------+-------------+-------+-------+
   f each false
+-+-+-+-+-+
|0|0|0|0|0|
+-+-+-+-+-+

Conor O'Brien

Posted 2016-12-18T17:51:06.027

Reputation: 36 228

You should be able to replace {. Take with $ Shape. – Adám – 2017-11-07T10:59:53.580

1

Python 2.7, 38 bytes

>> i=lambda a:(a[:2]*len(a))[0:len(a)]==a

Test Cases:

>> print i([1,2,1,2])
>> True
>> print i([10,5,10,5,10]
>> True
>> print i([5,4,3,5,4,3])
>> False
>> print i([3,2,1,2,1,2])
>> False

Falcon

Posted 2016-12-18T17:51:06.027

Reputation: 31

2

I'd call this a duplicate of this answer.

– mbomb007 – 2016-12-19T17:27:59.787

1

Pyke, 6 bytes, noncompeting

2<Ql{q

Try it here!

2<     -   inp[:2]
    {  -  reshape(^, v)
  Ql   -   len(inp)
     q - ^ == inp

Allow reshape node to take a list as well as a string

Blue

Posted 2016-12-18T17:51:06.027

Reputation: 26 661

1

Shenzen IO (Assembler) ,83 76 bytes, noncompeting

Shenzen io is a puzzle game where you can code you code in a special assembler-ish language.

Unfortunately, you can only use integers between -999 and 999 as inputs or outputs, and there is no way to tell if an array has ended. So i assumed that the array was written on a ROM that wraps around after reading the last cell. This means that only even arrays can be used, which is the reason for it being noncompeting.

Code:

@mov x0 dat
@mov x0 acc
teq x0 dat
+teq x0 acc
b:
+mov 1 p1
-mov 0 p1
-jmp b

Explanation:

  # calling for x0 will cause rom to move 1 cell forward

 @ mov x0 dat # Moves value to variable dat (only run once)
 @ mov x0 acc # Moves rom position forward and moves x0 to acc           
  teq x0 dat  # See if dat equals x0  
+ teq x0 acc  # If last expression was true, see x0 equals acc
b:            # Label for jumps (GOTO)
+ mov 1 p1    # Set output (p1) to 1 (same case as previous line)
- mov 0 p1    # if any expression was false, set output to 0 
- jmp b       # jump to b: (same case as prev line)

Sorry if any of this is confusing, this is my first code-golf answer.

EDIT: removed 7 bytes by replacing loops by run-once code

Anamne

Posted 2016-12-18T17:51:06.027

Reputation: 11

Welcome to PPCG! – FlipTack – 2016-12-20T19:01:56.373

1

Ruby, 131 119 bytes

a=->x{!(x.values_at(*x.each_index.select{|i|i.even?}).uniq)[1]&!(x.values_at(*x.each_index.select{|i|i.odd?}).uniq[1])}

Lambda a expects an array x and returns true if there are 0 or 1 unique values for the odd indexed elements and 0 or 1 unique values for the even indexed elements in the array.

Notable byte-safers

  • use of lambda over def
  • !arr[1] vs. arr.length < 2
  • & vs &&

Test cases

p a[[]]
p a[[1]]
p a[[1,1]]
p a[[1,2,1]]
p a[[1,2,1,2]]
p a[[3,4,3]]
p a[[10,5,10,5,10]]
p a[[10,11]]
p a[[9,9,9,9,9]]

#false
p a[[5,4,3,5,4,3]]==false
p a[[3,2,1,2,1,2]]==false
p a[[1,2,1,2,1,1,2]]==false
p a[[2,2,3,3]]==false
p a[[2,3,3,2]]==false

manonthemat

Posted 2016-12-18T17:51:06.027

Reputation: 141

1

Ruby, 23 bytes

->a{a[2..-1]==a[0..-3]}

G B

Posted 2016-12-18T17:51:06.027

Reputation: 11 099

1

Dart, 46 bytes

(l){var i=0;return l.every((x)=>x==l[i++%2]);}

Run with:

void main() {
  var f = (l){var i=0;return l.every((x)=>x==l[i++%2]);};
  print(f([1,2,1,2,1]));
}

Dwayne Slater

Posted 2016-12-18T17:51:06.027

Reputation: 111

1

C#, 54 bytes

using System.Linq;p=>!p.Where((v,i)=>v!=p[i%2]).Any();

Filter array to show values that do not match the first value for evens and the 2nd value for odds. If there are not any results, return true.

Grax32

Posted 2016-12-18T17:51:06.027

Reputation: 1 282

1

Japt, 7 6 bytes

eUîU¯2

Try it or run all test cases

           :Implicit input of array U
   U¯2     :Get the first 2 elements of U
 Uî        :Repeat that array to the length of U
e          :Test for equality with the original U

Shaggy

Posted 2016-12-18T17:51:06.027

Reputation: 24 623

0

Octave, 51 bytes

@(L)numel(L)<3||(f=@(n)isequal(L{n:2:end}))(1)&f(2)

Input is a cell array of positive integers.

Try it online!

rahnema1

Posted 2016-12-18T17:51:06.027

Reputation: 5 435

0

C#, 66 bytes

a=>{int r=1,i=0;for(;i<a.Length;)if(a[i]!=a[i++%2])r=0;return r;};

Anonymous function which receives an integer array and returns 1 if array is alternating and 0 otherwise.

Full program with ungolfed function and test cases:

using System;

public class Program
{
    public static void Main()
    {
        Func<int[], int> f =
        a =>
        {
            int r = 1,  // return value. 1 is true, by default
                i = 0;  // iterator
            for ( ; i<a.Length ; )  // for each array element
                if ( a[i] != a[i++%2] ) // if the even (or odd) elements are not the same
                    r = 0;      // a falsy (0) value will be assigned to the return element
            return r;       // returning if the array is alternating or not
        };

        // test cases:
        Console.WriteLine("Edge cases (all TRUE):");
        Console.WriteLine(f(new int[]{}));      //  True
        Console.WriteLine(f(new int[]{1}));     //  True
        Console.WriteLine(f(new int[]{1,1}));   //  True
        Console.WriteLine(f(new int[]{1,2,1})); //  True

        Console.WriteLine("Some other TRUE test cases:");
        Console.WriteLine(f(new int[]{1,2,1,2}));      // True
        Console.WriteLine(f(new int[]{10,5,10,5,10})); // True
        Console.WriteLine(f(new int[]{10,11}));        // True
        Console.WriteLine(f(new int[]{9,9,9,9,9}));    // True

        Console.WriteLine("Some FALSE test cases:");
        Console.WriteLine(f(new int[]{5,4,3,5,4,3}));   // False
        Console.WriteLine(f(new int[]{3,2,1,2,1,2}));   // False
        Console.WriteLine(f(new int[]{1,2,1,2,1,1,2})); // False
        Console.WriteLine(f(new int[]{2,2,3,3}));       // False
        Console.WriteLine(f(new int[]{2,3,3,2}));       // False
    }
}

adrianmp

Posted 2016-12-18T17:51:06.027

Reputation: 1 592

0

Clojure, 70 bytes

(fn[c](let[n #(max(count(set(take-nth 2 %)))1)](=(n c)(n(rest c))1))))

Checks that the distinct count of every 2nd item is 1, and handles empty collections as a special case. Also tried many approaches based on reduce and group-by but not much luck there.

NikoNyrh

Posted 2016-12-18T17:51:06.027

Reputation: 2 361

0

Another option with R: 36 bytes.

all(rep_len(head(x,2),length(x))==x)

And I think I've found a much shorter version: 15 bytes

all(!diff(x,2))

skan

Posted 2016-12-18T17:51:06.027

Reputation: 101

0

C,  52   50   49  47 bytes

Thanks to @ceilingcat for golfing two bytes!

f(i,l)int*i;{return--l<2?1:*i-i[2]?0:f(++i,l);}

Outputs 1 if the array alternates, 0 otherwise.

Try it online!

Steadybox

Posted 2016-12-18T17:51:06.027

Reputation: 15 798

0

Jelly, 6 bytes

My first Jelly program! I know I'll never beat Dennis, but I'm awfully proud of this regardless.

s2ZE€Ạ // Main link: Argument A (array)
s2     // Split A into chunks of 2 
       // [1,2,1,2] -> [[1,2],[1,2]]
  Z    // Transpose A   
       // [[1,2],[1,2]] -> [[1,1],[2,2]]
   E€  // Map an equality check to each sublist of A
       // [[1,1],[2,2]] -> [1,1]
     Ạ // Any: return 0 if A contains any falsy values, else return 1
       // [1,1] -> 1

Try it here!

Xanderhall

Posted 2016-12-18T17:51:06.027

Reputation: 1 236

0

PHP, 52 bytes

Run with -r.

foreach($argv as$i=>$n)$i<3|$n==$argv[$i-2]?:die(1);

loops from 3rd to last argument. Continue while argument equals that two positions earlier, else exit with code 1 (error). Exit with 0 (ok) after loop finishes.

or for 53 bytes:

while(++$i<$argc-3&$f=$argv[$i]==$argv[2+$i]);echo$f;

loops through all arguments but the last two. Continue while current argument equals that two positions later. Print 1 for true, nothing for false (string representations of true and false).

Titus

Posted 2016-12-18T17:51:06.027

Reputation: 13 814

0

Oracle SQL, 73 Bytes

select count(*)from(select a,lag(a,2)over(order by 1)b from t)where a!=b;

Output:

True  =  0
False != 0

True Example:

create table t (a number);
truncate table t;
insert into t values (10);
insert into t values (5);
insert into t values (10);
insert into t values (5);
insert into t values (10);
commit;

select count(*)from(select a,lag(a,2)over(order by 1)b from t)where a!=b;

  COUNT(*)
----------
         0

False Example:

create table t (a number);
truncate table t;
insert into t values (1);
insert into t values (2);
insert into t values (3);
insert into t values (3);
insert into t values (1);
insert into t values (2);
commit;

select count(*) from(select a,lag(a,2)over(order by 1)b from t)where a!=b;


  COUNT(*)
----------
         4

Giacomo Garabello

Posted 2016-12-18T17:51:06.027

Reputation: 1 419

0

Perl, 27 bytes

Includes +1 for p

perl -pE '$_=!/\b(\d+) \d+ (?!\1\b)/' <<< "1 2 1 2"

Ton Hospel

Posted 2016-12-18T17:51:06.027

Reputation: 14 114

0

Add++ v5.1, 9 bytes

L~,Ñ+B]B=

Try it online!

caird coinheringaahing

Posted 2016-12-18T17:51:06.027

Reputation: 13 702

0

Perl 6, 30 bytes

2>*[(0,2...*;1,3...*)].all.Set

Try it online!

Anonymous Whatever lambda that takes a list and returns an all Junction that boolifies to True/False.

Explanation:

  *[(       ;       )]          # Index from the list
     0,2...*                    # The even indexed elements
             1,3...*            # The odd indexed elements
                      .all      # Are both the lists
                          .Set  # When converted to a set
2>                              # The length is smaller than 2?

Jo King

Posted 2016-12-18T17:51:06.027

Reputation: 38 234

0

Common Lisp, 62 bytes

(defun f(l)(or(not #1=(caddr l))(and(=(car l)#1#)(f(cdr l)))))

Try it online!

Renzo

Posted 2016-12-18T17:51:06.027

Reputation: 2 260

0

05AB1E, 5 bytes

2∍s∍Q

Try it online!

Explanation:

2∍s∍Q   //Full Program
2∍      //extend/shorten input to length 2       e.g. [1,2,1,2,2] -> [1,2]
  s∍    //extend/shorten to length of input      e.g. [1,2] -> [1,2,1,2,1]
    Q   //is equal to input                      e.g. [1,2,1,2,1] != [1,2,1,2,2]

Cowabunghole

Posted 2016-12-18T17:51:06.027

Reputation: 1 590

Why is this non-competing? – pppery – 2019-10-01T01:49:40.640

@pppery It's non-competing because it uses the latest version of 05AB1E which was released after this challenge was posted – Cowabunghole – 2019-10-01T21:14:45.357

Such answers no longer need to be marked non-competing – pppery – 2019-10-01T21:26:51.107

Huh, TIL. Thanks for the update! Maybe now that I'm competing my 1 year old answer can win this 3 year old challenge :P – Cowabunghole – 2019-10-03T20:31:00.827

Nope, you're outgolfed by Dennis

– pppery – 2019-10-03T20:31:53.503

Lol I know, I was just making a joke. I forgot I had even posted this until you commented on it! – Cowabunghole – 2019-10-03T21:19:12.653