Skip to content

Instantly share code, notes, and snippets.

View jcchurch's full-sized avatar

James Church jcchurch

View GitHub Profile
@jcchurch
jcchurch / symmetric.m
Created June 16, 2011 14:59
Determine if a graph is symmetric
function yes = symmetric(g)
% Determins if an adjacency matrix
% is symmetric or not.
yes = 1;
[height width] = size(g);
if height ~= width
yes = 0;
end
for i = 1:height
import java.util.*;
public class Tribute {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int datasets = scan.nextInt();
scan.nextLine();
for (int i = 0; i < datasets; i++) {
String original = scan.nextLine();
@jcchurch
jcchurch / linear_regression.m
Created October 13, 2011 15:45
Linear Regression in Matlab
function [m b] = linear_regression(X, Y)
n = length(Y);
EX = sum(X);
EY = sum(Y);
EX2 = sum(X.*X);
EXY = sum(X.*Y);
m = (n*EXY - EX*EY) / (n*EX2 - EX*EX);
b = (EY - m*EX) / n;
end
@jcchurch
jcchurch / birthdays.py
Created October 17, 2011 15:07
Coupon Collector's Problem applied to Birthdays
# This is not the actual Birthday Paradox Problem.
# This is something similar suggested by Taylor Smith.
#
# How many people do you need in a room to have a 50%
# chance of having every possible birthday represented
# within the room?
#
# After typing all of this out, I realized that it's
# the same as the Coupon Collector's Problem.
@jcchurch
jcchurch / Honeycomb.java
Created October 18, 2011 16:15
Detection of Hexes in a Cartesian Plane
import java.util.Scanner;
// This is another problem submission from a programming practice.
// The problem description can be found here:
// http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=374&page=show_problem&problem=2697
// Solved this one in approximately 1 hour.
public class Honeycomb {
public static void main(String[] args) {
@jcchurch
jcchurch / pig.py
Created November 5, 2011 18:04
Convert the binary in the Phreaknic program to ASCII
#!/usr/bin/env python
message = "010100000110100101100111001000000111100101101111011101010010011101110010011001010010000001100001001000000110010001101001011000110110101100101110"
bytes = []
i = 0
while i < len(message):
bytes.append( message[i:i+8] )
i += 8
@jcchurch
jcchurch / distancePointToLineSegment.m
Created March 2, 2012 22:42
Distance from point to the nearest point along a line segment.
function distance = distancePointToLineSegment(p, a, b)
% p is a point
% a is the start of a line segment
% b is the end of a line segment
pa_distance = sum((a-p).^2);
pb_distance = sum((b-p).^2);
unitab = (a-b) / norm(a-b);
@jcchurch
jcchurch / thereIsACycle.m
Created March 7, 2012 16:42
Determine if a graph contains a cycle.
function yes = thereIsACycle(G)
% G is an n-by-n association matrix.
% An edge is defined by anything in that
% matrix that is not 0 and not positive infinity.
% Elements along the diagonal are ignored.
% Returns 1 if there is a cycle. 0 otherwise.
yes = 0;
n = size(G, 1);
seen = zeros(1, n);
@jcchurch
jcchurch / tricenters.m
Created June 30, 2012 20:50
Finding the circumcircles of a list of triangles
function [CENTERS, RADII] = tricenters(TRI, X, Y)
% This function takes the same parameters as the function 'trimesh' does, since they require the same inputs.
% INPUTS:
% TRI: A set of triangle indices. (N-by-3, where N is the number of triangles.)
% X: The X coordinate of the triangle indices.
% Y: The Y coordinate of the triangle indices.
%
% OUTPUTS:
% CENTERS: A 2D matrix of N-by-2, where N is the number of triangles in TRI.
% Each entry is a X, Y coordinate of a circumcenter.
@jcchurch
jcchurch / drawCircles.m
Created June 30, 2012 20:56
Drawing Circles in Matlab
function drawCircles(center,radius,NOP,style)
%------------------------------------------------------------------------------
% CIRCLE(CENTER,RADIUS,NOP,STYLE)
% This routine draws a circle with center defined as
% a vector CENTER, radius as a scaler RADIS. NOP is
% the number of points on the circle. As to STYLE,
% use it the same way as you use the rountine PLOT.
% Since the handle of the object is returned, you
% use routine SET to get the best result.
%