Skip to content

Instantly share code, notes, and snippets.

View RobertSudwarts's full-sized avatar

RobertSudwarts

  • Vienna, Austria
View GitHub Profile
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
@RobertSudwarts
RobertSudwarts / ko-chart.js
Last active August 29, 2015 14:25 — forked from jmhdez/ko-chart.js
Custom bindings to use Chart.js with Knockout.js
/*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')
@RobertSudwarts
RobertSudwarts / deg_to_cardinal.py
Created June 7, 2015 16:18
[python] degrees to cardinal directions
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]
@RobertSudwarts
RobertSudwarts / tree.md
Last active August 29, 2015 14:17 — forked from hrldcpr/tree.md

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@RobertSudwarts
RobertSudwarts / mod10_algo.py
Last active August 29, 2015 14:16
python implementation of mod10 check digit for invoices etc
# 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
@RobertSudwarts
RobertSudwarts / BrowserPDFRender.py
Last active August 29, 2015 14:11
generate PDFs on the fly (ie in a browser) using LaTeX, with Turbogears, mako templates and texcaller
"""
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
@RobertSudwarts
RobertSudwarts / hull_black_scholes.py
Last active August 29, 2015 14:04
options pricing
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))