Created
August 24, 2012 16:16
-
-
Save aaronjgreenberg/3452441 to your computer and use it in GitHub Desktop.
Writing Quines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# This program prints out its own source code | |
# =========================================== | |
import sys | |
def quine(filename): | |
f = open(filename, 'r') | |
for line in f.readlines(): | |
print line.strip('\n') | |
if __name__ == '__main__': | |
quine(sys.argv[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# This program prints its own source code | |
# ======================================= | |
def quine(): | |
code = [ | |
'#!/usr/bin/env python', | |
'# This program prints its own source code', | |
'# =======================================', | |
'', | |
'def quine():', | |
' code = [', | |
' ]', | |
' for i in range(6):', | |
' print code[i]', | |
' for i in range(len(code)):', | |
' print " " + chr(39) + code[i] + chr(39) + ","', | |
' for i in range(6, len(code)):', | |
' print code[i]', | |
'', | |
'quine()', | |
] | |
for i in range(6): | |
print code[i] | |
for i in range(len(code)): | |
print " " + chr(39) + code[i] + chr(39) + "," | |
for i in range(6, len(code)): | |
print code[i] | |
quine() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A couple of quines I wrote, just for fun. The first is probably the easiest quine you can write that's not an empty file. The second is a more traditional quine, modeled after the example on Wikipedia.