-3
\$\begingroup\$

Challenge

You have been assigned a task to write a program which takes a multigraph as its input and if it contains an Eularian circuit to return one. Otherwise, the program's behavior is undefined, according to the specifications you're handed by your boss.

However, you recently caught your new coworker Steven several times treating undefined behavior in C carelessly and urged him be more careful because undefined behavior can also mean that all files on the system get deleted. He now dared you to write your program in a way such that it deletes itself when the aforementioned case of (previously) undefined behavior occurs. You don't want to lose your honor and accept.

Description

An Eulerian circuit is a circuit in an undirected multigraph which visits every edge exactly once.

You may choose the formats of your program's input and output yourself. They don't need to be the same formats. For example, you may take a description of the edges like

({1,2},{1,4},{2,4},{2,3},{2,3})

as your input for this graph

, but return a sequence of vertices as your output:

(1,2,3,2,4,1)

Details

You don't need to be able to accept multigraphs which contain vertices without edges.

Holding your program only in RAM and terminating it afterwards doesn't count as deleting it. It needs to be in a file. The file must only be deleted if no Eulerian circuit exists in the input multigraph.

The vertices have to be identifiable (it needs to be possible to tell the Euler circuit in the input provided the output of your program), the edges don't. However, you may restrict the means of identification (e.g. only positive integers or only strings consisting of a # followed by a positive integer) as long as large graphs are representable (only 1 letter of the English alphabet isn't sufficient). Identifiers don't have to be explicit. For example, you may let the order of the input determine the number a vertex is identified with in the output.

The means of output (saved to a file, printed at stdout, return value of a function) don't matter.

You can't rely on the name of your program on the disk not changing. If you only write a function, you still have to delete the file on the disk.

Victory Condition

This is code golf, shortest code wins.


Spoiler warning for this link: Simple algorithms to solve this problem can be found on the internet.

\$\endgroup\$
5
  • \$\begingroup\$ Are functions allowed for this or only full programs with input and output? \$\endgroup\$
    – user61980
    Commented Jan 11, 2017 at 1:35
  • \$\begingroup\$ @ngenisis They are: "The means of output (saved to a file, printed at stdout, return value of a function) don't matter." \$\endgroup\$
    – UTF-8
    Commented Jan 11, 2017 at 7:54
  • 2
    \$\begingroup\$ If anyone wants to solve this in Python, just use Vigil instead and put a swear not cycle() in your main. \$\endgroup\$ Commented Jan 11, 2017 at 9:09
  • \$\begingroup\$ This really needs test cases for non-graph instances. \$\endgroup\$ Commented Jan 11, 2017 at 9:37
  • 4
    \$\begingroup\$ I downvoted this because it's basically two unrelated problems (finding a cycle, and deleting the program's file on disk) combined into one. That doesn't seem to add much over submitting the problems separately, other than making it harder to find the appropriate language. \$\endgroup\$
    – user62131
    Commented Jan 11, 2017 at 22:51

1 Answer 1

4
\$\begingroup\$

Mathematica, 48 bytes

FindEulerianCycle@#/.{}->DeleteFile@FileNames[]&

Yep, there's a builtin for that. It returns {} if there's no Eulerian cycle, so we manually redirect that to a command deleting all files in the current working directory.

\$\endgroup\$
3
  • \$\begingroup\$ Actually, this just deletes all files in the current working directory. Wouldn't you need something like NotebookFileName[]? Do byte counts for notebooks include all of the cell data? \$\endgroup\$
    – user61980
    Commented Jan 11, 2017 at 1:32
  • \$\begingroup\$ Also, if this is just a function it's not clear what "the file" means \$\endgroup\$
    – user61980
    Commented Jan 11, 2017 at 1:35
  • \$\begingroup\$ I believe one can run Mathematica from the prompt and type << [filename] to run this program, which can be a simple text file. Not gonna test this particular one though :) Also the OP writes "The means of output (... return value of a function)". \$\endgroup\$ Commented Jan 11, 2017 at 5:24

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.