November 2010
10 posts
1 tag
Run Length Encoding using Lisp
;; This is my first attempt at any Lisp programming. ;; Lisp programming has a long tradition, so I'm sure ;; some things could be better in this program. ;; ;; My goal was to write something in Lisp, so I wrote ;; Run Length Encoding. ;; ;; -James ;; drop - drops the first n elements in a list ;; The same function exists in Haskell. (defun drop (mylist n) (if (eq n 0) mylist ...
Nov 25th
1 tag
Eyeballs that Follow Your Cursor in Processing
void setup() { size(400, 400); background(204); } void draw() { drawTwoEyesLookingAtMouse(200, 200); } void drawTwoEyesLookingAtMouse(int cx, int cy) { int eye_y = 200; int left_eye_x = cx - 70; int right_eye_x = cx + 70; drawEyeLookingAtMouse(left_eye_x, eye_y); drawEyeLookingAtMouse(right_eye_x, eye_y); } void drawEyeLookingAtMouse(int cx, int cy) { int...
Nov 23rd
1 note
1 tag
Adding Two Numbers in a Java Swing GUI
/** * Last Friday I was approached in my office by our * CS Department's Head Lab Assistant and the TA * responsible for teaching our Java 3 course. * * The Head Lab Assistant told me that the TA (standing * right beside her) was sick and unable to teach. * Someone needed to fill in the course for him. * I'm the fall back up guy for these kinds of emergencies, * so it fell to me to teach...
Nov 22nd
2 notes
1 tag
Stripping Non-printable Characters out of a File...
#include <stdio.h> /* * I had a PDF document that I needed to post * onto a Wiki, so I used a tool called * pdftotext to convert the PDF into text. * * The pdftotext converter converted several * of the characters to unprintable onces. * The characters don't show up when you read * the file, but they still exist. This was * creating problems for the Wiki I was using. * * I needed...
Nov 20th
1 tag
Filtering a BibTeX file by terms used in the...
import sys import re """ Author: James Church Date: November 16, 2010 Filename: filterByKeyword.py This program will gather up all of the citations in a BibTeX file that share a keyword passed to it on the command line. For example, say you have a list of citations in a file "docs.bib", but some docs are tagged with the keyword "ants" and other are tagged with the keyword "beetles". Let's get a...
Nov 19th
1 note
1 tag
Simple Heap Data Structure in Ruby
class Heap # Initialize our Heap # To use: my_heap = Heap.new def initialize @storage = Array.new end # Test if the Heap is empty. # To use: my_heap.empty? def empty? return @storage.length == 0 end # Insert an item into the Heap # Increases the size of the Heap by 1 # To use: my_heap.insert(item) def insert(item) @storage...
Nov 15th
1 note
1 tag
Computing the First 20 Fibonacci Numbers in Erlang
#!/usr/bin/env escript fib(0) -> 0; fib(1) -> 1; fib(N) -> fib(N-1) + fib(N-2). print_list([H|T]) -> io:format("~p~n", [H]), print_list(T); print_list([]) -> true. main(_) -> print_list( [ fib(X) || X <- lists:seq(1,20) ] ).
Nov 14th
2 notes
1 tag
Floyd's Algorithm in C++
/* Floyd's Shortest Distance Algorithm This function takes an adjacency matrix as input. Then the algorithm modifies the cells in the adjacency matrix so that all of the edge placements are replaced with the shortest distance to that edge. The internal "graph" structure is overwritten, so you'd better make a copy! */ void floyds(int **graph, int size) { for (int k = 0; k...
Nov 12th
1 tag
Nov 12th
33,246 notes
1 tag
The Coin Change Problem in Python
def coin_change(amount, denominations, collection=[]): """ Given an amount of money and some coin denominations, this function prints every combination of coins needed to express the desired amount using the input denominations. amount - an amount, in the smallest unit. denominations - a list of coin denominations, from largest to smallest. """ for coin in...
Nov 1st