Skip to content

Instantly share code, notes, and snippets.

import unittest
class _Node(object):
'''
Represents a node in a BST.
value is any object that implements __lt__(), enough for
sorting in python 3.
'''
var server = nodeOauth2Server({
model: modelHandlers,
grants: ['password', 'refresh_token'],
debug: false,
refreshTokenLifetime: null, // never expire refresh token
passthroughErrors: true
});
module.exports = {
grantWrap: function oauthGrantWrap(req, res, next) {
import unittest
def bsearch_left(items, v):
'''
Binary search, finds left-most position.
Returns (bool, insertion-point after which
element should be inserted to maintain an invariant).
'''
if v is None:
raise ValueError('v must be set')
import array
import tracemalloc
from contextlib import contextmanager
@contextmanager
def tracemem(msg):
tracemalloc.start()
snapshot1 = tracemalloc.take_snapshot()
yield
snapshot2 = tracemalloc.take_snapshot()
@lvsl-deactivated
lvsl-deactivated / dozer-in-thread-demo.py
Created November 25, 2012 09:10
Using Dozer memory profiler in any python script
from wsgiref.simple_server import make_server
from wsgiref.util import setup_testing_defaults
import random
import threading
import dozer
def run_dozer():
# taken from wsgiref documentation
def simple_app(environ, start_response):
# coding: utf-8
'''
Given a list of integers, your task is to write a program to output an integer-valued list of equal length such that the output element at index 'i' is the product of all input elements except for the input element at 'i'.
In other words, let inputArray by an integer array of length 'n'. The solution,computed into outputArray, would be:
for each j from 1 to n-2:
outputArr[ j ] = inputArray[0] * inputArray[1] * inputArray[2] * ... * inputArray[j-1] * inputArray[j+1] * inputArray[j+2] *...* inputArray[n-1]
for j = 0
@lvsl-deactivated
lvsl-deactivated / frequent_terms.py
Created August 12, 2012 10:44
evernote contest
# coding: utf-8
'''
Frequency Counting of Words / Top N words in a document.
Given N terms, your task is to find the k most frequent terms from given N terms.
Input format :
First line of input contains N, denoting the number of terms to add.
@lvsl-deactivated
lvsl-deactivated / circular_buffer.py
Created August 12, 2012 10:42
evernote contest
# coding: utf-8
'''
Implement a circular buffer of size N. Allow the caller to append, remove and list the contents of the buffer. Implement the buffer to achieve maximum performance for each of the operations.
The new items are appended to the end and the order is retained i.e elements are placed in increasing order of their insertion time. When the number of elements in the list elements exceeds the defined size, the older elements are overwritten.
There are four types of commands.
@lvsl-deactivated
lvsl-deactivated / top_four.py
Created August 12, 2012 10:41
evernote contest
# coding: utf-8
'''
Given an unordered list of N elements, write a function to find the top four elements of the given list. Make sure your function runs in linear time O(N).
Input format :
First line of input contains N , number of elements in list.
Next N line , each contains a element.
@lvsl-deactivated
lvsl-deactivated / quake_live.py
Created July 5, 2012 21:38
Google CodeJam EuroPython 2012, Problem A "Quake Live" solution
#!/usr/bin/env python
# coding: utf-8
import itertools
def find_min_skill_difference(skills):
n = len(skills)
if n == 2:
return abs(skills[0] - skills[1])
skills.sort()