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 / clisp.lisp
Created February 4, 2014 14:59
Learn X in Y minutes tutorial for programming languages. Codes are pasted from http://learnxinyminutes.com
;; LISP TUTORIAL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 0. Syntax
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; General form.
;; Lisp has two fundamental pieces of syntax: the ATOM and the
;; S-expression. Typically, grouped S-expressions are called `forms`.
@junjiah
junjiah / bellman_ford.py
Created August 30, 2015 14:03
solved 'Breadth First Search: Shortest Reach' on hackerrank https://www.hackerrank.com/challenges/bfsshortreach
from collections import namedtuple
# A reasonable large number to represent infinity.
INF = (1 << 31)
UNIT_LENGTH = 6
# Struct for edges.
Edge = namedtuple('Edge', ['src', 'dest'])
def calculate_shortest_distances(node_num, edges, src):
@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 / Week 1 - Ruby.rb
Last active January 4, 2016 05:49
Homework for "Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages"
######### day 1 homework #########
# Bonus problem: If you're feeling the need for a little more, write
# a program that picks a random number. Let a player guess the
# number, telling the player whether the guess is too low or too high.
while (true)
random_num = rand(10)
input = ""
puts "guess a number between 0 and 9"
input = gets
@junjiah
junjiah / surron_region.cpp
Created January 20, 2014 03:40
solved "Surrounded Regions" on LeetCode (performance-critical, thus using iterative DFS) http://oj.leetcode.com/problems/surrounded-regions/
class Solution {
public:
void solve(vector< vector<char> > &board) {
height = board.size();
if (height == 0) return;
width = board[0].size();
this->board = &board;
if (height <= 2 || width <= 2) return;
mark = new vector< vector<bool> >(height, vector<bool>(width, false));
// mark 'O' alive
// class UndirectedGraphNode {
// int label;
// ArrayList<UndirectedGraphNode> neighbors;
// UndirectedGraphNode(int x) {
// label = x;
// neighbors = new ArrayList<UndirectedGraphNode>();
// }
// }
public class Solution {