This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; 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. | |
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |