Skip to content

Instantly share code, notes, and snippets.

View cslarsen's full-sized avatar

Christian Stigen Larsen cslarsen

View GitHub Profile
@cslarsen
cslarsen / sendeth.py
Created April 27, 2014 07:14
One way of sending raw Ethernet packets in Python
"""Demonstrates how to construct and send raw Ethernet packets on the
network.
You probably need root privs to be able to bind to the network interface,
e.g.:
$ sudo python sendeth.py
"""
from socket import *
@cslarsen
cslarsen / objc-gcc.m
Created March 20, 2012 19:23
How to compile Objective-C on the command line on Mac OS X
/*
* To compile objective-c on the command line:
*
* gcc -framework Foundation objc-gcc.m
*
* You may have to link with -lobjc or other libs,
* as required.
*/
#import <Foundation/Foundation.h>
@cslarsen
cslarsen / free-body.mp
Created January 7, 2012 10:40
Free body diagram with frictional forces made in MetaPost
% MetaPost free body diagram with frictional forces
%
% Placed in the public domain by the author; Christian Stigen Larsen
% 2011-10-07
%
% To render:
% $ mptopdf free-body.mp
%
beginfig(1)
@cslarsen
cslarsen / animals-game.py
Created January 7, 2012 08:23
Python animals guessing game
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
"""
Written by Christian Stigen Larsen, http://csl.sublevel3.org
Placed in the public domain by the author
This is a version of the classic game of guessing which animal you
are thinking about. It uses a binary tree to guess.
@cslarsen
cslarsen / heterogeneous-array.go
Last active January 1, 2023 13:41
How to create heterogeneous arrays in Go / golang.
/*
* Example of heterogeneous arrayo in golang.
*
* The trick is simply to create an array that accepts elements that conform
* to the naked interface (an interface with no requirements).
*
* Expected output:
*
* $ go run array.go
* [1 2 3.14 hey {10 20}]
@cslarsen
cslarsen / human-numbers.cpp
Created February 20, 2012 18:44
Convert big number to human readable format
/*
* A simple way to format numbers as human readable strings.
* E.g., 123456789 ==> 123 million
*
* Written by Christian Stigen Larsen
* http://csl.sublevel3.org
*
* Placed in the public domain by the author, 2012
*/
@cslarsen
cslarsen / ipv4.py
Created January 11, 2012 15:16
Two small Python functions to convert IPv4 address to integer and vice-versa
#!/usr/bin/env python
"""Functions to convert IPv4 address to integer and vice-versa.
Written by Christian Stigen Larsen, http://csl.sublevel3.org
Placed in the public domain by the author, 2012-01-11
Example usage:
$ ./ipv4 192.168.0.1 3232235521
192.168.0.1 ==> 3232235521
@cslarsen
cslarsen / euler_phi.cpp
Created January 18, 2012 20:16
Euler's totient function phi --- a fast implementation in C++
/*
* Euler's totient function phi(n).
* http://en.wikipedia.org/wiki/Euler%27s_totient_function
*
* This is an *EXTREMELY* fast function and uses
* several tricks to recurse.
*
* It assumes you have a list of primes and a fast
* isprime() function. Typically, you use a bitset
* to implement the sieve of Eratosthenes and use
@cslarsen
cslarsen / binary_gcd.cpp
Created January 18, 2012 20:03
Binary gcd algorithm in C++ using iteration and bit shifts
/*
* The binary gcd algorithm using iteration.
* Should be fairly fast.
*
* Put in the public domain by the author:
*
* Christian Stigen Larsen
* http://csl.sublevel3.org
*/
int binary_gcd(int u, int v)
@cslarsen
cslarsen / palindrome-linear.cpp
Created March 2, 2011 19:56
Linear scan for palindromes
/*
* Find the longest palindrome in the text.
*
* This is Greplin's first challenge, and I originally solved it in Python.
*
* This algorithm is linear on the average input, but has a quadratic
* worst case running time. There exists an even better algorithm, but
* this should do.
*
* There might also be a small error below, but you get the general idea.