Skip to content

Instantly share code, notes, and snippets.

@pixyj
pixyj / beta_distribution.R
Created June 21, 2014 01:51
Beta Distribution in R
#https://www.youtube.com/watch?v=UZjlBQbV1KU
x <- seq(0, 1, length=100)
a= 0.5
b= 0.5
y <- x^(a-1) * (1-x)^(b-1)
plot(x, y, type="l", lwd=3)
@pixyj
pixyj / fours_and_fives.py
Last active August 29, 2015 13:57
Any integer greater than or equal to 12 can be written as a sum of 4's and 5's
import collections
import functools
#https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
class memoized(object):
'''Decorator. Caches a function's return value each time it is called.
If called later with the same arguments, the cached value is returned
(not reevaluated).
'''
@pixyj
pixyj / write.py
Created March 15, 2014 07:17
Write a Python sequence into a file
def write(file_path, sequence, line_format):
"""
Example Usage: Save squares of numbers from 1 to 100 in a csv file
squares = ( (i, i * i) for i in range(1, 101))
write("squares.txt", squares, "{},{}")
"""
lines = (line_format.format(*line_variables) for line_variables in sequence)
@pixyj
pixyj / play_publish.go
Last active August 29, 2015 13:57
Simple Go program to check if your app is published yet on the Play Store
package main
import (
"fmt"
"net/http"
"time"
"os/exec"
)
func IsAppPublished(url string) bool {
@pixyj
pixyj / mle.csv
Created February 16, 2014 09:10
Maximum Likelihood estimator for k heads in n trials. https://www.udacity.com/course/viewer#!/c-st101/l-48727700/e-48726376/m-48724282
0 0
0.01 0.000297
0.02 0.001176
0.03 0.002619
0.04 0.004608
0.05 0.007125
0.06 0.010152
0.07 0.013671
0.08 0.017664
0.09 0.022113
@pixyj
pixyj / flip_predictor.py
Created February 16, 2014 06:26
Flip Predictor. Drill to learn Bayes Rule in Udacity's Intro to Stats class
#FlipPredictor
#A coin is drawn at random from a bag of coins of varying probabilities
#Each coin has the same chance of being drawn
#Your class FlipPredictor will be initialized with a list of the probability of
#heads for each coin. This list of probabilities can be accessed as self.coins
#in the functions you must write. The function update will be called after every
#flip to enable you to update your estimate of the probability of each coin being
#the selected coin. The function pheads may be called and any time and will
#return your best estimate of the next flip landing on heads.
@pixyj
pixyj / watchdog_starter
Created February 6, 2014 09:22
Seed file for listening to filesystem changes using python watchdog
import os
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class VHandler(FileSystemEventHandler):
def __init__(self):
FileSystemEventHandler.__init__(self)
@pixyj
pixyj / knapsack.py
Created December 4, 2013 16:22
Dynamic programming solution to knapsack problem in python.
#For simulations
import random
class Item(object):
def __init__(self, value=0, weight=0):
self.value = value
self.weight = weight
def __repr__(self):
return "({value}, {weight})".format(**self.__dict__)
@pixyj
pixyj / max_weight_independent_set
Last active December 30, 2015 05:39
Max weight independent set of a path graph
def max_weights(a):
weights = [0] * (len(a) + 2)
for i, v in enumerate(a):
j = i + 2
with_v = weights[j-2] + v
without_v = weights[j-1]
weights[j] = max(with_v, without_v)
@pixyj
pixyj / data.txt
Created November 30, 2013 18:38
Solution to inversion count in an array. HackerRank problem: http://www.hackerrank.com/challenges/insertion-sort $ python inv_count.py < data.txt
10
20
3 15 12 10 2 13 14 5 6 9 17 18 16 8 0 11 1 19 4 7
20
16 0 1 15 8 10 18 17 11 13 5 6 19 4 14 12 3 9 2 7
20
0 19 11 13 15 18 5 2 9 4 12 10 7 14 17 8 3 16 6 1
20
2 14 7 4 1 17 6 16 18 9 12 13 5 3 10 11 19 0 8 15
20