# 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? – Robert Hickman Dec 19 '16 at 21:06
• @RobertHickman a list of positive integers, within your language's standard int size – FlipTack Dec 19 '16 at 21:12
• oh I see that in the question now. Oops and thanks. – Robert Hickman Dec 19 '16 at 21:12

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

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

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

• Why is this non-competing? – pppery Oct 1 '19 at 1:49
• @pppery It's non-competing because it uses the latest version of 05AB1E which was released after this challenge was posted – Cowabunghole Oct 1 '19 at 21:14
• Such answers no longer need to be marked non-competing – pppery Oct 1 '19 at 21:26
• 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 Oct 3 '19 at 20:31
• – pppery Oct 3 '19 at 20:31