Skip to content

Instantly share code, notes, and snippets.

@pyrocat101
pyrocat101 / bisect-left.clj
Created November 1, 2013 07:46
bisect-left in Clojure
(defn binary-search-gte
[items value]
(letfn
[(do-search
[low high]
(if (< low high)
(let [mid (quot (+ low high) 2)]
(if (> value (items mid))
(recur (inc mid) high)
(recur low mid)))
@pyrocat101
pyrocat101 / perm.py
Created November 18, 2013 01:38
Recursive Permutation in Python
def permutation(xs):
if len(xs) == 1:
return [xs]
ret = []
for x in xs:
for y in permutation(filter(lambda a: a != x, xs)):
ret.append([x] + y)
return ret
def perm(xs):
@pyrocat101
pyrocat101 / rot-bsearch.py
Last active December 28, 2015 15:19
Binary Search on Rotated Array
# non-recursive version
def rot_bsearch(xs, val, lo=0, hi=None):
"""
Binary Search on rotated sorted array.
>>> rot_bsearch([8,1,2,3,4,5], 5)
5
>>> rot_bsearch([], 42)
-1
>>> rot_bsearch([1], -1)
-1
@pyrocat101
pyrocat101 / my_pow.py
Last active December 28, 2015 20:29
Float number powered by an integer
import math
def my_pow(a, b):
"""
Returns the value of the `a` raised to the power of `b`.
Assume that `a` is a float number and `b` is an integer.
Special cases:
* if `b` is zero, then result is 1.0
* if `a` is NaN and `b` is non-zero, then the result is NaN.
* if `a` is 0 and `b` < 0, or `a` is +Inf and `b` > 0, then the
@pyrocat101
pyrocat101 / ip-address-regex.js
Created November 20, 2013 18:44
IPv4 and IPv6 Address RegExp
var IPV4 = /^(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}$/,
IPV6 = /^([0-9a-f]){1,4}(:([0-9a-f]){1,4}){7}$/i;
Given an array ‘D’ with n elements: d[0], d[1], …, d[n-1], you can perform the following two steps on the array.
1. Randomly choose two indexes (l, r) with l < r, swap (d[l], d[r])
2. Randomly choose two indexes (l, r) with l < r, reverse (d[l…r]) (both inclusive)
After you perform the first operation a times and the second operation b times, you randomly choose two indices l & r with l < r and calculate the S = sum(d[l…r]) (both inclusive).
Now, you are to find the expected value of S.
Input Format
@pyrocat101
pyrocat101 / glob.py
Last active December 29, 2015 23:09
Glob style wildcard
# -*- coding: utf-8 -*-
def glob(s, p, i=0, j=0, aux=0, star=-1):
"""
>>> glob("aa", "*")
True
>>> glob("aa", "a")
False
>>> glob("aa", "aa")
True
@pyrocat101
pyrocat101 / hist_largest_rect.py
Created December 5, 2013 07:02
Largest rectangle area in histogram.
def hist_largest_rect(heights):
""" Largest Rectangle in Histogram.
>>> hist_largest_rect([2,1,5,6,2,3])
10
>>> hist_largest_rect([3,3,3])
9
>>> hist_largest_rect([3,4,4])
9
>>> hist_largest_rect([1,2,3])
4
;;
;; NS CHEATSHEET
;;
;; * :require makes functions available with a namespace prefix.
;;
;; * :use makes functions available without a namespace prefix
;; (i.e., refers functions to the current namespace).
;;
;; * :import refers Java classes to the current namespace.
;;
@pyrocat101
pyrocat101 / wildcard_lookup.py
Created February 11, 2014 03:26
Wildcard Dictionary Lookup
class TRIE(object):
"""
>>> "foo" in TRIE(["foo", "bar"])
True
>>> "fo." in TRIE(["foo", "bar"])
True
>>> "f.o" in TRIE(["foo", "bar"])
True
>>> ".oo" in TRIE(["foo", "bar"])
True