My Hobby: Solving XKCD puzzles on my lunch break
July 9, 2007 at 11:16 am | In Geek Humor, Python | 1 CommentI like geek comics. I especially like geek comics that have an embedded NP-Complete problem:

There are 2 solutions:
Solution #1:
<MenuItem Mixed Fruit 215>
<MenuItem Hot Wings 355>
<MenuItem Hot Wings 355>
<MenuItem Sampler Plate 580>
Solution #2:
<MenuItem Mixed Fruit 215>
<MenuItem Mixed Fruit 215>
<MenuItem Mixed Fruit 215>
<MenuItem Mixed Fruit 215>
<MenuItem Mixed Fruit 215>
<MenuItem Mixed Fruit 215>
<MenuItem Mixed Fruit 215>
This isn't "as fast as possible" to be sure, but it should be a general solution.
#!/usr/bin/env python
"""Attempt to solve XKCD from July 9 2007"""
#All monetary values are in pennies to avoid floating point errors
# (Yea, I could use decimal.Decimal but it's SLOW!)
import operator
class MenuItem:
def __init__(self, name, price):
self.name = name
self.price = price
def __add__(self, other):
if hasattr(other,"name") and hasattr(other,"price"):
#This is a MenuItem
return self.price + other.price
else:
return self.price + other
def __repr__(self):
return "
Update July 10: Visitor wac pointed out that my program is pretty inefficient. I agree - I did it really quick on my lunch break yesterday. For order values above $20 it becomes an intractable solution pretty quick. Here is a much more efficient solution that implements value caching (at the cost of requiring more memory than the previous solution):
#!/usr/bin/env python
"""Attempt to solve XKCD from July 9 2007
This is a new version that uses a cache for calculations
"""
#All monetary values are in pennies to avoid floating point errors
# (Yea, I could use decimal.Decimal but it's SLOW!)
import operator
import sys
class MenuItem:
def __init__(self, name, price):
self.name = name
self.price = price
def __add__(self, other):
if hasattr(other,"name") and hasattr(other,"price"):
#This is a MenuItem
return self.price + other.price
else:
return self.price + other
def __repr__(self):
return "
With the improved version I can calculate order values up to $75 in under a minute. That's interesting in and of itself really, because although the second algorithm is much more efficient it would still be practically impossible to calculate an order value of $1000. That's the nature of NP problems for you.
LOLPython
June 6, 2007 at 1:17 pm | In Geek Humor, Python | 1 Comment
IN MAI time GIMME localtime LIKE TIMEZOR
IZ __name__ KINDA LIKE "__main__"?
VISIBLE "YOUZ GOT CHEEZBURGERS?"
CATURDAY CAN HAS FIV
TODAYZ CAN HAS TIMEZOR THING LOOK AT SIKS!
YOUGOTZ CAN HAS raw_input THING
IZ YOUGOTZ OWN lower THING KINDA LIKE "yes"?
IZ TODAYZ KINDA LIKE CATURDAY?
MYLOOPZ CAN HAS THR33
WHILE I CUTE?
VISIBLE "TODAYZ CATURDAY! I GETZ ALL YOUS CHEEZBURGERS!"
MYLOOPZ THROWZ AWAY ONCE
IZ MYLOOPZ SMALL LIKE EASTERBUNNY?
KTHXBYE
NOPE?
VISIBLE "O RLY? CAN I HAS YOUS CHEEZBURGER?"
NOPE?
VISIBLE "I SAW WHAT YOU DID, YOU ATE MY CHEEZBURGERS!"
Yea, you're probably saying to yourself, "WTF IS THAT?"
It's LOLPython, the geekiest, funniest thing I've seen all week!
The LOLPython interpreter translates a variation of LOLCode into standard python. LOLCode itself is pretty neat, but because LOLPython is based on Python, it automatically inherits all of the Python library too. Here's the above code after translation:
from time import localtime as TIMEZOR
if __name__ == '__main__' :
print 'YOUZ GOT CHEEZBURGERS?'
CATURDAY = 5
TODAYZ = TIMEZOR ()[ 6 ]
YOUGOTZ = raw_input ()
if YOUGOTZ . lower ()== 'yes' :
if TODAYZ == CATURDAY :
MYLOOPZ = 3
while 1:
print 'TODAYZ CATURDAY! I GETZ ALL YOUS CHEEZBURGERS!'
MYLOOPZ -= 1
if MYLOOPZ <= 0 :
break
else :
print 'O RLY? CAN I HAS YOUS CHEEZBURGER?'
else :
print 'I SAW WHAT YOU DID, YOU ATE MY CHEEZBURGERS!'

Yep, I'm a nerd
January 11, 2006 at 12:36 pm | In Geek Humor, Python | No CommentsI was just reading today's Foxtrot.

Check this out:
bin = ['01011001','01001111','01010101',
'01001110','01000101','01010010','01000100']
for b in bin:
print chr(int(b,2)),
I Love comics that are made just for me. ![]()
Powered by WordPress.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^
XML Sitemap

