21
\$\begingroup\$

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
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]
\$\endgroup\$
  • \$\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\$ – Mego Oct 12 '15 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 Oct 12 '15 at 6:05
  • \$\begingroup\$ By "output nothing", do you mean output the next program, but not the (non-existant) string? \$\endgroup\$ – Mego Oct 12 '15 at 6:08
  • \$\begingroup\$ @Mega Yes, that's what I meant. \$\endgroup\$ – absinthe Oct 12 '15 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 Oct 12 '15 at 6:10
4
\$\begingroup\$

CJam, 25

L{\_l~(>1<lN+a@+`@"_~"}_~

Try it online

Explanation:

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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:])
r([""],r)

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.

\$\endgroup\$
0
\$\begingroup\$

Javascript ES6, 130 128 121 120 113 bytes

a=[];b=_=>{a.push(prompt());console.log((a[a.length-prompt()-1]||"")+`
a=`+JSON.stringify(a)+";b="+b+";b()")};b()
\$\endgroup\$
  • \$\begingroup\$ down to 87: a=[];b=_=>(a.push(prompt()),[a[a.length-prompt()-1]||"",`a=‌​[${a}];b=${b};b()`]);b() \$\endgroup\$ – Mama Fun Roll Oct 12 '15 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\$ – Mama Fun Roll Oct 12 '15 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\$ – Mama Fun Roll Oct 12 '15 at 22:31
  • \$\begingroup\$ Some of the last few revisions had noncompliant output formats. Rolled back to last compliant version. \$\endgroup\$ – SuperJedi224 Oct 12 '15 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.