当前位置:网站首页>可以“繁殖”的程序
可以“繁殖”的程序
2022-07-31 05:09:00 【AtomicGu】
Python描述
编写这样一个py脚本文件:
- 每次运行这个脚本文件,在工作目录下以随机文件名创建一个新文件,文件内容为这个py脚本自身。
- 不能使用sys.argv[0],即不能借助操作系统的帮助。
一种解答:
m = r"""from random import sample chrs = [chr(i) for i in range(ord("A"), ord("Z") + 1)] with open("".join(sample(chrs, 5)) + ".py", "w") as f: print("m=r\"\"\"{}\"\"\"".format(m), file=f) print(m, file=f) """
from random import sample
chrs = [chr(i) for i in range(ord("A"), ord("Z") + 1)]
with open("".join(sample(chrs, 5)) + ".py", "w") as f:
print("m=r\"\"\"{}\"\"\"".format(m), file=f)
print(m, file=f)
C语言描述
如果语言不提供原始字符串功能呢?
编写一个.c文件并编译成.exe程序,要求该程序能实现如下效果:
运行该.exe文件,在标准输出流中打印自己的源代码(即.c文件内容)
一种解答:
#include <stdio.h>
const char* m =
"\";\n\nvoid\nprint_raw_str(const char* s)\n{\n char c = *s;\n while (c != "
"'\\0') {\n switch (c) {\n case '\\\\':\n "
"printf(\"\\\\\\\\\");\n break;\n case '\\\"':\n "
"printf(\"\\\\\\\"\");\n break;\n case '\\n':\n "
"printf(\"\\\\n\");\n break;\n default:\n printf(\"%c\", "
"c);\n }\n c = *++s;\n }\n}\n\nint\nmain()\n{\n printf(\"%s\", "
"\"#include <stdio.h>\\n\\nconst char* m = \\\"\");\n print_raw_str(m);\n "
"printf(\"%s\", m);\n}\n";
void
print_raw_str(const char* s)
{
char c = *s;
while (c != '\0') {
switch (c) {
case '\\':
printf("\\\\");
break;
case '\"':
printf("\\\"");
break;
case '\n':
printf("\\n");
break;
default:
printf("%c", c);
}
c = *++s;
}
}
int
main()
{
printf("%s", "#include <stdio.h>\n\nconst char* m = \"");
print_raw_str(m);
printf("%s", m);
}
亚当和夏娃
上两节给出的代码就像他们“代码族”的亚当和夏娃,它们都可以实现“鸡生蛋,蛋生鸡”的怪诞循环,对于py脚本,则可以直接实现“无性生殖”。
严格来说,上面两节给出的代码并非完全符合要求的代码。
这样做即可得到真正符合要求的代码(以C语言形式为例):
- 使用编译器编译上面给出的代码:
得到a.exe文件$gcc a.c
- 运行a.exe文件,其输出才是完全符合要求的源代码:
得到b.c文件,b.c文件才是真正符合要求的代码。$a.exe > b.c
如何写出可以“繁殖”的程序
可以给繁殖过程加入各种复杂功能,比如:“一次生俩”,下面是教程。
以Python为例,只需两步:
- 写出如下代码框架
两行print是核心、关键。m = r"""""" # 插入其它代码 print("m=r\"\"\"{}\"\"\"".format(m)) print(m) # 插入其它代码
- 把第二行到最后一行复杂粘贴到m中
m = r"""# 插入其它代码 print("m=r\"\"\"{}\"\"\"".format(m)) print(m) # 插入其它代码 """ # 插入其它代码 print("m=r\"\"\"{}\"\"\"".format(m)) print(m) # 插入其它代码
- 大功告成,这个程序就可以在stdout中打印自己的源代码了。
由于C语言没有原始字符串的机制,所以要自己插入一个“反转义”的函数。
意义
什么程序会要做这种奇怪的事情呢?
答案是“计算机病毒”,或者“人工智能”。
有些人会说,借助操作系统提供的argv[0]不就行了吗,搞那么复杂干嘛?
那么问题来了:“没有操作系统就没有计算机病毒吗?计算机病毒必须依靠操作系统才能存活吗?”
答案是否定的,按这种方式,裸机+汇编编程都能写出可自我繁殖的程序。
附注
考察生命科学相关的一个悖论:
- 生物都是机器
- 生物都能自再生(繁殖)
- 机器不能自再生
关于第三句话:
例如汽车和生产汽车的自动生产线,设计生产线比设计汽车更加复杂,因为生产线中包含了汽车的设计。
因为任何生产机器B的机器A,A肯定比B更复杂。一个机器不会比他自己更复杂,所以没有机器能制造它自己,故机器不能自再生。
答案是:这个悖论是不成立的,机器可以自再生。
边栏推荐
猜你喜欢
随机推荐
torch.normal function usage
Paginate the list collection and display the data on the page
tf.keras.utils.pad_sequences()
Typec手机有线网卡网线转网口转接口快充方案
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
C语言实验四 循环结构程序设计(一)
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
基于web3.0使用钱包Metamask的三方登陆
pycharm专业版使用
数据库上机实验2 单表查询和嵌套查询
MySQL transaction isolation level, rounding
MySQL (updating)
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
matlab abel变换图片处理
[mysql improves query efficiency] Mysql database query is slow to solve the problem
Interviewer, don't ask me to shake hands three times and wave four times again
分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
【LeetCode-SQL每日一练】——2. 第二高的薪水
ES source code API call link source code analysis