Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / decanting.py
Last active August 29, 2015 14:18
Solution to Decanting problem with capacities 8, 5, 3
"""
Problem Description:
You are given three vessels A, B and C of capacities 8, 5 and 3 gallons respectively. A is filled, while B and C are empty.
Divide the liquid in A into two equal quantities.
(From Graph Theory, Narsingh Deo)
http://bit.ly/1Dnp4HA
Usage:
import decanting as dc
import networkx as nx
@pixyj
pixyj / monte.py
Created May 7, 2015 14:08
A solution to a probability problem using a Monte Carlo simulation
from __future__ import division
import random
import itertools
"""
People are waiting in line to board a 100-seat airplane. Steve is the first person in the line. He gets on the plane but suddenly can't remember what his seat number is, so he picks a seat at random. After that, each person who gets on the plane sits in their assigned seat if it's available, otherwise they will choose an open seat at random to sit in.
The flight is full and you are last in line. What is the probability that you get to sit in your assigned seat?
Usage:
python monte.py
@pixyj
pixyj / treap.py
Created June 23, 2015 02:10
Treap Insertion in Python. (Deletion is not implemented)
from random import randint
import ipdb
RAND_MAX = 1000
class Node(object):
def __init__(self, key, data, priority=None):
self.key = key
self.data = data
from collections import namedtuple
from itertools import combinations
Rod = namedtuple('Rod', ['length', 'price', ])
def optimal_price(rods,total_length=20):
zero_rod = Rod(length=0,price=0)
if total_length > len(rods):
@pixyj
pixyj / Preferences.sublime-settings
Created August 30, 2015 17:36
My Sublime settings. Haven't customized much yet.
// While you can edit this file, it's best to put your changes in
// "User/Preferences.sublime-settings", which overrides the settings in here.
//
// Settings may also be placed in file type specific options files, for
// example, in Packages/Python/Python.sublime-settings for python files.
{
// Sets the colors used within the text area
"color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme",
// Note that the font_face and font_size are overriden in the platform
@pixyj
pixyj / thread_and_queue_example.py
Created December 30, 2015 18:57
Thread and Queue Python example - blast from my past
import Queue
import threading
class PrintMsg(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
print "waiting for msg"
msg = self.queue.get()
@pixyj
pixyj / first_class_functions.ipynb
Last active April 5, 2016 10:50
Functions as first class citizens in Python
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.