Skip to content

Instantly share code, notes, and snippets.

View glesica's full-sized avatar

George Lesica glesica

View GitHub Profile
@glesica
glesica / petersburg.r
Created August 26, 2012 03:37
R functions to simulate the Petersburg Paradox.
pburg <- function() {
p <- 1
while (sample(1:2, 1)[1] == 1) {
p <- p * 2
}
return(p)
}
ipburg <- function(n) {
r <- sapply(1:n, function(x) { return(pburg()) })
[xcb] Unknown sequence number while appending request
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
invader: ../../src/xcb_io.c:160: append_pending_request: Assertion `!xcb_xlib_unknown_seq_number' failed.
Program received signal SIGABRT, Aborted.
0x00007ffff75ab445 in __GI_raise (sig=<optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 0x00007ffff75ab445 in __GI_raise (sig=<optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
# value_iteration.r
# George Lesica
# CSCI 555 - FA 2012
# Homework 5
# Solution to problem 3
INTENDED <- 0.8
LEFT <- 0.1
RIGHT <- 0.1
@glesica
glesica / particle.r
Created November 26, 2012 17:52
Particle filter assignment.
# particle.r
# George Lesica
# CSCI 555 - FA 2012
# Homework 7.5
# Solution to problem 2
samp <- function(S) {
return(sample(S$X, length(S$X), replace=T, prob=S$W))
}
convolution <- function(A, B) {
# Computes the convolution of two vectors.
#
# Args:
# A, B: column vectors representing polynomial coefficients.
#
# Returns:
# The coefficient vector resulting from multiplying the polynomial
# represented by A by the polynomial represented by B.
Ap <- c(A, B*0)
perceptron <- function(x, y, w, a, verbose=F) {
# Uses the Perceptron algorithm to find a weight vector that
# satisfies the given data.
#
# Args:
# x: an m by n matrix representing the data under study
# y: objective vector of m elements
# w: initial guess for the weight vector, n elements
# a: learning rate, should be in (0,1]
# verbose: echo information w at each iteration (default: False)
@glesica
glesica / sentiment.r
Created February 24, 2013 23:30
A really simple sentiment analysis function implemented in R that illustrates some of the nifty features of the R language.
sentiment <- function(str, words, weights=NULL) {
if (is.null(weights)) {
weights <- rep(1, length(words))
}
if (length(words) != length(weights)) {
stop('Length of words and weights not equal')
}
str.wts <- rep(0, length(str))
@glesica
glesica / update-hosts.sh
Last active January 18, 2024 17:37
A quick shell script that will automatically update a Linux HOSTS file to block ads and malicious domains.
#!/usr/bin/env sh
# Filename: update-hosts.sh
# Author: George Lesica <george@lesica.com>
# Description: Replaces the HOSTS file with a customized version that blocks
# domains that serve ads and malicious software, creating a backup of the old
# file.
HOSTS_URL="http://someonewhocares.org/hosts/zero/hosts"
NEW_HOSTS="hosts"
@glesica
glesica / example1.py
Created April 20, 2013 18:08
Simple drawing example using Pycairo.
from math import pi
from cairo import SVGSurface, Context, Matrix
WIDTH = 6 * 72
HEIGHT = 4 * 72
s = SVGSurface('example1.svg', WIDTH, HEIGHT)
c = Context(s)
# Transform to normal cartesian coordinate system
@glesica
glesica / contour.jl
Created September 17, 2013 02:15
Contouring algorithm in Julia.
# contour(A, v)
# Implements the Marching Squares contouring algorithm (see:
# https://en.wikipedia.org/wiki/Marching_squares for details). The
# matrix `A` is an m x n scalar field and the scalar `v` is the
# value to be contoured. The return value is an (m-1) x (n-1) matrix
# of contour line type indices (see the Wikipedia article).
# TODO: Implement in parallel
function contour(A, v)
rows, cols = size(A)