Skip to content

Instantly share code, notes, and snippets.

View cslarsen's full-sized avatar

Christian Stigen Larsen cslarsen

View GitHub Profile
@cslarsen
cslarsen / permut.cpp
Last active September 5, 2015 07:35
Permutations in C++
/*
* Print all permutations of string ``s´´. (C++)
*
* Solved as a counting problem and coded in a hurry.
*
* A more elegant and recursive solution:
* http://nicolas-lara.blogspot.com/2009/01/permutations.html
*/
#include <stdio.h>
@cslarsen
cslarsen / revlink.c
Created June 30, 2010 00:39
Reverse singly linked list in C
@cslarsen
cslarsen / palindrome.cpp
Created March 2, 2011 13:17
Find longest palindrome in text
/*
* Find the longest palindrome in the text.
*
* This is Greplin's first challenge, and I originally solved it in Python.
*
* The algorithm is not optimal, but simple on the eyes and easy to understand
* On big inputs it's possible to use an approximately linear algorithm.
*
* Christian Stigen Larsen
*/
@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.
@cslarsen
cslarsen / prime_sieve.h
Created October 25, 2011 21:18
A simple sieve of Eratosthenes using bitsets and vectors
/*
* A simple sieve of Eratosthenes using bitsets, written by Christian Stigen Larsen
* You are free to use this code for anything (i.e., public domain).
*/
#ifndef INC_PRIMES_H
#define INC_PRIMES_H
#include <vector>
#include <algorithm>
@cslarsen
cslarsen / gist:1314379
Created October 25, 2011 21:34
FizzBuzz, can you spot any bit patterns?
/*
* FizzBuzz
*/
#include <stdio.h>
char* bits(int n)
{
#define BITS 8
#define BIT(x) (x? '1' : ' ');
@cslarsen
cslarsen / gist:1517172
Created December 24, 2011 11:35
Two quines in C

In "Reflections on trusting trust" --- http://cm.bell-labs.com/who/ken/trust.html --- Ken Thompson urges the reader to try write a quine. If you haven't done so yet, please try before reading on.

Here are two quines I made (without cheating), loosely based on the same idea, that I wrote in C. The first one is macro-based, utilizing the ability for macros to quote parameters:

#define Q(x) #x;x
char *q=Q(main(){printf("#define Q(x) #x;x\nchar *q=Q(");printf(q);printf(")\n");})

Compiling and running it produces an exact copy of itself:

~/devel/quine csl$ gcc --no-warnings shortq.c -o shortq && ./shortq

@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 / 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 / prime_sieve_heap.hpp
Created January 10, 2012 18:52
Sieve of Eratosthenes using bitsets on the heap
/*
* A simple sieve of Eratosthenes using bitsets, written by Christian Stigen Larsen
* You are free to use this code for anything (i.e., public domain).
*
* This sieve allocates the bitset on the heap, so you can calculate a lot
* of prime numbers.
*/
#ifndef INC_PRIMES_H
#define INC_PRIMES_H