Skip to content

Instantly share code, notes, and snippets.

View TheRayTracer's full-sized avatar

Simon Flannery TheRayTracer

  • Melbourne, Australia
View GitHub Profile
@TheRayTracer
TheRayTracer / main.cpp
Last active May 8, 2020 09:22
An example implementation to solve the knapsack problem. This includes a 2D and 1D array solution.
#include <iostream>
#include <fstream>
#include <vector>
#include <cassert>
namespace std { }
using namespace std;
struct item
{
@TheRayTracer
TheRayTracer / main.cpp
Created December 16, 2012 05:55
A simple implementation of Kruskal's algorithm to determine a Minimum Spanning Tree. Also added an optional parameter to extend the base algorithm to supported a k number of clusters.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
namespace std { }
using namespace std;
@TheRayTracer
TheRayTracer / main.cpp
Created December 7, 2012 09:04
A quick implementation of Prim's algorithm to find the MST of a (connected) input graph.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cassert>
namespace std { }
using namespace std;
struct edge
@TheRayTracer
TheRayTracer / main.cpp
Created December 5, 2012 09:16
An example of a greedy algorithm to implement a non-optimal scheduling solution.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
namespace std { }
using namespace std;
@TheRayTracer
TheRayTracer / main.cpp
Last active October 6, 2015 20:48
Example code to find the nth node from the end of a linked list using only one pass.
#include <iostream>
#include <assert.h>
namespace std { };
using namespace std;
struct node
{
node* next;
size_t data;
@TheRayTracer
TheRayTracer / main.sql
Created May 10, 2012 13:37
Example SQL setup using triggers to provide logging and auditing services. This file was prepared for SQL Lite, and the syntax would differ slightly for SQL Server and MySQL.
-- Work-a-round for SQL lite's lack of ENUM support.
CREATE TABLE enum(id INTEGER NOT NULL PRIMARY KEY, action VARCHAR(255) NOT NULL);
INSERT INTO enum VALUES (0, "INSERT");
INSERT INTO enum VALUES (1, "UPDATE");
INSERT INTO enum VALUES (2, "DELETE");
PRAGMA foreign_keys = ON;
-- Create a table to audit.
CREATE TABLE inventory(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(255) NOT NULL, description VARCHAR(255), cost INTEGER NOT NULL, quantity INTEGER NOT NULL);
@TheRayTracer
TheRayTracer / main.cmd
Created May 9, 2012 13:13
A simple C++ implementation of the Levenshtein distance algorithm to measure the amount of difference between two strings.
main.exe kitten sitting
@TheRayTracer
TheRayTracer / main.py
Last active October 3, 2015 13:58
Examples of handy functions including Factorial, Fibonacci and palindrome finding functions using both recursion and loops.
def get_factorial_recursively(i):
f = 1
if i > 1:
f = f * i * get_factorial_recursively(i - 1)
return f
def get_factorial_iteratively(i):
f = 1
for j in range(2, i + 1):
f = f * j
@TheRayTracer
TheRayTracer / main.cpp
Created April 21, 2012 12:58
The following is a naive implementation of the Rabin-Karp algorithm to search for sub strings within larger strings using a rolling hash. The standard strstr() function will outperform this proof-of-concept implementation, but this implementation is in th
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <cstring>
#include <cassert>
#include <sys/stat.h>
#define WIN32_LEAN_AND_MEAN
@TheRayTracer
TheRayTracer / QuickSort.txt
Created April 9, 2012 09:47
This is a Python proof-of-concept quick sort implementation to compare the importance of selecting a quality pivot.
2148
9058
7742
3153
6324
609
7628
5469
7017
504