Determine if an Array contains something other than 2
Python 2, 43 40 bytes
f=lambda l:l>=[]and all(map(f,l))or l==2
Try it online!
At time of posting this answer, it was still allowed per this meta consensus to output via throwing an error / not throwing an error. Therefore this answer at 26 bytes was valid:
f=lambda l:l==2or map(f,l)
Try it online!
Prolog (SWI), 43 33 bytes
I smell... recursion.
Thanks to Emigna and Leaky Nun for saving 10 bytes!
Code
a([]).
a([X|T]):-(X=2;a(X)),a(T).
Try it online! or Verify all test cases!
Explanation:
For non-Prolog users, a list is formatted in the following way: [Head | Tail]
.
The Head
is the first element of the list, and tail is the remaining list. Test it here!. An important case here is that the tail of a list with 1 element is equal to []
. You can test that here.
% State that an empty array is truthy.
a([]).
% If the list is not empty (covered by the previous line), we need to check
% whether the Head is equal to 2 or whether the head is truthy.
% After that, we only need to check if the remaining list is truthy.
a([Head | Tail]) :- (Head = 2; a(Head)), a(Tail).
Jelly, 4 bytes
F;2E
Try it online!
How it works
F;2E
F flatten
;2 append 2
E all elements are equal