Skip to content

Instantly share code, notes, and snippets.

@kalebr
kalebr / cumulative_probability_at_least
Last active December 22, 2015 11:18
R - cumulative probability of at least number of positive outcomes
cumProb <- function(min, trials, prob) {
x = 0
if(min == 0) {
error("Mininum must be greater than 0")
}
if(min > trials) {
return(0)
}
@kalebr
kalebr / lnoe_dice_combat_sim
Last active December 22, 2015 12:28
last night on earth combat sim
fight <- function(numZombieDie, numHumanDie) {
zd <- expand.grid(rep(list(1:6), numZombieDie))
hd <- expand.grid(rep(list(1:6), numHumanDie))
results <- vector()
for (z in 1:nrow(zd)) {
for (h in 1:nrow(hd)) {
zroll <- as.numeric(zd[z, ])
hroll <- as.numeric(hd[h, ])
@kalebr
kalebr / poke_clusters.R
Last active December 23, 2015 03:49
heirarchial clustering of pokemon by attributes
library(RCurl)
library(reshape2)
library(plyr)
library(ggplot2)
pokemon <- read.csv(text = getURL("https://raw.github.com/veekun/pokedex/master/pokedex/data/csv/pokemon_species.csv", ssl.verifypeer=FALSE))
pokemon_stats <- read.csv(text = getURL("https://raw.github.com/veekun/pokedex/master/pokedex/data/csv/pokemon_stats.csv", ssl.verifypeer=FALSE))
stat_names <- read.csv(text = getURL("https://raw.github.com/veekun/pokedex/master/pokedex/data/csv/stat_names.csv", ssl.verifypeer=FALSE))
pokemon <- ddply(pokemon, .(evolution_chain_id), subset, evolves_from_species_id == max(evolves_from_species_id, na.rm=T))
@kalebr
kalebr / R - smooth density heatmap
Last active January 29, 2020 17:43
Smoothed heatmap without chunkiness of stat_bin2d
library(ggplot2)
df <- data.frame(x=rnorm(1000), y=rnorm(1000))
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
ggplot(df[sample(1:nrow(df), 1000), ], aes(x, y)) +
stat_density2d(geom="tile", aes(fill=..density.., alpha=sqrt(sqrt(..density..))), contour=FALSE, n=100) +
scale_alpha(range = c(0.5, 1.0)) + scale_fill_gradientn(colours = jet.colors(10), trans="sqrt")
@kalebr
kalebr / list_criteria_count.cs
Created October 11, 2013 01:20
c# count elements below value in list
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
@kalebr
kalebr / index_vector_color_select.R
Created May 20, 2014 18:25
select colors using a vector as indicies
n = 5
g = sample(1:n, 20, replace=T)
c = sapply(g, FUN=function(x) rainbow( n )[[x]])
@kalebr
kalebr / flag_encoding_to_int.R
Created June 11, 2014 15:03
encoding a set of flags as an int in R
gen_id <- function( v, silent=FALSE ) {
v = sort(v)
z <- unlist(lapply( which( chars %in% v) - 1, FUN=function(x) { bitwShiftL(1, x)}))
total <- sum(z)
if(!silent) {
print(v)
print(z)
print(total)
}
}
@kalebr
kalebr / int_coding.py
Created June 20, 2014 17:56
Encoding a selection of sets into an int and decoding.
def make_id( v, bit_map ):
bits_used = 0
_id = 0
for pair in zip( v, bit_map ):
_id += pair[0] * (2 ** bits_used )
bits_used += pair[1]
return _id
@kalebr
kalebr / class_static_variable.py
Created June 27, 2014 15:23
Demo of python static class variable
import random
class Student():
x = 17
def __init__(self):
self.age = random.randint(0, 20)
s = Student()