Take an array which consists of positive integers or arrays, output if it only contains 2s.
Output should be a truthy or falsey value (Sorry if this destroys answers)
Truthy Test Cases
[2]
[2,2]
[[2],[2,2],2]
[]
[[],[]]
Falsey Test Cases
[1]
[22]
[2,2,2,1]
[[1,2],2]
Standard Loopholes Are forbidden.
Default IO rules apply.
Code-golf, Fewest bytes wins!
58 Answers 58
Python 2, (削除) 44 (削除ここまで) (削除) 43 (削除ここまで) 42 bytes
Takes x as the string representation of the list. This also assumes like in the example the representations have no spaces.
lambda x:set(x)<=set("[],2"*0**("22"in x))
Explanation
Both of these take the characters in the string representation of the input and determine if any characters other than [], 2 are in it. They do this by casting to a set and comparing to the set of just those characters. However this fails if we have a number other than 2 which has only digits of 2 (e.g. 22 or 222), in order to patch this case we multiply the string used to create the set by the negation of whether or not x contains "22". If it contains it this will be the empty set, otherwise it will be the same as before.
-
\$\begingroup\$
lambda x:set(x)==set("[], 2")? \$\endgroup\$Leaky Nun– Leaky Nun2017年05月13日 07:48:56 +00:00Commented May 13, 2017 at 7:48 -
\$\begingroup\$ Fails for
[22]\$\endgroup\$Leaky Nun– Leaky Nun2017年05月13日 07:51:31 +00:00Commented May 13, 2017 at 7:51 -
\$\begingroup\$ @LeakyNun Fixed \$\endgroup\$2017年05月13日 08:00:22 +00:00Commented May 13, 2017 at 8:00
-
\$\begingroup\$ @LeakyNun Your suggestion fails for
[]\$\endgroup\$2017年05月13日 08:10:27 +00:00Commented May 13, 2017 at 8:10 -
\$\begingroup\$
lambda x:set(x)<=set("[],2"*-~-("22"in x))for -1 \$\endgroup\$ovs– ovs2017年05月13日 08:31:34 +00:00Commented May 13, 2017 at 8:31
Ohm, 6 bytes
∙e]Å2N
Uses CP-437 encoding.
Explanation:
∙e]Å2E
∙e ■しかくEvaluate the input to form an array
Å ■しかくany( , )
] ■しかく flatten(input)
2N ■しかく lambda x:x!=2
■しかくimplict end of any and print
PHP, 46 bytes
<?=!preg_match('/:"(?!2")/',serialize($_GET));
-
\$\begingroup\$ @JörgHülsermann Could you please give an example? All the test cases seem to work. If you test it not through a browser, do you pass scalar values of
$_GETas strings? \$\endgroup\$user63956– user639562017年05月14日 16:00:34 +00:00Commented May 14, 2017 at 16:00 -
\$\begingroup\$
<?=!preg_match('/:"(?!2")/',$argn);and input is a string representation of the serialized array - 11 Bytes \$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017年05月14日 16:35:54 +00:00Commented May 14, 2017 at 16:35
PHP<7.0, 29 Bytes
Input as as string array JSON encoded
<?=!ereg("22|[013-9]",$argn);
PHP<7.0, 42 Bytes
use the deprecated function ereg
<?=!ereg("22|[013-9]",json_encode($_GET));
PHP, 50 Bytes
prints 1 for true and nothing for false
-1 Byte for other wise remove !
or + 1 Byte for true 1, false 0 add + before !
<?=!preg_match('#22|[013-9]#',json_encode($_GET));
-
2\$\begingroup\$ You don't need the
$rvariable:<?array_walk_recursive($_GET,function($i){$i-2&¨})?>1. \$\endgroup\$user63956– user639562017年05月14日 13:48:05 +00:00Commented May 14, 2017 at 13:48
Pyth, 6 bytes
!-.nQ2
Very similar to my CJam answer. I'm still new to Pyth, so please tell me if there's anything I can golf off.
Explanation:
Q Input: [[[], [2]], [1]]
.n Flatten: [2, 1]
- 2 Remove 2s: [1]
! Not: False
Ruby 1.9+, 19 bytes
->x{x*$/!~/^2?+.$/}
Form is stolen from G B's answer. The main difference is that I join on a newline (the default value of $/), which lets me write a shorter regex by using the ^.$ special characters that have specific interactions with newlines.
The trick that makes this version-specific (1.9 and higher) is the use of the "possessive" quantifier 2?+. Like 2?, it'll match zero or one instances of a 2, but this form will hold onto that 2 forever and prevent it from being matched by the ., so a 2 on its own line won't match the overall regexp.
Japt -!, (削除) 6 (削除ここまで) (削除) 5 (削除ここまで) 4 bytes
c dÍ
c dÍ :Implicit output of array
c :Flatten
d :Any truthy (non-zero) when
Í :Subtracted from 2
:Implicit output of negation of result
Setanta, 84 bytes
gniomh f(l){ma go_teacs(l)<"[" toradh l==2t:=1le i idir(0,fad@l)t=t&f(l[i])toradh t}
Alternate text substitution-based solution, 93 bytes
gniomh(l){l=go_teacs(l)le i idir(0,5)l=athchuir@l(["[2",", 2","[","]",","][i],"")toradh"!">l}
Turns out to be longer than the recursion-based solution, but I think this is neat.
Clojure, 21 or 25 bytes
#(every?(set"[]2 ")%)
#(every? #{2}(flatten %))
The first one takes the argument as string, second one as an actual nested list.
#{2} is the set with one element 2, calling it with an existing element returns it and for a non-existing it returns nil (falsy).
Axiom, 280 bytes
w(x:Union(List(Any),NNI)):Boolean==(x case List(Any)=>(g:List(Any):=x;leaf? g=>return true;for i in g repeat(q:=w(i);q=false=>return false);return true);r:NNI:=x;r=2=>true;false)
m(b:List Any):Boolean==(for i in b repeat(q:=w(i::Union(List(Any),NNI));q=false=>return false);true)
ungolfed and test
f(x:Union(List(Any),NNI)):Boolean==
x case List(Any)=>
g:List(Any):=x
leaf? g =>return true
for i in g repeat
q:=f(i)
q=false => return false
return true
r:NNI:=x
r=2=>true
false
h(b:List Any):Boolean==
for i in b repeat
q:=f(i::Union(List(Any),NNI))
q=false=> return false
true
(5) -> [[i, m(i)] for i in [ [2],[2,2],[[2],[2,2],2],[],[[],[]] ] ]
(5)
[[[2],true],[[2,2],true],[[[2],[2,2],2],true],[[],true],[[[],[]],true]]
Type: List List Any
(6) -> [[i, m(i)] for i in [ [1],[22],[2,2,2,1], [[1,2],2] ] ]
(6) [[[1],false],[[22],false],[[2,2,2,1],false],[[[1,2],2],false]]
Type: List List Any
CJam, 7 bytes
Input is in the form of a CJam array literal.
q~e_2-!
Explanation:
q e# Read input: | "[[1 2] 2]"
~ e# Eval: | [[1 2] 2]
e_ e# Flatten: | [1 2 2]
2- e# Remove 2s: | [1]
! e# Not: | 0
Actually, 10 bytes
⌠♂i⌡Y2#@-b
Explanation:
⌠♂i⌡Y2#@-b
⌠♂i⌡Y call the function until the result stops changing (fixed-point combinator):
♂i for each item: flatten
2#@- set difference: all items that are not 2s
b cast to boolean (1 if list is not empty, else 0)
TXR Lisp, 30 bytes
(opip flatten(all @1(op = 2)))
Run:
1> (opip flatten(all @1(op = 2)))
#<intrinsic fun: 0 param + variadic>
2> [*1 '()]
t
3> [*1 '(2)]
t
4> [*1 '(2 2 ())]
t
5> [*1 '(() ())]
t
6> [*1 '(() (1))]
nil
7> [*1 '((2) 1 ())]
nil
Common Lisp, (削除) 68 (削除ここまで) 64 bytes
(defun n(l)(or(and(consp l)(n(car l))(n(cdr l)))(not l)(= l 2)))
-
\$\begingroup\$ This fails on the [[1,2],2] test case. \$\endgroup\$Ørjan Johansen– Ørjan Johansen2017年08月27日 17:31:55 +00:00Commented Aug 27, 2017 at 17:31
-
\$\begingroup\$ @ØrjanJohansen Fixed at the cost of one byte. \$\endgroup\$clapp– clapp2017年08月27日 17:33:11 +00:00Commented Aug 27, 2017 at 17:33
-
1\$\begingroup\$ It is now a dupe of this: codegolf.stackexchange.com/a/120371/44874 \$\endgroup\$steenbergh– steenbergh2017年08月30日 13:42:55 +00:00Commented Aug 30, 2017 at 13:42
Zsh, 9 bytes
<<<${@/2}
Falsey: output contains a number. Truthy: anything else (spaces or empty string). Link includes a test harness that prints T or F
-
\$\begingroup\$ This doesn't really do nested lists does it? It just seems to be space separated numbers \$\endgroup\$Jo King– Jo King2023年07月10日 05:04:39 +00:00Commented Jul 10, 2023 at 5:04
-
\$\begingroup\$ True, Zsh only does 1-dimensional arrays/lists \$\endgroup\$roblogic– roblogic2023年07月10日 10:18:42 +00:00Commented Jul 10, 2023 at 10:18
Pyt, 4 bytes
Ƒ2=Π
Ƒ implicit input; Ƒlatten array
2= is each element equal to 2?
Π product (returns 1 for empty array); implicit print
APL(Dyalog Unicode), (削除) (削除ここまで)9 bytes SBCS
(⊢≡2⍴⍨≢)∊
Alternatively, another 9-byte solution:
∧/'2 '∊⍨⍕
[[2]]does not contain a two. \$\endgroup\$