Created
April 18, 2011 15:47
-
-
Save anonymous/925584 to your computer and use it in GitHub Desktop.
This generates an arbitrary-depth fizz-buzz-generator and is also a quine.
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/python | |
import sys | |
code = """#!/usr/bin/python | |
import sys | |
code = {0}{1}{0} | |
def initial_solution(): | |
result = [] | |
for i in range(1, 101): | |
if i % 5 == 0 and i % 3 == 0: | |
result.append("%d fizz buzz" % (i,)) | |
elif i % 5 == 0: | |
result.append("%d fizz" % (i,)) | |
elif i % 3 == 0: | |
result.append("%d buzz" % (i,)) | |
else: | |
result.append("%d" % (i,)) | |
return result | |
def add_troll_layer_to(solution): | |
new_solution = [] | |
new_solution.append("public static void main(String... args) {{") | |
for line in solution: | |
new_solution.append('\\tSystem.out.println("%s");' % (escape(line),)) | |
new_solution.append("}}") | |
return new_solution | |
def escape(line): | |
result = "" | |
for character in line: | |
if character == '\\t': result += "\\\\t" | |
elif character in ['"', '\\\\']: result += "\\\\" + character | |
else: result += character | |
return result | |
def escape_code(code): | |
result = "" | |
for character in code: | |
if character == '\\t': result += "\\\\t" | |
elif character == '\\\\': result += "\\\\\\\\" | |
else: result += character | |
return result | |
def troll_at_level(level): | |
current_solution = initial_solution() | |
for i in range(0, level): | |
current_solution = add_troll_layer_to(current_solution) | |
return "\\n".join(current_solution) | |
def print_self(): | |
print code.format('{0}', escape_code(code)) | |
def main(): | |
if len(sys.argv) == 2: | |
argument = sys.argv[1] | |
if argument == "Q" or argument == "q": | |
print_self() | |
else: | |
print troll_at_level(int(argument)) | |
else: | |
print "USAGE: %s INTEGER or Q" % (sys.argv[0],) | |
if __name__ == "__main__": | |
main()""" | |
def initial_solution(): | |
result = [] | |
for i in range(1, 101): | |
if i % 5 == 0 and i % 3 == 0: | |
result.append("%d fizz buzz" % (i,)) | |
elif i % 5 == 0: | |
result.append("%d fizz" % (i,)) | |
elif i % 3 == 0: | |
result.append("%d buzz" % (i,)) | |
else: | |
result.append("%d" % (i,)) | |
return result | |
def add_troll_layer_to(solution): | |
new_solution = [] | |
new_solution.append("public static void main(String... args) {") | |
for line in solution: | |
new_solution.append('\tSystem.out.println("%s");' % (escape(line),)) | |
new_solution.append("}") | |
return new_solution | |
def escape(line): | |
result = "" | |
for character in line: | |
if character == '\t': result += "\\t" | |
elif character in ['"', '\\']: result += "\\" + character | |
else: result += character | |
return result | |
def escape_code(code): | |
result = "" | |
for character in code: | |
if character == '\t': result += "\\t" | |
elif character == '\\': result += "\\\\" | |
else: result += character | |
return result | |
def troll_at_level(level): | |
current_solution = initial_solution() | |
for i in range(0, level): | |
current_solution = add_troll_layer_to(current_solution) | |
return "\n".join(current_solution) | |
def print_self(): | |
print code.format('"""', escape_code(code)) | |
def main(): | |
if len(sys.argv) == 2: | |
argument = sys.argv[1] | |
if argument == "Q" or argument == "q": | |
print_self() | |
else: | |
print troll_at_level(int(argument)) | |
else: | |
print "USAGE: %s INTEGER or Q" % (sys.argv[0],) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment