Skip to content

Instantly share code, notes, and snippets.

@pixyj
pixyj / list_memcached_keys.sh
Last active March 2, 2016 06:53 — forked from bkimble/gist:1365005
List local memcached keys using Ruby
#!/usr/bin/env ruby
require 'net/telnet'
require 'terminal-table'
# This code is copied from a comment on the original repo.
tbl = Terminal::Table.new headings: %w(id expires cache_key bytes)
localhost = Net::Telnet::new("Host" => "localhost", "Port" => 11211, "Timeout" => 3)
matches = localhost.cmd("String" => "stats items", "Match" => /^END/).scan(/STAT items:(\d+):number (\d+)/)
@pixyj
pixyj / exec_aloud.py
Last active February 29, 2016 13:29
Python decorator to say function's name before it is executed (for OS X)
"""
Usage Example:
Case 1 - Debug: Say function name and start pdb before executing add
@exec_aloud(debug=True)
def add(x, y):
return x + y
@pixyj
pixyj / numbers_and_squares.csv
Created January 31, 2016 02:30
Example output for a quiz.
1 1
2 4
3 9
4 16
5 25
@pixyj
pixyj / first_class_functions.ipynb
Last active April 5, 2016 10:50
Functions as first class citizens in Python
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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 / 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
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 / 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
@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 / 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