# 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

• What are the possible values of the elements of the array? Dec 19 '16 at 21:06
• @RobertHickman a list of positive integers, within your language's standard int size Dec 19 '16 at 21:12
• oh I see that in the question now. Oops and thanks. Dec 19 '16 at 21:12

# Vyxalag, 5 4 bytes

-1 byte because flag.

yWv≈


Try it Online!

Explanation:

       # 'a' flag - treat newline separated inputs as a list
y      # Uninterleave
W     # Wrap resulting lists together into a list
v≈   # On each list: are all elements the same?
# 'g' flag - Output minimum value of the top of the stack


Essentially, given <a|b|a|b>, separate into <a|a> and <b|b>, then check if a==a and b==b. If either of those return 0, print 0. Otherwise, print 1.

# 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
}
}


# 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!

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.

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))


# 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). # 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  # Perl, 27 bytes Includes +1 for p perl -pE '$_=!/\b(\d+) \d+ (?!\1\b)/' <<< "1 2 1 2"


L~,Ñ+B]B=


Try it online!

# 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?


# Common Lisp, 62 bytes

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


Try it online!

# 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!

• You can save a byte by doing ?: in the first ternary. Dec 29 '20 at 15:16

# Factor, 42 bytes

[ dup <evens> std swap <odds> std + 0. = ]


Try it online!

• dup <evens> std Take the standard deviation of the even indices.
• swap <odds> std Take the standard deviation of the odd indices.
• + 0. = Is their sum zero?