Created
April 18, 2011 08:57
-
-
Save anonymous/925031 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| 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 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 main(): | |
| if len(sys.argv) == 2: | |
| print troll_at_level(int(sys.argv[1])) | |
| else: | |
| print "USAGE: %s INTEGER" % (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