21
1
Write a program that takes as input a string and an integer n
, and outputs:
- The string that was passed to the program
n
times ago; - A new program that will be used for the next invocation.
You cannot store any data outside of the program, and your program cannot call previous programs in the chain. If the string does not exist, output an empty string (but still output the next program).
Example run, where I use the notation program_n
for each successive program (Of course, [This text is the nth program]
would be replaced with actual code.)
$ program_1 "One" 1
[This text is the second program]
$ program_2 "Two" 1
One
[This text is the third program]
$ program_3 "Three" 2
One
[This text is the fourth program]
$ program_4 "Four" 2
Two
[This text is the fifth program]
$ program_5 "Five" 1
Four
[This text is the sixth program]
Should the new program's code be output as a string? Or should it be saved to a file and the filename output? – Mego – 2015-10-12T06:04:41.910
@Mego Output it as a string (that is, to STDOUT). You don't need to implement the copying the new program to a file. – absinthe – 2015-10-12T06:05:29.797
By "output nothing", do you mean output the next program, but not the (non-existant) string? – Mego – 2015-10-12T06:08:26.183
@Mega Yes, that's what I meant. – absinthe – 2015-10-12T06:08:52.370
You could also add the
program_n+1
's to the output line like[program_3, One]
if that is what you would like to see. If both output goes to stdout how should they be separated? Also are function allowed instead of full programs? – randomra – 2015-10-12T06:10:36.680@randomra Yeah, that's basically what I wanted. You can separate them with a newline. And it should be a full program. – absinthe – 2015-10-12T06:15:00.593
Can the string have spaces in it? – svsd – 2015-10-12T06:58:37.630
Suppose my language has support for variable history within a session -- allowing me to easily access and modify past instances of a variable. Is it absolutely unequivocal that my program can't, say, generate its own new n+1 source files with all the variable histories and iteratively execute itself? – cat – 2015-10-20T04:08:06.187
given that I promise not to infect you with a fork bomb (garbage collection: WIP) – cat – 2015-10-20T04:09:06.407