Skip to content

Instantly share code, notes, and snippets.

View smehan's full-sized avatar

Shawn Mehan smehan

View GitHub Profile
@smehan
smehan / cum_percentage.R
Created August 25, 2017 21:40
Generate a cumulative percentage of values in a DF
# Generate a cumulative percentage for all values in a DF
library(matrixStats)
df <- data.frame(Alphabet = c("A", "A","A","A","A", "B", "B", "B"),
Value = c(1,1, 2,2,3,2,2,4))
tbl <- prop.table(table(df), 1) * 100
tbl[] <- rowCumsums(tbl)
@smehan
smehan / order_factors.R
Created August 23, 2017 18:29
Reorder factors by frequencies
library(forcats)
set.seed(555)
df <- data.frame(x=factor(sample(as.character(1:10), 100, replace=TRUE)))
table(df$x)
1 10 2 3 4 5 6 7 8 9
9 10 12 14 10 10 5 12 8 10
levels(fct_rev(fct_infreq(df$x)))
@smehan
smehan / aggregate.R
Created August 11, 2017 23:03
aggregate data and create new field based on the aggregated data.
# sample data
DF = read.table(text="name cluster count
F00851.3 20 2
F00851.2 20 2
F00851 20 2
F00851.8 20 2
F00851.4 20 2
F00851.5 20 2
F00851.1 20 2
F00851.6 21 2
@smehan
smehan / heap.py
Created July 26, 2017 23:05
general heapq wrapper for non-class module
class Heap(object):
def __init__(self, data=None, key=lambda x: None):
self.heap = data or []
heapq.heapify(self.heap)
self.key = key # scoring function to be implemented for domain
def pushleft(self, item):
if self.key:
item = (self.key(item), item)
@smehan
smehan / TimeAsWords.scala
Created July 22, 2017 18:46
Time As Words parser and translation
object TimeAsWords extends App {
def parseTime(hhmm: String): (Int, Int) = {
"""^([01]\d|2[0-3]):([0-5]\d)$""".r.findAllIn(hhmm).matchData foreach {
md => return (md.group(1).toInt, md.group(2).toInt)
}
throw new IllegalArgumentException("Input string doesn't match required format: 00:00 - 23:59")
}
def formatTime(hhmm: (Int, Int)): String = {
val englishNum = Map(
@smehan
smehan / BST_study_ 1.py
Created July 15, 2017 02:33
Python implementation of BST
from collections import deque
"""
Node builds a BST and the accompanying methods allow for some recursive functions on the BST
"""
class Node:
def __init__(self, data):
self.right=self.left=None
self.data = data
@smehan
smehan / simpleWeb.clj
Created June 27, 2017 20:46
Simple Clojure web frameworks
[[compojure "1.5.2"]
[ring/ring-core "1.5.0"]
[ring/ring-defaults "0.2.3"]
[ring/ring-jetty-adapter "1.5.0"]]
(ns sample
(:require [compojure.core :as compojure]
[ring.adapter.jetty :as jetty]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]))
@smehan
smehan / TRFactorial.scala
Last active June 21, 2017 21:56
Scala for number theory
object Factorial extends App {
println(factorial(5))
println(factorial2(5))
// 1 - basic recursive factorial method
def factorial(n: Int): Int = {
if (n == 0) 1
else n * factorial(n-1)
}
@smehan
smehan / timing.scala
Created June 21, 2017 21:48
Scala profiling
/* A wrapper function that will time a function and output elapsed time */
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0) + "ns")
result
}
@smehan
smehan / Chapter_1.md
Last active October 19, 2023 13:35
Introduction to Probability: Joseph K. Blitzstein; Jessica Hwang

1. How many ways are there to permute the letters in the word MISSISSIPPI?

To solve, use the multiplication principle. Have a set of lists, A = {a1, a2, a3, ..., an}. k-permutations will result in picking letters from source string and moving to next element, chosing from remainder. If all of the slots were unique:

s <- 'MISSISSIPPI'

n <- len(nchar(s))