Skip to content

Instantly share code, notes, and snippets.

from collections import Counter
from math import log
def prime_fac(x):
"""Returning a map factor -> exponent."""
c = Counter()
for i in xrange(2, x + 1):
while x % i == 0:
c[i] += 1
x //= i
module FunctionEvaluator where
import qualified Data.Map.Strict as M
evaluateFunction :: Ord a => (a -> Either b ([a], [b] -> b)) -> a -> b
evaluateFunction f x = snd $ memoize x M.empty
where
memoize n m = case M.lookup n m of
Just v -> (m, v)
Nothing ->
@junjiah
junjiah / prob.ipynb
Last active September 26, 2016 07:11
Probability distribution and murmurhash.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@junjiah
junjiah / batch_clean.js
Last active July 28, 2016 04:59
Remove zombie / bots for a Weibo account.
var i = 0;
var pageNumber = 5;
function clearBotFans() {
function removeFan(fanHrefElement) {
fanHrefElement.click();
const okButtons = document.querySelectorAll('a[action-type="ok"]');
if (okButtons) {
okButtons[0].click();
}
func testMaliciousExpressionNFA() {
// NFA matching.
let re = REAutomata(expr: "(0|00)*1")
self.measureBlock {
XCTAssertFalse(re.test("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
}
}
func testMaliciousExpressionDFA() {
// DFA matching.
@junjiah
junjiah / regex.cc
Created October 1, 2015 02:40
solved 'Regular Expression Matching' on LeetCode https://leetcode.com/problems/regular-expression-matching/
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
bool isMatch(string s, string p) {
int s_len = s.size(), p_len = p.size();
vector<vector<bool>> matched(p_len + 1, vector<bool>(s_len + 1, false));
@junjiah
junjiah / kth_bst_bad.py
Created September 21, 2015 18:33
solved 'Kth Smallest Element in a BST' on LeetCode https://leetcode.com/problems/kth-smallest-element-in-a-bst/
# Memoize the size and iteratively get down.
# Bad because getting size requires O(n) time.
class Solution(object):
def kthSmallest(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""
self.sz_dict = {None: 0}
@junjiah
junjiah / longest_paren.cc
Created September 15, 2015 20:50
solved 'Longest Valid Parentheses' on LeetCode https://leetcode.com/problems/longest-valid-parentheses/
class Solution {
public:
int longestValidParentheses(string s) {
int sz = s.size();
// Records for DP, `longest[i]` is the max number
// of pairs of brackets ending with `s[i-1]`.
vector<int> longest(sz + 1, 0);
int max_pairs = 0;
for (int i = 1; i < sz; ++i) {
if (s[i] == '(') {
@junjiah
junjiah / merge_k_ls.cpp
Created September 15, 2015 11:42
solved 'Merge k Sorted Lists' on LeetCode https://leetcode.com/problems/merge-k-sorted-lists/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// Divide and conquer.
@junjiah
junjiah / search_range.py
Created September 13, 2015 19:05
solved 'Search for a Range' on LeetCode https://leetcode.com/problems/search-for-a-range/
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
left = self.lowerBound(nums, target)
if left == len(nums) or nums[left] != target:
return (-1, -1)