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

39 Answers 39

0
\$\begingroup\$

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!

\$\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
\$\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\$

Common Lisp, 62 bytes

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

Try it online!

\$\endgroup\$
0
\$\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\$
0
\$\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\$
  • \$\begingroup\$ Why is this non-competing? \$\endgroup\$ – pppery Oct 1 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\$ – Cowabunghole Oct 1 at 21:14
  • \$\begingroup\$ Such answers no longer need to be marked non-competing \$\endgroup\$ – pppery Oct 1 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\$ – Cowabunghole Oct 3 at 20:31
  • \$\begingroup\$ Nope, you're outgolfed by Dennis \$\endgroup\$ – pppery Oct 3 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.