27
1
In a single file, write a program that requires no input and produces no output. When run it should reverse the name of the file it is contained in, regardless of what the name is, without altering the code or producing other lasting side effects.
Any way of achieving this is fine. It only matters that once the program is run the only lasting change is that its file name has been reversed. e.g. no new files should be in the directory.
Running the program again should reverse the name back. Indeed, the program should be able to be run arbitrarily many times.
For the purposes of this challenge:
- You may assume filenames are always strings of lowercase letters (a-z) between 1 and 127 characters long. (If your language requires files to have extensions to run then just reverse the part before the extension, e.g.
mycode.bat
→edocym.bat
.) - You may assume the code file is in a directory by itself so it will not have naming conflicts (except with itself).
- You may not assume the filename is not a palindrome, i.e. the same when reversed. Filenames that are palindromes should work just as well as those that aren't.
- You may read your file's contents or metadata. There are no quine restrictions here.
- You may assume your program will be run on a particular, modern, commonplace operating system (e.g. Windows/Linux), since not all shells have the same command set.
As a concrete example, say you have a Python program in a file called
mycode
in its own directory. Runningpython mycode
in the terminal should result in the filename being reversed to
edocym
. The fileedocym
should be alone in its directory - no file namedmycode
should exist anymore. Runningpython edocym
will reverse the name back to
mycode
, at which point the process can be repeated indefinitely.If the same Python file was renamed
racecar
(without changing the code) and then runpython racecar
nothing should visibly change since "racecar" is a palindrome. That same goes if the filename were, say,
a
orxx
.
The shortest code in bytes wins. Tiebreaker is higher voted answer.
Does the current working directory matter? – Brad Gilbert b2gills – 2016-06-11T02:11:32.567
@BradGilbertb2gills You should be able to copy the folder with the program to somewhere else and still have it work (assuming you have permissions and whatnot). You can assume the working directory of the shell will be the folder the file is in. – Calvin's Hobbies – 2016-06-11T03:30:08.783
6What if we are using a compiled language? How does the executable affect your rules? – EMBLEM – 2016-06-11T05:39:37.043
Request to clarify 'accepts no input and produces no output'. Technically, the name of the file is an input that must ne retrieved from the file system and the changed name must be sent to the file system. These are input and output. It may be worthwhile to specify that no other outputs are allowed. – atk – 2016-06-13T00:14:12.527
Additionally, do you consider use of child processes to be violations of the input/output rules? That is, calling a separate process to perform part of the task, like reversing the name, would require sending the other process the name (output from your code and input to the app) and receive the value from the process (output of the process and input to the app). – atk – 2016-06-13T00:16:38.263
@EMBLEM I guess the executable should reverse its own filename. Tbh I didn't consider this and your own reasonable interpretation of the rules is probably fine. – Calvin's Hobbies – 2016-06-13T00:19:12.247
@atk You're overthinking, splitting hairs. User input should not be required. No output should be visible when running the program in a normal way. Think of it like a function with no args that returns void. – Calvin's Hobbies – 2016-06-13T00:20:48.483