Skip to content

Instantly share code, notes, and snippets.

View ianbishop's full-sized avatar

Ian Bishop ianbishop

View GitHub Profile
@ianbishop
ianbishop / knapsack
Created February 11, 2011 00:45
knapsack.js
v = values from item1..itemn
w = weights from item1..itemn
n = number of items
W = maximum weight of knapsack
m[n, n] = array(int, int)
function knapsack
for w=0..W
m[0, w] = 0
for i=1 to n
@ianbishop
ianbishop / Mozart fix
Created March 16, 2011 23:40
Fixes 'Aquamacs missing' error in Mozart 1.4 for Mac OSX
detectAquamacs()
{
if [ ! -e "/Applications/Aquamacs.app" ]; then
`$CD ok-msgbox --no-cancel --icon info --timeout 20 --title "Aquamacs not found" --text "This application is needed to run Mozart" --informative-text "You can download it from http://aquamacs.org/"`
exit 2
fi
}
exec /Applications/Aquamacs.app/Contents/MacOS/Aquamacs --eval '(setq load-path (cons "'$OZHOME'/share/elisp" load-path))' -l oz.elc -f run-oz $2 &
@ianbishop
ianbishop / Mozart_fix.sh
Created March 16, 2011 23:40
Fixes 'Aquamacs missing' error in Mozart 1.4 for Mac OSX
detectAquamacs()
{
if [ ! -e "/Applications/Aquamacs.app" ]; then
`$CD ok-msgbox --no-cancel --icon info --timeout 20 --title "Aquamacs not found" --text "This application is needed to run Mozart" --informative-text "You can download it from http://aquamacs.org/"`
exit 2
fi
}
exec /Applications/Aquamacs.app/Contents/MacOS/Aquamacs --eval '(setq load-path (cons "'$OZHOME'/share/elisp" load-path))' -l oz.elc -f run-oz $2 &
@ianbishop
ianbishop / napsack.js
Created March 21, 2011 03:39
nap sack
v = values from item1..itemn
w = weights from item1..itemn
n = number of items
W = maximum weight of knapsack
m[n, n] = array(int, int)
function knapsack
for w=0..W
m[0, w] = 0
for i=1 to n
@ianbishop
ianbishop / napsack.js
Created March 21, 2011 03:40
nap sack
v = values from item1..itemn
w = weights from item1..itemn
n = number of items
W = maximum weight of knapsack
m[n, n] = array(int, int)
function knapsack
for w=0..W
m[0, w] = 0
for i=1 to n
@ianbishop
ianbishop / napsackrecurrence.js
Created March 21, 2011 03:44
napsack recurrence
if i = 0 or w = 0
m[i, w] = 0
if w[i] > w
m[i - 1, w]
if w[i] <= w
max(m[i - 1, w], m[i - 1, w - w[i]] + v[i])
@ianbishop
ianbishop / MessagePassing1.rb
Created March 23, 2011 22:30
Simple message passing example in Ruby (1)
class Repeater
@@subscribers = []
# Add the new subscriber to our list of subscribers
def subscribe(newSubscriber)
@@subscribers << newSubscriber
end
# Send the message to all of our subscribers
def broadcast(message)
@ianbishop
ianbishop / MessagePassing2.rb
Created March 23, 2011 23:13
More advanced example of Simple Message Passing
class GameState
include Subscriber
def initialize(repeater)
@repeater = repeater
@repeater.subscribe(self)
end
# Handle only messages of attempt move, ignore the rest
def accept(message)
@ianbishop
ianbishop / SimpleMultiplayer.rb
Created March 23, 2011 23:41
A new module for the simple message passing system
class GameMultiplayer
include Subscriber
def initialize(repeater)
@repeater = repeater
@repeater.subscribe(self)
end
def accept(message)
case message[:command]
@ianbishop
ianbishop / revised_repeater.rb
Created March 24, 2011 22:35
Revised repeater that uses send/respond_to? instead
class Repeater
@@subscribers = []
def subscribe(newSubscriber)
@@subscribers << newSubscriber
end
def broadcast(message)
@@subscribers.each { |subscriber|
subscriber.send(message[:command], message[:options]) if subscriber.respond_to? message[:command]