24
\$\begingroup\$

CodeDrawing one-liner

Teaser

Behold this formidable drawing:

Can you draw this in a single stroke? Give it a try.

Can you do this one, now:

Give it a try.

How it works

These "make this drawing with one pen stroke" problems are problems with a rather simple solution. For each vertex, compute its degree. If all vertices have an even degree or if only two vertices have an odd degree, this is possible. In any other case, it is impossible. This is the same as finding an Eulerian path in the graph. This is also related to the very famous seven bridges of Königsberg puzzle.

For the first drawing, the degrees are

   2
 /   \
4-----4
|\   /|
|  4  |
|/   \|
3-----3

so we are in the second possible case, as all numbers are even except for the two 3s in the bottom. What is more, when we have odd numbers, we have to start in one of them and finish in the other. When everything is even, we can start wherever we want but we will finish where we started.

For the second drawing it is impossible, as there are too many odd degrees:

     2
   /   \
  5-----5
 /|\   /|\
2 |  4  | 2
 \|/   \|/
  5-----5
   \   /
     2

Task

Given the degrees of the edges as input, decide if a corresponding drawing could be done in a single pen stroke. Output Truthy if such a feat is possible and output Falsy otherwise.

Input

The degrees of the vertices in any sensible format, such as

  • an array of integers like [2,4,4,4,3,3] or [2,5,5,2,4,2,5,5,2]
  • separate arguments to a function, like f(2,4,4,4,3,3) or f(2,5,5,2,4,2,5,5,2)

Output

A Truthy value if all numbers are even or exactly two of them are odd, Falsy otherwise.

Test cases

Truthy

[2, 4, 4, 3, 3]
[2, 2]
[1, 2, 3, 4, 6, 8]
[2, 4, 2, 4, 2, 4, 2, 4]
[8, 6, 8, 2, 2, 3, 11]
[1, 3]
[8]

Falsy

[1, 2, 3, 4, 5, 6]
[2, 3]
[9]
[7, 2, 2, 2]
[4, 2, 7]
[3, 3, 3, 3]
[1, 2, 3, 1, 1]
[1, 1, 4, 1, 1]
[7, 7, 7, 7, 7, 7, 7, 7]

This is so shortest submission in bytes, wins! If you liked this challenge, consider upvoting it... And happy golfing!

\$\endgroup\$
8
  • 3
    \$\begingroup\$ Amazingly, none of your falsy test cases can really come from a graph! \$\endgroup\$ Commented Feb 15, 2020 at 9:54
  • 4
    \$\begingroup\$ Even then the sum of the degrees should be twice the number of edges and therefore an even number \$\endgroup\$ Commented Feb 15, 2020 at 10:09
  • 2
    \$\begingroup\$ Are you saying impossible lists of integers may be inputs? If so I think it should be clear in the spec (i.e. "The degrees of the vertices" would not be a correct description of the input). \$\endgroup\$ Commented Feb 15, 2020 at 12:54
  • 1
    \$\begingroup\$ As far as I know, these conditions are necessary but not sufficient i.e. one-strokyness implies them, but not the other way around \$\endgroup\$ Commented Feb 16, 2020 at 18:23
  • 1
    \$\begingroup\$ Related puzzle codeforces.com/problemset/problem/788/B \$\endgroup\$
    – qwr
    Commented Feb 18, 2020 at 0:12

32 Answers 32

13
\$\begingroup\$

Python, 31 bytes

lambda l:sum(n%2for n in l)|2<3

Try it online!

We use k|2<3 to check that k is either 0 or 2. This works because the bit operation |2 sets the bit for place-value 2, so to get a result that's 2 or less, there must be no other bits set.

\$\endgroup\$
1
  • \$\begingroup\$ A really good golf, as per usual! +1 \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:40
5
\$\begingroup\$

Wolfram Language (Mathematica), 25 bytes

(a=Tr@Mod[#,2])==0||a==2&

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ +1 on this really short submission! Tr is summing everything, right? \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:43
  • 2
    \$\begingroup\$ @RGS If I recall correctly, Tr calculates the trace of a matrix, but for a vector that's simply the sum of the elements. \$\endgroup\$
    – Neil
    Commented Feb 16, 2020 at 0:09
4
\$\begingroup\$

05AB1E, 5 4 bytes

Thanks to 05AB1E's weird boolification system, any number but 1 is falsy.

-1 Thanks to Jonathan Allan

ÉO1α

Try it online!

Explanation

É     Vectorizing n % 2
 O    Sum the resulting list
  1α  |n - 1|
\$\endgroup\$
3
  • \$\begingroup\$ Are you going to submit answers in all the languages you know..? XD \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 9:48
  • \$\begingroup\$ I believe any integer but 1 is falsey in 05AB1E, if so you don't need the Θ. \$\endgroup\$ Commented Feb 15, 2020 at 13:22
  • \$\begingroup\$ This is incredibly short! Good job \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:45
4
\$\begingroup\$

Turing Machine But Way Worse, 1245 bytes

0 0 0 1 1 0 0
0 1 0 1 2 0 0
1 2 1 1 3 0 0
1 3 1 1 4 0 0
0 4 0 1 5 0 0
1 4 1 1 5 0 0
0 5 0 1 6 0 0
1 5 1 1 6 0 0
0 6 0 1 7 0 0
1 6 1 1 7 0 0
0 7 0 1 0 0 0
1 7 1 1 8 0 0
0 8 0 1 9 0 0
0 9 0 1 a 0 0
1 a 1 1 b 0 0
1 b 1 1 c 0 0
0 c 0 1 d 0 0
1 c 1 1 d 0 0
0 d 0 1 e 0 0
1 d 1 1 e 0 0
0 e 0 1 f 0 0
1 e 1 1 f 0 0
0 f 0 1 0 0 0
1 f 1 1 8 0 0
0 b 0 1 k 0 0
0 3 0 1 4 0 0
0 g 0 1 h 0 0
0 h 0 1 i 0 0
1 i 1 1 j 0 0
1 j 1 1 k 0 0
0 k 0 1 l 0 0
1 k 1 1 l 0 0
0 l 0 1 m 0 0
1 l 1 1 m 0 0
0 m 0 1 n 0 0
1 m 1 1 n 0 0
0 n 0 1 g 0 0
1 n 1 1 o 0 0
0 o 0 1 p 0 0
0 p 0 1 q 0 0
1 q 1 1 r 0 0
1 r 1 1 s 0 0
0 s 0 1 t 0 0
1 s 1 1 t 0 0
0 t 0 1 u 0 0
1 t 1 1 u 0 0
0 u 0 1 v 0 0
1 u 1 1 v 0 0
0 v 0 1 g 0 0
1 v 1 1 o 0 0
0 r 0 1 A 0 0
0 j 0 1 k 0 0
0 w 0 1 x 0 0
0 x 0 1 y 0 0
1 y 1 1 z 0 0
1 z 1 1 A 0 0
0 A 0 1 B 0 0
1 A 1 1 B 0 0
0 B 0 1 C 0 0
1 B 1 1 C 0 0
0 C 0 1 D 0 0
1 C 1 1 D 0 0
0 D 0 1 w 0 0
1 D 1 1 E 0 0
0 E 0 1 F 0 0
0 F 0 1 G 0 0
1 G 1 1 H 0 0
1 H 1 1 I 0 0
0 I 0 1 J 0 0
1 I 1 1 J 0 0
0 J 0 1 K 0 0
1 J 1 1 K 0 0
0 K 0 1 L 0 0
1 K 1 1 L 0 0
0 L 0 1 w 0 0
1 L 1 1 E 0 0
0 H 0 1 0 0 1
0 z 0 1 A 0 0
0 2 1 1 M 0 0
0 y 1 1 M 0 0
0 q 1 1 M 0 0
0 M 1 1 N 0 0
0 N 0 1 O 0 0
0 O 0 1 P 0 0
0 P 0 1 Q 0 0
0 Q 1 1 0 1 1
0 a 0 1 0 0 1
0 i 0 1 0 0 1
0 G 0 1 0 0 1

Try it online!

Input: A list like n, n, n, n, ...
Output: (blank) for falsy, 1 for truthy

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I take my hat off to you for your work :p +1 \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 13:52
  • \$\begingroup\$ I didn't realize this existed until today. I'm so honored to have someone use my programming language-- especially when it's for a non-trivial problem-- so this is the first time I've come back from the dead on SE just to upvote this solution. \$\endgroup\$
    – MilkyWay90
    Commented Oct 31, 2020 at 1:46
3
\$\begingroup\$

Charcoal, 8 bytes

⁼¹↔⊖Σ﹪A²

Try it online! Link is to verbose version of code. Port of @ovs' 05AB1E answer. Explanation:

      A     Input array
     ﹪ ²    Vectorised modulo literal 2
    Σ       Sum
   ⊖        Decremented
  ↔         Absolute
⁼¹          Equals literal 1

There are other ways to check for 0 or 2 for the same byte count, such as Not(Decremented(Absolute(Decremented(...)))), Equals(2, BitwiseOr(2, ...)), or Count("02", Cast(...)) (careful not to use "20" of course).

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your submission! +1 \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:40
3
\$\begingroup\$

Retina 0.8.2, 18 bytes

M`[13579]\b
^[02]$

Try it online! Link includes test cases. Explanation:

M`[13579]\b

Count the number of odd numbers.

^[02]$

Compare to 0 or 2.

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

JavaScript (ES6), 29 bytes

a=>a.map(x=>m>>=x&1,m=5)&&m&1

Try it online!

How?

We start with the bitmask \$m=101_2=5_{10}\$ and right-shift it by one position for each odd value in the input array. The final result is given by the parity of \$m\$.

\$\endgroup\$
1
  • \$\begingroup\$ Ah this feels really clever! +1 \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:47
2
\$\begingroup\$

Python 3, 34 bytes

lambda a:sum(n%2for n in a)in(0,2)

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Good job on this one! +1 \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 9:47
2
\$\begingroup\$

05AB1E, 6 5 bytes

-1 byte thanks to a'_'.

ÉO<ÄΘ

Try it online!

Explanation

É       is the number odd (vectorizes over the input list)
 O      sum (number of odd values)
  <     decrement (number of odd values - 1)
   Ä    absolute value (|number of odd values - 1|)
    Θ   equal to 1 (|number of odd values - 1| == 1)
\$\endgroup\$
3
  • \$\begingroup\$ 5 bytes by using a == 1 built-in. I guess you win. \$\endgroup\$
    – user92069
    Commented Feb 15, 2020 at 10:28
  • \$\begingroup\$ @a'_' thanks a lot. It took me a while to understand this because lowercase and uppercase theta look much too similar. \$\endgroup\$
    – ovs
    Commented Feb 15, 2020 at 10:39
  • \$\begingroup\$ @ovs let's ask Greece to change their alphabet a bit! +1 on your short submission \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:41
2
\$\begingroup\$

Bash + Unix utilities, 28 bytes

dc<<<`grep -c [13579]$`d2-*p

Try it online!

Input is on stdin: one number per line.

Output is on stdout: 0 is truthy, any non-zero value is falsy.

How it works:

  • grep counts the number n of lines ending with an odd digit;
  • dc then computes and prints n*(n-2), which is 0 iff n equals 0 or 2.
\$\endgroup\$
0
2
\$\begingroup\$

Excel, 50 bytes

=ISNUMBER(FIND(SUMPRODUCT(--(MOD(A:A,2)=1)),"02"))

MOD(A:A,2)=1 to find odd number SUMPRODUCT() to count them FIND( ,"02") to check if 0 or 2 ISNUMBER() to converts to TRUE/FALSE

Feels very clunky, hope to refine...

\$\endgroup\$
1
  • \$\begingroup\$ Looks nice :D +1 \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 17:42
2
\$\begingroup\$

C++ (gcc), 73 64 bytes

I don't know why I keep trying.

int f(int*a,int s){int o=1;for(;s;)o+=a[--s]%2;return o%2&&o<4;}

Commented :

int f(int*a,int s){ // function definition

  int o=1;      // odd counter starts at 1 because then parity of truthy inputs is one

  for(;s;)          // Use size arg as counter and iterate backwards (Thanks @MitchellSpector)
    o+=a[--s]%2;    // check for odd, add parity

  return o%2&&o<4;  // Valid values for odd are 0 and 2 (or rather 1 and 3)
                    // Solve with parity and range check
}

TIO-k6qqj7ln

\$\endgroup\$
4
  • \$\begingroup\$ You keep trying because this is fun! +1 \$\endgroup\$
    – RGS
    Commented Feb 16, 2020 at 23:55
  • \$\begingroup\$ Nice... Keeping the basic idea, here's how you can get it down from 73 to 64 bytes: int f(int*a,int s){int o=1;for(;s;)o+=a[--s]%2;return o%2&&o<4;} \$\endgroup\$ Commented Feb 17, 2020 at 1:53
  • 1
    \$\begingroup\$ @MitchellSpector My good coding practice of not editing the arguments is showing!! \$\endgroup\$ Commented Feb 17, 2020 at 17:20
  • \$\begingroup\$ Suggest o&o<4 instead of o%2&&o<4 \$\endgroup\$
    – ceilingcat
    Commented May 4, 2020 at 7:28
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 26 bytes

p=>(p.Count(n=>n%2>0)|2)<3

Try it online!

Thanks to answer of @xnor -1B

27 bytes

p=>(p.Count(n=>n%2>0)&-3)<1

Try it online!

This one was my first attempt, but I made mistake at first so I'm publishing it now when I fixed it.

\$\endgroup\$
4
  • \$\begingroup\$ Also a port of the python answer, right? +1 \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 13:53
  • 1
    \$\begingroup\$ At first I made it by myself, but I made mistake when checking if the count is 0 or 2 so it didn't work as i expected so I got inspired by the |2<3 part. But I found the mistake now, so I will add my original thought (one byte longer) \$\endgroup\$ Commented Feb 20, 2020 at 7:29
  • \$\begingroup\$ really nice answers! Thanks for sharing \$\endgroup\$
    – RGS
    Commented Feb 20, 2020 at 9:37
  • 1
    \$\begingroup\$ 22 \$\endgroup\$
    – dana
    Commented Mar 5, 2021 at 11:58
2
\$\begingroup\$

Julia 1.0, 15 bytes

~v=sum(v.%2)&-3

Try it online!

A port of Gymhgy's Japt answer (0 as Truthy, other numbers as Falsy).

17 bytes porting Arnauld's JS solution :

~v=5>>sum(v.%2)%2 Try it online!

And an obligatory port of the popular 05AB1E solution, at 19 bytes:

~v=abs(sum(v.%2)-1) Try it online!

\$\endgroup\$
2
\$\begingroup\$

MATL, 6 bytes

osq|1=

Try it online!

I herd we're portin 05AB1E solutions!

A port of Arnauld's JS solution while we're at it (8 bytes):
osW5w/ko Try it online!

And one of Gymhgy's Japt answer (6 bytes, 0 as Truthy, other numbers as Falsy):
os3_Z& Try it online! (though, os2FZ: is a more straightforward implementation of the idea in MATL Try it online!)

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

Keg, 13 bytes

÷⑷2%⑸⅀:0=$2=+

Try it online!

Simply using everyone else's algorithm!

\$\endgroup\$
2
  • \$\begingroup\$ Why bother coming up with your own algorithm if everyone else is doing great, right? +1 \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:48
  • 1
    \$\begingroup\$ @RGS Exactly! Who cares about originality anyway! :P \$\endgroup\$
    – lyxal
    Commented Feb 15, 2020 at 21:49
1
\$\begingroup\$

Japt -!, 7 bytes

èu a1 É

Try it

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for your submission! +1 were you the one designing the Japt interpreter? \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:46
  • \$\begingroup\$ Yes, @RGS, that interpreter is my creation. \$\endgroup\$
    – Shaggy
    Commented Feb 16, 2020 at 0:41
  • \$\begingroup\$ it looks really good! I just wanted you to know that! Good job on that :) \$\endgroup\$
    – RGS
    Commented Feb 16, 2020 at 10:56
1
\$\begingroup\$

W d, 8 7 bytes

♦æ╥└¬y÷

Uncompressed:

2mJ1-z1=

Explanation

2m       % Modulo the list by 2, auto-vectorizes.
  J      % Sum the list.
   1-    % Decrement by 1.
     z   % Absolute value.
      1= % Is this 1?
\$\endgroup\$
2
  • \$\begingroup\$ When you look at this on mobile the first character is the moderator diamond. \$\endgroup\$
    – S.S. Anne
    Commented Feb 16, 2020 at 1:08
  • \$\begingroup\$ Yes, but without the aeTLlyt. \$\endgroup\$
    – S.S. Anne
    Commented Feb 16, 2020 at 15:19
1
\$\begingroup\$

Stax, 8 bytes

This method is shorter in Stax.

ƒ╟⌡≡ù₧╬)

Run and debug it

Explanation

{2%m       Map modulo 2 over the input
    |+     Sum the resulting list
      02\  In the list [0, 2]:
         # How many times does this number appear?
\$\endgroup\$
1
\$\begingroup\$

PHP, 39 43 bytes

for(;$n=$argv[++$i];)$s+=$n%2;echo!($s&-3);

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Thanks misread the question! Fixed. \$\endgroup\$ Commented Feb 15, 2020 at 20:43
  • \$\begingroup\$ @Arnauld thanks for the help here :) \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:46
1
\$\begingroup\$

Perl 5 -pa, 20 bytes

$_=(2|grep$_%2,@F)<3

Try it online!

Borrows the |2<3 trick from @xnor's Python answer

\$\endgroup\$
1
  • \$\begingroup\$ Good job porting xnor's trick and getting an answer shorter than his :) \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:46
1
\$\begingroup\$

Japt -!, 6 bytes

èu &-3

Try it

èu &-3        Full program taking in array of degrees U
èu            Amount of odd numbers in array
   &          Bitwise AND
    -3        With -3; -3 has very bit set except of the second bit (1111...11101)
-!            Zero -> True, Other numbers -> False
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the Japt submission! Can't you have several test cases in the Japt interpreter? \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:44
  • \$\begingroup\$ I understand, thanks for the explanation!! \$\endgroup\$
    – RGS
    Commented Feb 15, 2020 at 21:51
1
\$\begingroup\$

GolfScript, 11 bytes

{2%+}*(2?(!

Try it online!

My first go at it, may shrink it a bit. I made it so 1 is true, 0 is false, but if you're okay with the other way around, I can do different things :)

Short explanation; Convert all the elements to mod 2 and sum them. The only valid answers would be 0 or 2. Now, subtract 1 and square them. 0 is -1 is 1. 2 is 1 is 1. 0 and 2 both give you 1. Lastly, subtract 1 and perform a not operation to make all non-0, non-2 values to a 0 and the 0-2s to a 1.

Kind of roundabout, and I'm sure I can cut two characters somewhere.

\$\endgroup\$
1
  • \$\begingroup\$ This looks nice! +1 thanks for your submission \$\endgroup\$
    – RGS
    Commented Feb 16, 2020 at 23:55
1
\$\begingroup\$

APL (Dyalog Unicode), 10 bytesSBCS

0 2∊⍨1⊥2∘|

Try it online!

Because a challenge needs an APL answer.

How it works

0 2∊⍨1⊥2∘|
       2∘|  ⍝ Modulo 2
     1⊥     ⍝ Sum
0 2∊⍨       ⍝ Is member of [0 2]?
\$\endgroup\$
1
  • \$\begingroup\$ Yes it does! +1 for that :) \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 8:03
1
\$\begingroup\$

Whitespace, 102 bytes

[S S S N
_Push_0][S N
S _Dupe_0][N
S S N
_Create_Label_LOOP][S N
S _Dupe][S N
S _Dupe][T  N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][S N
S _Dupe_input][N
T   T   S N
_If_neg_Jump_to_Label_DONE][S S S T S N
_Push_2][T  S T T   _Modulo][T  S S S _Add][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_DONE][S N
N
_Discard][S N
S _Dupe][N
T   S T N
_If_0_Jump_to_Label_TRUTHY][S S S T S N
_Push_2][T  S S T   _Subtract][N
T   S T N
_If_0_Jump_to_Label_TRUTHY][T   N
S T _Print_as_integer][N
N
N
_Exit_program][N
S S T   N
_Create_Label_TRUTHY][S S S T   N
_Push_1][T  N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Since Whitespace inputs one integer at a time, the input should contain a trailing -1 so it knows when to stop reading integers and the input is done.
Outputs 1/0 for truthy/falsey respectively.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Integer n = 0
Start LOOP:
  Integer i = STDIN as input
  If(i < 0):
    Jump to Label DONE
  n = n + (i modulo-2)
  Go to next iteration of LOOP

Label DONE:
  If(n == 0): Jump to Label TRUTHY
  If(n-2 == 0): Jump to Label TRUTHY
  Print 0 as integer to STDOUT
  Exit program

Label TRUTHY:
  Print 1 as integer to STDOUT
\$\endgroup\$
3
  • \$\begingroup\$ I was starting to fear this would never happen :( +1 for making me happy on a Monday morning! Quick question: when reading from stdin, is there a default way of knowing the input is over? \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 8:17
  • 1
    \$\begingroup\$ @RGS Whitespace only has two ways to read STDIN input: read as integer and read as character. So no, there isn't a default way of knowing the input is over, unless trying to read a next character that isn't there anymore, after which the program stops with an error. I usually use a newline as indicator when a string input is being asked; -1 if only positive integers are being asked; etc. \$\endgroup\$ Commented Feb 17, 2020 at 8:21
  • 1
    \$\begingroup\$ thanks! I was asking because the other day I read your "make a whitespace interpreter" challenge and as I read through the specs, I couldn't figure out how we'd know the input is over when programming in whitespace. For some reason I just kept thinking that, much like brainfck "reads" a 0 when input is over, whitespace must have a similar mechanism :p but it really doesn't need to and it doesn't have :) \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 8:33
1
\$\begingroup\$

MathGolf, 6 bytes

¥Σ(±1=

Try it online.

Explanation:

¥       # Take modulo-2 on each integer of the (implicit) input-list
 Σ      # Sum those
  (     # Decrease it by 1
   ±    # Take the absolute value
    1=  # And check whether this is equal to 1
        # (after which the entire stack joined together is output implicitly)
\$\endgroup\$
1
  • \$\begingroup\$ Cool subtracting by one and then absolute value to check for 0 and 2 \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 8:33
1
\$\begingroup\$

Java 8, 28 bytes

s->(s.map(i->i%2).sum()|2)<3

Port of @xnor's Python answer

Try it online.

Explanation:

s->               // Method with IntStream parameter and boolean return-type
  (s.map(i->i%2)  //  Take modulo-2 for each value in the IntStream
    .sum()        //  And them sum those
    |2)           //  Take a bitwise-OR with 2
       <3         //  And check whether that is smaller than 3
\$\endgroup\$
1
  • \$\begingroup\$ Keep up the good work +1 \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 10:52
1
\$\begingroup\$

Chevron, 172 147 bytes

^__>^n
>^n>^t
^t~s>>^s
0>>^o
0>>^i
^i+1>>^i
^t,^i~c>>^c
->+2??^c~^_i
->-3
^n^c>^n
^n~o>>^z
^o+^z>>^o
^__>^n
->+2?^i=^s
->-9
^o-2>>^p
^o*^p>>^o
>^o

Takes numbers from stdin in the form 2 4 4 3 3.

Outputs 0 or -0 for truthy, anything else falsy.

\$\endgroup\$
3
  • \$\begingroup\$ Hey there, thanks for your submission! Could you please consider adding a tio link with the test cases? \$\endgroup\$
    – RGS
    Commented Feb 17, 2020 at 17:47
  • 1
    \$\begingroup\$ Chevron isn't on tio yet, I'll work on that :) \$\endgroup\$
    – Superloach
    Commented Feb 17, 2020 at 17:48
  • \$\begingroup\$ @RGS it's not tio, but here's something to test on :) ch.superloach.xyz \$\endgroup\$
    – Superloach
    Commented Feb 17, 2020 at 19:39
1
\$\begingroup\$

Vyxal, 6 bytes

∺∑⨪Ȧ1=

Try it Online!

Just another port of the 05ab1e answer.

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

R, 30 29 bytes

Or **R>=4.1, 22 bytes by replacing the word function with a \.

Edit: -1 byte thanks to @Giuseppe.

function(v)sum(v%%2/2)%in%0:1

Try it online!

Almost straightforward approach.

Less straightforward, but used by many other answers, approach results in the same byte-count:

function(v)(sum(v%%2)-1)^2==1

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 29 bytes? \$\endgroup\$
    – Giuseppe
    Commented Mar 6, 2022 at 19:49
  • \$\begingroup\$ @Giuseppe nice one, thanks! \$\endgroup\$
    – pajonk
    Commented Mar 6, 2022 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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