Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
#Newbie programmer | |
def factorial(x): | |
if x == 0: | |
return 1 | |
else: | |
return x * factorial(x - 1) | |
print factorial(6) | |
#First year programmer, studied Pascal |
/*global ko, Chart */ | |
(function(ko, Chart) { | |
ko.bindingHandlers.chartType = { | |
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { | |
if (!allBindings.has('chartData')) { | |
throw Error('chartType must be used in conjunction with chartData and (optionally) chartOptions'); | |
} | |
}, |
from geventwebsocket.handler import WebSocketHandler | |
from gevent.pywsgi import WSGIServer | |
from flask import Flask, request, render_template | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return render_template('index.html') |
def degrees_to_cardinal(d): | |
''' | |
note: this is highly approximate... | |
''' | |
dirs = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", | |
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"] | |
ix = int((d + 11.25)/22.5) | |
return dirs[ix % 16] |
Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
# 12345 -> '54321' [the returned value is a string] | |
reverse = lambda x: str(x)[::-1] | |
# translate each element of x to a string and join eg. [18, 3, 2] -> '1832' | |
strjoin = lambda x: "".join(map(str, x)) | |
# '1832' -> 1 + 8 + 3 + 2 -> 14 | |
sumstr = lambda x: sum(int(n) for n in x) | |
# compute the number which must be added to `x` to get to the nearest 10 |
""" | |
Installation for debian based distros | |
You will of course need the tex library installed: | |
$ sudo apt-get install texlive | |
And SWIG is required for dealing with texcaller's 'C' wrapper. | |
$ sudo apt-get install swig | |
Download the texcaller package from github and 'make' the |
import math | |
import numpy | |
import matplotlib.pyplot | |
import scipy.stats | |
# See [Hull], Chapter 13. The pricing formulae are given in Section 13.8 | |
def d1(S0, K, r, sigma, T): | |
return (math.log(S0 / K) + (r + sigma**2 / 2) * T) / (sigma * math.sqrt(T)) |