搜索
首页后端开发Python教程QuineRelay:奎因蛇的诞生

在 Quine 博客系列之前的冒险中,我们探索了如何编写我们自己的 Quine 和内含子。今天我们将了解 QuineRelays 是什么以及如何利用 Introns 来创建它们。

想象一圈程序,每个程序都用不同的语言编写。该循环中的每个程序都有一个目的:打印序列中下一个程序的源代码。最后一个程序通过打印第一个程序的源代码来关闭循环。

本质上,QuineRelays 是一组 n 个程序,采用 n 种不同的语言,例如:

  • 每个程序都会输出下一个程序的源代码。
  • 最后一个程序输出第一个程序的源代码。 这里考虑循环链表。最后一个例子令人震惊!

让我们看一些例子来更好地理解这个概念。


二阶继电器

这个二阶 QuineRelay 具有一个 JavaScript 程序,可以打印 Python 程序,然后打印回原始 JavaScript 程序。这是一对动态的自我复制组合。
JavaScript → Python ⥀

JavaScript:在线尝试!

console.log((q=_=>`print(${JSON.stringify(`console.log((q=${q+[]})())`)})`)())

Python:在线尝试!

print("console.log((q=_=>`print(${JSON.stringify(`console.log((q=${q+[]})())`)})`)())")

三阶继电器

使用三阶 QuineRelay 会让事情变得更加有趣。这个从 Haskell 程序开始,输出 Python 程序,Python 程序输出 Ruby 程序,最后 Ruby 程序循环回原始 Haskell 程序。
Haskell → Python2 → Ruby ⥀

Haskell:在线尝试!

q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
main=q "q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']" "def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'" "def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"

Python2:在线尝试!

def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'
q("def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'","def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end","q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']")

Ruby:在线尝试!

def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end
q("def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end","q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']","def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'")

4阶继电器

这是一个四阶 QuineRelay:Ruby → Java → C# → Python ⥀
GitHub


不同文化之间有一些共同的象征意义。其中之一是衔尾蛇,它在埃及、希腊、罗马、印度教、西伯利亚、挪威、非洲和南美神话中都有提及。衔尾蛇是一个古老的符号,描绘了一条蛇或龙吞噬自己的尾巴,代表着创造和毁灭的永恒循环。

QuineRelay: Birth of the Quine serpent

在北欧神话中,耶梦加得是一条巨蛇,是洛基和女巨人安格尔博达的中间的孩子。奥丁将耶梦加得扔进米德加德(人类王国)周围的海洋中,这条蛇在那里变得如此巨大,以至于它包围了世界并抓住了自己的尾巴。由于它围绕着米德加德(地球),因此被称为世界之蛇 - 衔尾蛇。耶梦加得释放尾巴是诸神黄昏(世界的最后之战)开始的标志之一。

Quine 中继非常巧妙地体现了这种象征意义,因为循环中的每个程序都会产生下一个程序,最后才会重生,因此被昵称为 Ouroboros 程序。


128阶衔尾蛇继电器

请坐在座位上。这是一个 128 阶 Ouroboros QuineRelay。是的,你没看错。 128!!!
Ruby → Rust → Scala → ...(其他 120 个)... → Python → R → Ratfor → rc → REXX ⥀
GitHub

QuineRelay: Birth of the Quine serpent

好像这还不够令人印象深刻,它还包括一个复活节彩蛋。缩小后的原始 Ruby 代码包含一条 Ouroboros 龙!

QuineRelay: Birth of the Quine serpent


编写你自己的 QuineRelay

让我们启动之前编写的Python内含子,尝试将其变成二阶QuineRelay。

Python Intron:在线尝试!

intron = 'wubbalubbadubdub'
data = "print('intron =', repr(intron)); print('data =', repr(data)); print(data)"
print('intron =', repr(intron)); print('data =', repr(data)); print(data)

利用内含子的魔力,我们现在可以轻松地将不同语言的同级奎因的代码部分放入内含子中。生成以下形式的程序:

Python:

intron = "code part of sibling"
data = "code part of self"
print('intron =', repr(intron)); print('data =', repr(data)); print(data)

因为,每个变量只是充当不同奎因的数据。让我们将 data 和 intron 分别重命名为 d1 和 d2。

Python:

d1 = "code part of self"
d2 = "code part of sibling"
print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d1)

现在,d2充当内含子,但上面的程序仍然尝试打印self的代码部分。为了让它打印下一个的源代码,让我们在最后打印(d2)而不是打印(d1)。

Python:

d1 = "code part of self"
d2 = "code part of sibling"
print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d2)

我们已经知道 d1 的内容只是第 3 行的副本。但是我们还没有 d2 的内容。

比如说,我们想用 JavaScript 创建一个 QuineRelay。我们用 JS 写一个类似的内含子。

JavaScript:

d1 = "code part of sibling"
d2 = "code part of self"
console.log(`d1 = ${JSON.stringify(d1)}`); console.log(`d2 = ${JSON.stringify(d2)}`); console.log(d1);

现在,上面 JS 内含子的第 3 行,就是我们想要的兄弟程序的 代码
将彼此的代码粘贴为对方的内含子。
笔记。我们需要在 js 中添加 d1 + '' 以避免一些引用不匹配

Python: Try it online!

d1 = "print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d2)"
d2 = "console.log(`d1 = ${JSON.stringify(d1)}`); console.log(`d2 = ${JSON.stringify(d2)}`); console.log(d1 + '');"
print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d2)

JavaScript: Try it online!

d1 = "print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d2)"
d2 = "console.log(`d1 = ${JSON.stringify(d1)}`); console.log(`d2 = ${JSON.stringify(d2)}`); console.log(d1 + '');"
console.log(`d1 = ${JSON.stringify(d1)}`); console.log(`d2 = ${JSON.stringify(d2)}`); console.log(d1 + '');

There you go. It's a proper 2nd order QuineRelay! A Python program, that prints a JavaScript program, that prints the original Python program back in a cycle.


Creating a QuineRelay is an exercise in creative coding and understanding how different languages represent and manipulate strings. It involves weaving together introns from various programs, each containing the code to replicate its next neighbour.

At its core, an nth-order relay is a game of n clever ways to escape quotes across n programming languages.

Stay tuned for the next post on MultiQuine!


Sources and references:

  • Ouroboros programs, wiki page.
  • A Third Order Quine in Three Languages, blog by sigfpe.
  • Chain Quine, GitHub repo by Ibragimov Ruslan.
  • 128 Quine Relay, GitHub repo by Yusuke Endoh.
  • QuineRelay: Birth of the Quine serpent, self-referencing blog where a word in each n-order example hyperlinks to the next, cyclically. (May not work in dev.to; checkout the canonical blog)
  • Jörmungandr, wiki page.
  • Ouroboros, wiki page.
  • Art of Code, talk by Dylan Beattie

以上是QuineRelay:奎因蛇的诞生的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
可以在Python数组中存储哪些数据类型?可以在Python数组中存储哪些数据类型?Apr 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您尝试将错误的数据类型的值存储在Python数组中,该怎么办?如果您尝试将错误的数据类型的值存储在Python数组中,该怎么办?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python标准库的哪一部分是:列表或数组?Python标准库的哪一部分是:列表或数组?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

您应该检查脚本是否使用错误的Python版本执行?您应该检查脚本是否使用错误的Python版本执行?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

在Python阵列上可以执行哪些常见操作?在Python阵列上可以执行哪些常见操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些类型的应用程序中,Numpy数组常用?在哪些类型的应用程序中,Numpy数组常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什么时候选择在Python中的列表上使用数组?您什么时候选择在Python中的列表上使用数组?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用