Calculate factorial in Python 🙃
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
def factorial(x): """ Work out x! (with a little help from the google calculator...) """ import re import urllib import time time.sleep(2) class AppURLopener(urllib.FancyURLopener): def __init__(self, *args): # *Cough* *Cough* self.version = 'Mozilla 1.3' urllib.FancyURLopener.__init__(self, *args) opener = AppURLopener() page = opener.open('http://www.google.com/search?q=%d!' % x).read() result = re.findall('<b>%d ! = (.*?)</b>' % x, page) if result: return int(result[0].replace('<font size=-2> </font>', '')) else: raise Exception, "Google not willing today!:\n\n %s" % page |