43
\$\begingroup\$

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 
\$\endgroup\$
3
  • \$\begingroup\$ What are the possible values of the elements of the array? \$\endgroup\$ Commented Dec 19, 2016 at 21:06
  • \$\begingroup\$ @RobertHickman a list of positive integers, within your language's standard int size \$\endgroup\$ Commented Dec 19, 2016 at 21:12
  • \$\begingroup\$ oh I see that in the question now. Oops and thanks. \$\endgroup\$ Commented Dec 19, 2016 at 21:12

46 Answers 46

1
2
1
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ You can save a byte by doing ?: in the first ternary. \$\endgroup\$ Commented Dec 29, 2020 at 15:16
1
\$\begingroup\$

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] 
\$\endgroup\$
6
  • \$\begingroup\$ Why is this non-competing? \$\endgroup\$ Commented Oct 1, 2019 at 1:49
  • \$\begingroup\$ @pppery It's non-competing because it uses the latest version of 05AB1E which was released after this challenge was posted \$\endgroup\$ Commented Oct 1, 2019 at 21:14
  • 1
    \$\begingroup\$ Such answers no longer need to be marked non-competing \$\endgroup\$ Commented Oct 1, 2019 at 21:26
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Oct 3, 2019 at 20:31
  • \$\begingroup\$ Nope, you're outgolfed by Dennis \$\endgroup\$ Commented Oct 3, 2019 at 20:31
1
\$\begingroup\$

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?
\$\endgroup\$
1
\$\begingroup\$

Python 3, Python 2 22 bytes

Simple and efficient ^^

lambda x:x[2:]==x[:-2] 

Try it online! (python 3)

Try it online! (python 2)

\$\endgroup\$
1
\$\begingroup\$

Vyxal ag, 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.

\$\endgroup\$
1
\$\begingroup\$

Thunno 2 M, 3 bytes

z€ạ 

Try it online!

Explanation

z€ạ # Implicit input z # Uninterleave the input # - push [input[0::2], input[1::2]] # (i.e. [even indices, odd indices]) € # For each inner list: ạ # Check if the elements are all equal # (i.e. [even indices all equal, # odd indices all equal] # Take the minimum of this list # - check if both were true # (i.e. the input was alternating) # Implicit output 
\$\endgroup\$
0
\$\begingroup\$

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 } } 
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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)) 
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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 
\$\endgroup\$
0
\$\begingroup\$

Perl, 27 bytes

Includes +1 for p

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

Add++ v5.1, 9 bytes

L~,Ñ+B]B= 

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

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? 
\$\endgroup\$
0
\$\begingroup\$

Pip, 8 bytes

$&$=*UWg 

Attempt This Online!

Explanation

$&$=*UWg­⁡​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁢​‎⁠⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌­ g ; ‎⁡List of command-line arguments UW ; ‎⁢Unweave into two alternating sublists $=* ; ‎⁣Fold each sublist on equality $& ; ‎⁤Fold those two results on logical AND 💎 

Created with the help of Luminespire.

\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.