Skip to content

Instantly share code, notes, and snippets.

View factorial_tail.py
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
View memoize.hs
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 Sep 26, 2016
Probability distribution and murmurhash.
View prob.ipynb
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 Jul 28, 2016
Remove zombie / bots for a Weibo account.
View batch_clean.js
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();
}
View testMaliciousExpression.swift
func testMaliciousExpressionNFA() {
// NFA matching.
let re = REAutomata(expr: "(0|00)*1")
self.measureBlock {
XCTAssertFalse(re.test("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
}
}
func testMaliciousExpressionDFA() {
// DFA matching.
View regex.cc
#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));
View kth_bst_bad.py
# 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}
View longest_paren.cc
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] == '(') {
View merge_k_ls.cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// Divide and conquer.
View search_range.py
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)
You can’t perform that action at this time.