15
1
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!
2Amazingly, none of your falsy test cases can really come from a graph! – Christian Sievers – 2020-02-15T09:54:17.563
@ChristianSievers included a couple of test cases from trees and the complete graphs on 4 and 8 vertices :) but I wasn't excluding the possibility of a graph having self-loops or multiple edges, so these could come from multi-graphs :) – RGS – 2020-02-15T10:01:30.710
3Even then the sum of the degrees should be twice the number of edges and therefore an even number – Christian Sievers – 2020-02-15T10:09:11.547
@ChristianSievers yes, and so what? :) – RGS – 2020-02-15T12:34:08.803
2Are 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). – Jonathan Allan – 2020-02-15T12:54:25.973
1As far as I know, these conditions are necessary but not sufficient i.e. one-strokyness implies them, but not the other way around – Varad Mahashabde – 2020-02-16T18:23:15.550
@VaradMahashabde the conditions are equivalent to the one-strokeyness, actually :) you can read about it by googling Eulerian path. The idea is that, as you traverse the edges, whenever you arrive at a vertex you must be able to leave it, thus requiring vertices to have even degree. This has one exception: if you don't arrive at the vertex from where you left, then those two vertices must have odd degree. Of course all of this is happening in a connected graph :) – RGS – 2020-02-16T19:23:08.567
1
Related puzzle https://codeforces.com/problemset/problem/788/B
– qwr – 2020-02-18T00:12:07.340