タイムマシンクインを書く


21

入力として文字列と整数nを受け取り、出力するプログラムを作成します。

  1. n回前にプログラムに渡された文字列。
  2. 次の呼び出しに使用される新しいプログラム。

プログラムの外部にデータを保存することはできません。また、プログラムはチェーン内の以前のプログラムを呼び出すことはできません。文字列が存在しない場合は、空の文字列を出力します(ただし、次のプログラムは引き続き出力します)。

program_n連続した各プログラムに表記法を使用する実行例(もちろん、[This text is the nth program]実際のコードに置き換えられます。)

$ 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]

新しいプログラムのコードを文字列として出力する必要がありますか?または、ファイルとファイル名の出力に保存する必要がありますか?
メゴ

@Mego文字列として(つまり、STDOUTに)出力します。新しいプログラムをファイルにコピーすることを実装する必要はありません。
アブサン

「何も出力しない」とは、次のプログラムを出力することを意味しますが、(存在しない)文字列は出力しませんか?
メゴ

@Megaはい、それが私が意味したことです。
アブサン

表示しprogram_n+1たい[program_3, One]場合は、出力行にを追加することもできます。両方の出力が標準出力に送られた場合、どのように分離する必要がありますか?また、完全なプログラムの代わりに機能が許可されていますか?
randomra

回答:


4

CJam、25

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

オンラインで試す

説明:

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)

ブロック:

\      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

最後に、要求された文字列(ある場合)、配列の表現、ブロック、文字列「_〜」が自動的に印刷されます。


2

Python、221バイト

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))

これを簡単にテストするには、を使用./thisgolf.py "yourfirststring" | python -c "import sys;exec(sys.stdin.read().split('\n')[1])" "your second string" <N>して、最後のビットを何度でも繰り返します。


2

Python 2、207バイト

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)

私の他の馬の構築されていますが、プログラムを変更し、このタスクはより簡単なので、これをさらにゴルフすることができました。入力をstdinに渡すことができた場合、これはもっと短くなるはずです。


0

ジャバスクリプトES6、130の 128 121 120 113バイト

a=[];b=_=>{a.push(prompt());console.log((a[a.length-prompt()-1]||"")+`
a=`+JSON.stringify(a)+";b="+b+";b()")};b()

87まで:a = []; b = _ =>(a.push(prompt())、[a [a.length-prompt()-1] || ""、 `a = ‌ [$ { a}]; b = $ {b}; b() `]); b()
ママファンロール

ああ。これだろうか?66バイトです:a = []、b =(x、y)=>(a.push(x)、 `$ {a [a.length-y-1] ||" "} \ na = [$ { a}]; b = $ {b} `)_____ \n実際の改行に置き換えます。
ママファンロール

次に、a = []、b =(x、y)=>(a.push(x)、 `$ {a [a.length-y-1] ||" "} \ na = $ {JSON.stringify (a)}; b = $ {b} `)、これにより80バイトが残ります(もちろん\ nを置き換えた後)。(私のコードがおそらくREPLスニペットであるという問題がまだある場合は、他の提案があります:P)
ママファンロール

最後のいくつかのリビジョンの一部には、非準拠の出力形式がありました。最後の準拠バージョンにロールバックしました。
SuperJedi224
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.