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 graph-theory 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)
orf(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 code-golf so shortest submission in bytes, wins! If you liked this challenge, consider upvoting it... And happy golfing!