Skip to content

Instantly share code, notes, and snippets.

@creature
creature / chris.py
Last active August 29, 2015 14:14
A demo Python 3 program that demonstrates some list filtering & simple constraint solving.
import random
import itertools
# The function we want to minimise. Our function takes a 4-tuple of values.
def f(tup):
w, x, y, z = tup # This is called "tuple unpacking"
return w + x + y + z
# Here's a function that takes a tuple and returns True if it fits the constraints,
# False otherwise. I've decided that there should be no more than 2 values > 50 in our
class Node
attr_accessor :value # The value we're storing
attr_accessor :next_node # The next node in the list, if any
def initialize(new_value)
@value = new_value
@next_node = nil
end
end
@creature
creature / generator.rb
Created April 20, 2014 00:05
A script that generates an XML file suitable for use by the Gnome Background Properties to change wallpapers.
files = []
puts "<background>
<starttime>
<year>2009</year>
<month>08</month>
<day>04</day>
<hour>00</hour>
<minute>00</minute>
<second>00</second>
</starttime>"
@creature
creature / linkedlist.rb
Last active June 10, 2020 07:52
A Ruby demo of a linked list, transformed into a doubly-linked list and some performance improvements.
class Node
attr_accessor :value
attr_accessor :next, :previous
def initialize(value)
@value = value
@next = nil
@previous = nil
end
end
@creature
creature / morethanoneway.rb
Created January 16, 2014 14:14
A demonstration that there's more than one way to reverse a string.
require 'test/unit'
class TestStringReversal < Test::Unit::TestCase
def setup
@methods = self.methods.select {|name| name.to_s.include? "reverse"}
end
def test_string_reversal
@methods.each {|method|
@creature
creature / hotels.rb
Created January 14, 2014 15:10
A simple example of object orientation
class Hotel
DOUBLE_ROOM_CAPACITY = 2
SINGLE_ROOM_CAPACITY = 1
def initialize(double_count, single_count)
@rooms = []
@capacity = 0
@guestbook = []
double_count.times do
@creature
creature / .bashrc
Created October 15, 2013 12:23
Displays some extra information in your bash prompt. If you're connected via ssh, then highlight the hostname; if the previous command has a non-zero exit code, show the ending character in red; if you're on a git branch, show its name at the start of the prompt; and if your branch has uncommitted changes, add an asterisk within the branch label.
# Add this to your .bashrc.
parse_git_changes() {
if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then
git update-index -q --refresh
DIFFS=$(git diff-index --no-ext-diff --name-only HEAD --)
LOCALS=$(git ls-files --exclude-standard --others)
if [[ "$DIFFS$LOCALS" ]]; then
echo "*"
fi
fi