We haven't had one in a while so let's have a new coding contest. I'm open to suggestions for the challenge, but if we don't have any good suggestions we will default to something stupid like "implement cowsay in erlang shitty language X".
Name:
Leah Culver!1LEahRIBg.2009-11-29 18:49
Here is a nearly complete version of cowsay implemented in python, although it differs slightly from the original.
* It doesn't support cowthink (although this shouldn't be difficult to add).
* The help returned by -h is sparse and the docstrings non-existant.
* It doesn't support the -l flag which displays the COWPATH as I don't support cowfiles.
* The -f flag opens up a regular file and uses that as a message, whereas the original uses a cowfile instead.
* It keeps the bug/feature of the original in that you can pass in a 1 character tongue string or eye string.
I'm sure there are others and bugs that I haven't found(well, I didn't do any unit tests ;), but there you go. Before you ask, the code is ugly, because I'm lazy and didn't really give a shit.
from optparse import OptionParser
from collections import namedtuple
def chunks(s,n):
l = []
while len(s) > n:
l.append(s[:n])
s = s[n:]
l.append(s)
return l
def strip_extra_whitespace(s):
l = []
prev = False
for c in s:
if c.isspace():
if prev:
continue
prev = True
else:
prev = False
l.append(c)
return "".join(l)