An Array of Challenges #1: Alternating Arrays
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.
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.
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