Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python One Line Quine

#1
Python One Line Quine

<div><p>Most computer scientists, programmers, and hackers don’t even know the meaning of the word “Quine” in the context of programming. So, first things first:</p>
<h2>What is a Quine?</h2>
<p><strong>Roughly speaking, a quine is a self-reproducing program: if you run it, it generates itself.</strong></p>
<p>Here’s a great definition:</p>
<p><em>:<strong>quine</strong>: /kwi:n/ /n./ [from the name of the logician Willard van Orman Quine, via Douglas Hofstadter] A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement. (<a href="http://www.nyx.net/~gthompso/quine.htm" target="_blank" rel="noreferrer noopener" title="http://www.nyx.net/~gthompso/quine.htm">source</a>)</em></p>
<blockquote class="wp-block-quote">
<p>The name “quine” was coined by Douglas Hofstadter, in his popular science book <em>Gödel, Escher, Bach</em>, in honor of philosopher Willard Van Orman Quine (1908–2000), who made an extensive study of indirect self-reference, and in particular for the following paradox-producing expression, known as Quine’s paradox.</p>
<p><cite><a href="https://en.wikipedia.org/wiki/Quine_(computing)" target="_blank" rel="noreferrer noopener" title="https://en.wikipedia.org/wiki/Quine_(computing)">Wikipedia</a></cite></p></blockquote>
<p>The shortest possible quine is the following empty program:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""> </pre>
<p>The program is self-reproducing because the output of the program is the program itself. Go ahead and run it in your own shell! <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f609.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>Let’s dive into a collection of Python Quines to demonstrate how they work!</p>
<h2>Python One-Liner Quine 1</h2>
<p>Here’s a short one-liner Quine, I found at <a href="https://cs.lmu.edu/~ray/notes/quineprograms/" target="_blank" rel="noreferrer noopener" title="https://cs.lmu.edu/~ray/notes/quineprograms/">this </a>resource:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">s='s=%r;print(s%%s,sep="")';print(s%s,sep="")</pre>
<ul>
<li>You may want to improve your <a href="https://pyformat.info/" target="_blank" rel="noreferrer noopener" title="https://pyformat.info/">string formatting skills</a> to really understand this one-liner. </li>
<li>The <a href="https://blog.finxter.com/the-separator-and-end-arguments-of-the-python-print-function/" title="Python Print Function [And Its SECRET Separator &amp; End Arguments]" target="_blank" rel="noreferrer noopener">separator argument of the print function</a> avoids printing a new-line character at the end. </li>
<li>The <code>%r</code> format specifier automatically supplies single quotes.</li>
</ul>
<p>Here’s the code in an interactive shell so that you can play with this Quine in your browser:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/SnoopyEnviousComments?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p><em><strong>Exercise</strong>: Run the code. What’s the output? Can you explain why?</em></p>
<h2>Python One-Liner Quine 2</h2>
</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/ForkedCloudyDestination?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
</div>


https://www.sickgaming.net/blog/2020/07/...ine-quine/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016