Name: FIZ. Z. MCBUZZMASTER 2012-03-16 18:52
Write FIZZBUZZ in the most convoluted way you can think of.
Rules:
1. Your fizzbuzz implementation must work
2. Convolution =/= obfuscation: your code should be legible but should either work in a stupid way or be hard to understand (but NOT to read! that's too easy)
3. No statements with no effect (I mean like sticking "i + 1;" in the middle of a C function, not side-effects - functional languages are fine too).
Mine:
Rules:
1. Your fizzbuzz implementation must work
2. Convolution =/= obfuscation: your code should be legible but should either work in a stupid way or be hard to understand (but NOT to read! that's too easy)
3. No statements with no effect (I mean like sticking "i + 1;" in the middle of a C function, not side-effects - functional languages are fine too).
Mine:
#!/usr/bin/env python
import socket
import sys
HOST = "127.0.0.1"
PORT = 9001
def client(n):
global HOST, PORT
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
while n > 0:
sock.send(str(n))
data = sock.recv(20)
sys.stdout.write("%02d: %s" % (n, data))
n -= 1
sock.close()
def server():
global HOST, PORT
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(1)
while True:
conn, addr = sock.accept()
conn.setblocking(1)
while True:
i = 0
try:
i = int(conn.recv(20))
except:
break
s = ""
if (i % 3) == 0:
s += "fizz"
if (i % 5) == 0:
s += "buzz"
conn.send(s + "\n")
if i == 0:
break
conn.close()
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: %s <client|server> [number]" % sys.argv[0])
exit(1)
if sys.argv[1] == "client":
if len(sys.argv) < 3:
print("Usage: %s <client|server> [number]" % sys.argv[0])
exit(1)
client(int(sys.argv[2]))
elif sys.argv[1] == "server":
server()
else:
print("Usage: %s <client|server> [number]" % sys.argv[0])
exit(1)