# 自我修改相乘

33

...至少对于“自我修改”的某种定义

# 任务

• 字符串的`B`长度至少为1。

• 对于每种`n ≥ 0`，字符串都是您选择的编程语言中的有效程序（表示完整的可运行程序或函数定义）。上标表示的重复，因此，这意味着字符串，，，等。每一个程序需要一个字符串作为输入，并返回一个串作为输出。`ABnC``AC``ABC``ABBC``ABBBC`

• 对于任何`m, n ≥ 0`程序，如果程序使用输入运行，它将返回。对于非这种形式的输入，程序可能会执行任何操作，包括崩溃。`ABmC``ABnC``ABm*n+1C`

``````AC(AC) -> ABC
ABC(AC) -> ABC
ABBBBBC(AC) -> ABC
AC(ABC) -> ABC
AC(ABBBBC) -> ABC
ABC(ABC) -> ABBC
ABBC(ABC) -> ABBBC
ABBBBC(ABBBC) -> ABBBBBBBBBBBBBC
ABBBC(ABBBBBBC) -> ABBBBBBBBBBBBBBBBBBBC
``````

16

# CJam，9个 8字节

``````A: 1
B: 0
C:  r,(#0q
``````

CJam解释器中在线尝试。

### 怎么运行的

``````(ABcode) e# Push the integer 10 ** len(Bcode).
<SP>     e# Noop. Separates (AB) and C for input reading.
r        e# Read the first whitespace-separated token from STDIN (ABinput).
,(       e# Push the string length minus 1: len(Binput)
#        e# Power operator: 10 ** len(Bcode) len(Binput) # ->
e#   (10 ** len(Bcode)) ** len(Binput) = 10 ** (len(Bcode) * len(Binput))
0        e# Push an additional 0 to complete len(Bcode) * len(Binput) + 1 zeroes.
q        e# Read the remaining input (C).
``````

12

# CJam，15 13 11字节

``````A: rl"
B: <SP>
C: <LF>",(*SNq
``````

CJam解释器中在线尝试。

### 怎么运行的

``````e# A

r     e# Read a whitespace-separated token from STDIN.
e# This reads the input up to the first space, but does not consume it.
l     e# Read the rest of the first line from STDIN.
e# This reads up to the first linefeed and consumes it.

"     e# Initiate a string.

e# B

<SP>  e# Fill the string with as many spaces as there are copies of B.

e# C

<LF>" e# Terminate the string with a linefeed.
e# This serves as a delimiter for the `l' command.
,(    e# Compute the length of the string minus 1 (to account for the LF).
*     e# Repeat the string read by `l' that many times.
SN    e# Push a space and a linefeed.
q     e# Read the remaining input (i.e., the second line) from STDIN.
``````

Optimizer

9

# 珀斯10

``````A: w*\0hl*w[<newline>
B: 0
C: <empty>
``````

9

# 视网膜，25 19字节

``````A: ]\]<LF>
B: ]]
C: <LF>m`^]*\$<LF>]\$0]

<LF> stands for newline
``````

``````]\]
]]
m`^]*\$
]\$0]
``````

• 改变输入`AB^mC``AB^(m*n)C`改变每`B``B^n`

• `]\]`匹配`B`输入中的每个内容，并且通过在模式行中转义来匹配其他内容
• `]]...]]``B^n`
• 变化`B^(m*n)``B^(m*n+1)`

• `m`^]*\$`走线只`]`
• `]\$0]``]]`向其添加额外的一对，使得该行与第一个正则表达式不匹配

8

# Python 3，51个字节

``````A: lambda s:s[:28]+"x"*(1+len("
B: x
C: ")*(len(s)-51))+s[-23:]``````

``````>>> f=lambda s:s[:28]+"x"*(1+len("xx")*(len(s)-51))+s[-23:]
>>> f('lambda s:s[:28]+"x"*(1+len("xxx")*(len(s)-51))+s[-23:]')
'lambda s:s[:28]+"x"*(1+len("xxxxxxx")*(len(s)-51))+s[-23:]'``````

J中的相同方法：

# J，35个字节

``````A: (19{.]),('x'#~1+(#'
B: x
C: ')*35-~#),_16{.]
``````

5

# CJam，22岁

``````A:<empty>
B:{])`\,q,K/(*))*"_~"}
C:{])`\,q,K/(*))*"_~"}_~
``````

``````ABBC(ABC) -> ABBBC
``````

``````{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}_~
``````

``````{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}_~
``````

``````{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}_~
``````

``````AC :{])`\,q,K/(*))*"_~"}_~
ABC:{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}_~
``````

``````{])`\,q,K/(*))*"_~"}

{                  }    e# This is a code block. Alone, this does nothing except
e# pushing this block to stack as is
]                      e# Wrap everything on stack in an array
)`                    e# Take out the last part and convert it to its string representation
\,                  e# Take length of remaining array
q,K/              e# Read the input, take its length and int divide by K (i.e. 20)
(*            e# Decrement and multiply by the array length on stack
))          e# Add two to the product
*         e# Repeat the string representation on stack that many times
"_~"     e# Put this string on stack
``````

``````{])`\,q,K/(*))*"_~"}_~                      e# Copy the block and run it
{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}~   e# Block is copied, run it
{      ...         } ])                     e# Wrapped array has the block in it.
`\,                  e# Stringify it and take length of remaining = 0
q,K/              e# No input so 0
(*))          e# 0 * -1 = 0. 0 + 2 = 2
*         e# Repeat the stringified block 2 times:
e# "{])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}"
"_~"     e# Put this string. Program ends, so print stack:
e# {])`\,q,K/(*))*"_~"}{])`\,q,K/(*))*"_~"}_~
``````

3

`f`是一个接受并返回的函数`String`

``````A:_:b="
B:
C: ";f s|a:c<-words s=unwords\$a:(drop 50s>>b):c
``````

• `_:b=" "`将字符串文字中除第一个空格之外的所有空格都分配给`b`，使其等于程序的m B副本。
• `s`是输入字符串。`a:c<-words s`将其拆分为以空格分隔的单词，从而`a`成为A并`c`成为包含C的单词的列表。由于`words`挤压了多个空格（程序的其余部分避免了），因此B副本被忽略。
• `drop 50s`是一个长度等于输入中B副本数n的字符串。`drop 50s>>b`串联的许多副本`b`，并给出mn个空格。
• `unwords\$a:(drop 50s>>b):c`将所有字符串与空格连接在一起。由于`(drop 50s>>b)`列表中插入了一个额外的“单词” ，因此也有一个额外的连接空间，自动将+1加到乘法中。

2

# Matlab，85岁

``````A:    "X=strsplit(input('','s'));m=0 "
B:    "+1 "
C:    ";[X{1},32,repmat(['+1',32],1,m*(length(X)-2)+1),X{end}]"
``````

1

# C（gcc），81个字节

``````A: m;f(char*s){m=strrchr(s+66,32)-s-65;printf("%.66s%*s",s,m*strlen("
B: <SPACE>
C: ")+16,s+m+66);}``````

Zgarb

1

# TI-Basic（83系列），65字节

``````Input Str1:sub(Str1,1,27:For(I,0,(length(Str1)-55)(length("
``````

B区：

``````X
``````

``````Y")-1:Ans+"X":End:Ans+sub(Str1,length(Str1)-27,28
``````

0

# Java 11，135 65 + 26 = 91字节

``s->{var p=s.split("\\(\"|\"\\.");return p[0]+"(\"B"+p[1].repeat("``

``B``

C

``".length())+'"'+'.'+p[2];}``

``````s -> { // lambda taking and returning a String
var p = s.split("\\(\"|\"\\."); // split input into parts A, B^n, C (where n may be 0) at the "(\"" and "\"."
return p[0] + "(\"B" + // put it back together: A plus the part the split shaved off, plus an extra B ...
p[1].repeat("BBB".length()) + // ... plus B^(n*m)
'"' + '.' + p[2]; // plus C, with the part the split shaved off reattached
}``````