Skip to content

Instantly share code, notes, and snippets.

Louis Warren louisswarren

Block or report user

Report or block louisswarren

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View keybase.md

Keybase proof

I hereby claim:

  • I am louisswarren on github.
  • I am louisswarren (https://keybase.io/louisswarren) on keybase.
  • I have a public key whose fingerprint is 1AC6 4B7A 624E 8409 A7D3 DC0F C405 A798 0B57 F6B7

To claim this, I am signing this object:

View mulmatrix.py
def scale_matrix(m, s):
return list(list(s * x for x in row) for row in m)
def print_matrix(m):
print('--' + ' ' * (len(m[0]) * 10) + '--')
for row in m:
print('| ', end='')
for x in row:
print('{:10.2f}'.format(x), end='')
print(' |')
View vertext.py
#!/usr/bin/python3
from sys import argv
text = ''.join(argv[1:]).upper().replace(' ', '')
print(' '.join(text))
print('\n'.join(text[1:]))
@louisswarren
louisswarren / pcat
Last active Mar 27, 2016
Script for viewing long files in tty using only head, tail, echo, bc
View pcat
#!/bin/sh
# Use to view long files without more or less
# One-liner so that it may be easily echo'd to /bin/pcat instead of using ed
# Usage:
# pcat filename pagenum
head -n $(echo "$2 * 20" | bc) $1 | tail -n 20
@louisswarren
louisswarren / converge.py
Last active Apr 30, 2016
Guess what a function converges to
View converge.py
import itertools
def adjacent_pairs(g):
a, b = itertools.tee(g)
next(b)
return zip(a, b)
def converge(f, vals, e=1e-9):
for a, b in adjacent_pairs(vals):
if abs(f(a) - f(b)) < e:
@louisswarren
louisswarren / sin_approx.py
Last active Apr 30, 2016
Test Taylor series approximations to sin
View sin_approx.py
from math import factorial, pi, sin
def max_diff(f, g, v):
return max(abs(f(x) - g(x)) for x in v)
def zigrange(x, s=1.0):
yield 0
for y in range(1, int(x / s + 0.5) + 1):
yield y * s;
yield -y * s;
@louisswarren
louisswarren / named_func.py
Last active Jun 6, 2016
Functions with human-readable representations
View named_func.py
def named_func(f):
return type('named_func', (), {
'__call__': lambda s, *a, **k: f(*a, **k),
'__repr__': lambda s: f.__name__,
'__name__': f.__name__,
'__doc__': f.__doc__,
})()
>>> @named_func
@louisswarren
louisswarren / with_rename.py
Last active Jun 7, 2017
Use with blocks in python to assign a shorter name to a variable
View with_rename.py
class mutate(Exception):
pass
class alias:
def __init__(self, obj):
self.obj = obj
def __enter__(self):
return self.obj
View generics.c
/* See http://stackoverflow.com/a/16523865 */
#include <stdio.h>
#include "sum.h"
define_sum(int);
define_sum(double);
int main(void)
@louisswarren
louisswarren / wifibridge.sh
Last active Jun 26, 2016
Configure device as a wifi bridge
View wifibridge.sh
#!/bin/sh
/sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.0 up
/sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
echo "1" | /usr/bin/tee /proc/sys/net/ipv4/ip_forward
You can’t perform that action at this time.