Write a program that takes as input a string and an integer n, and outputs:

  1. The string that was passed to the program n times ago;
  2. 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
[This text is the third program]
$ program_3 "Three" 2
[This text is the fourth program]
$ program_4 "Four" 2
[This text is the fifth program]
$ program_5 "Five" 1
[This text is the sixth program]
  • \$\begingroup\$ Should the new program's code be output as a string? Or should it be saved to a file and the filename output? \$\endgroup\$
    – user45941
    Commented Oct 12, 2015 at 6:04
  • \$\begingroup\$ @Mego Output it as a string (that is, to STDOUT). You don't need to implement the copying the new program to a file. \$\endgroup\$
    – absinthe
    Commented Oct 12, 2015 at 6:05
  • \$\begingroup\$ By "output nothing", do you mean output the next program, but not the (non-existant) string? \$\endgroup\$
    – user45941
    Commented Oct 12, 2015 at 6:08
  • \$\begingroup\$ @Mega Yes, that's what I meant. \$\endgroup\$
    – absinthe
    Commented Oct 12, 2015 at 6:08
  • \$\begingroup\$ 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? \$\endgroup\$
    – randomra
    Commented Oct 12, 2015 at 6:10

6 Answers 6


CJam, 25


Try it online


L      push an empty array (this is the array of previous strings)
{…}    push this block
_      duplicate the block
~      execute the 2nd copy (the stack contains the array and the block)

The block:

\      swap the array with the block
_      duplicate the array
l      read a line from the input (containing the integer n)
~(     evaluate n and decrement it
>      slice the array starting at that position
1<     slice the resulting array to keep only the first string (if any)
l      read the 2nd line from the input (containing the string)
N+     append a newline
a      wrap in an array
@      bring the previous array to the top
+      concatenate the arrays, thus prepending the new string
`      convert the array to its string representation
@      bring the block to the top
"_~"   push this string

At the end, the requested string (if any), the array's representation, the block and the string "_~" are printed automatically.


Python, 221 bytes

import sys
o,p=[''],r'import sys;a,o,p=int(sys.argv[2]),[{2},{0}],{1};print o[a] if len(o)>a else "","\n",p.format(`sys.argv[1]`,`p`,",".join(`s`for s in o))'
print '\n',p.format(`sys.argv[1]`,`p`,','.join(`s`for s in o))

To test this easily, use ./thisgolf.py "yourfirststring" | python -c "import sys;exec(sys.stdin.read().split('\n')[1])" "your second string" <N>, repeating the last bit as many times as you'd like.


Python 2, 207 bytes

def r(O,R):import sys,marshal as m;a=sys.argv;b=int(a[2]);O.extend(["",""]*b);O[b]=a[1];print"%s\nfrom marshal import*;c=%r;i=lambda:0;i.__code__=loads(c);i(%r,i)"%(O[0],m.dumps(R.__code__),O[1:])

Built on my other quine but changes program, this task is simpler so I was able to golf this further. If I was able to take the input to stdin, this should be much shorter.


Python 3, 90 bytes


Try it online!

How it works:

  • a store [<code>, <first call>, ..., <last call>]
  • a[1:-int(input())] gives the sublist [<first call>, ..., <wanted call>] (or the empty list if the wanted call doesn't exist). Then [-1:] gives the singleton of the last element of that sublist if it exists (the empty list otherwise). * then unpack the sublist.
  • The quine part is based on the quine a="print('a=%r;exec(a)'%a)";exec(a)

Javascript ES6, 130 128 121 120 113 bytes

  • \$\begingroup\$ down to 87: a=[];b=_=>(a.push(prompt()),[a[a.length-prompt()-1]||"",`a=‌​[${a}];b=${b};b()`]);b() \$\endgroup\$ Commented Oct 12, 2015 at 17:36
  • \$\begingroup\$ Oh. Would this? It's 66 bytes: a=[],b=(x,y)=>(a.push(x),`${a[a.length-y-1]||""}\na=[${a}];b=${b}`) _____replace \n with an actual newline. \$\endgroup\$ Commented Oct 12, 2015 at 18:05
  • \$\begingroup\$ Then try a=[],b=(x,y)=>(a.push(x),`${a[a.length-y-1]||""}\na=${JSON.stringify(a)};b=${b}`), which leaves you at 80 bytes (after replacing \n, of course). (If you still have a problem with my code possibly being a REPL snippet, then I have other suggestions :P). \$\endgroup\$ Commented Oct 12, 2015 at 22:31
  • \$\begingroup\$ Some of the last few revisions had noncompliant output formats. Rolled back to last compliant version. \$\endgroup\$ Commented Oct 12, 2015 at 23:31

Julia 1.0, 87 bytes


Try it online!

smilar approach than Jakque's answer but reversing the history order:

  • a stores [<last string>, ..., <first string>, <code>]
  • based on this quine by Dennis
  • the code is stored as a quote, which adds some ugly formatting but is equivalent

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.