Skip to content

Instantly share code, notes, and snippets.

@irokez
Created February 10, 2012 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irokez/1793986 to your computer and use it in GitHub Desktop.
Save irokez/1793986 to your computer and use it in GitHub Desktop.
Apply embed.ly
problem 1:
>>> fc = lambda n: 1 if n <= 1 else n * fc(n - 1)
>>> def r(n):
... s = str(n)
... d = 0
... for i in s:
... d += int(i)
... return d
>>> R = lambda n: r(fc(n))
>>> i = 0
>>> while True:
... i += 1
... x = R(i)
... if x == 8001:
... break
>>> print(i)
787
problem 2:
>>> import xml.parsers.expat
>>> u = 'http://apply.embed.ly/static/data/2.html'
>>> from urllib.request import urlopen
>>> html = urlopen(u)
>>> h = html.read()
>>> h = h.encode('utf-8')
>>> d = 0
>>> from collections import defaultdict
>>> t = defaultdict(list)
>>> def start_h(n, a):
... global d, t
... d += 1
... t[n] = d - 1
...
>>> def end_h(n):
... global d
... d -= 1
...
>>> p = xml.parsers.expat.ParserCreate()
>>> p.StartElementHandler = start_h
>>> p.EndElementHandler = end_h
>>> p.Parse('<?xml version="1.0">' + h)
1
>>> def sd(l):
... m = sum(l)/len(l)
... d = 0
... for i in l:
... d += (m - i) ** 2
... d /= len(l) - 1
... return sqrt(d)
...
>>> from math import sqrt
>>> sd(t['p'])
1.4303439938073574
problem 3:
>>> fs = 0
>>> for i in range(1, 901):
... fs += 900/i
...
>>> fs
6642.149292810678
>>> fc = 0
>>> for i in range(1, 901):
... fc += 900/i
... if fc > fs / 2:
... break
...
>>> i
22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment