This file contains hidden or 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 numpy as np | |
def otsu(x): | |
hist, bin_edges = np.histogram(x, bins=range(min(x), max(x) + 2)) | |
total = len(x) | |
max_, threshold = 0, 0 | |
sum_total, sum_1, weight_0, weight_1 = 0, 0, 0, 0 | |
for i in range(len(hist)): |
This file contains hidden or 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 numpy as np | |
from foc import fx | |
@fx | |
def dbscan(x, eps=1, min_samples=2): | |
d = np.array(x).reshape(-1, 1) | |
npoints = len(d) | |
labels = np.full(npoints, -1) # init all labels to -1 | |
visited = np.zeros(npoints, dtype=bool) |
This file contains hidden or 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
# BLEU implementation don't have to be verbose. | |
# Simple, but robust and not error-prone (tested) | |
# Introduced 'epsilon' to avoid extreme values due to zero precisions. Adjust it. | |
# | |
# >>> references = ["BLEU implementation don't have to be verbose".split()] | |
# >>> candidate = "Robust for all almost edge cases and not error-prone".split() | |
# >>> bleu(references, candidate) | |
import numpy as np |
This file contains hidden or 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
-- | Predicates strings of startWith | |
-- Just for clarify, the same of `isPrefixOf` | |
startWith :: (Eq a) => [a] -> [a] -> Bool | |
startWith [] _ = True | |
startWith _ [] = False | |
startWith (x : xs) (y : ys) = x == y && startWith xs ys | |
main :: String -> [(Int, String)] | |
main input = |
This file contains hidden or 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
;; prerequisites: im-select (https://github.com/daipeihust/im-select) | |
;; | |
;; install it! | |
;; $ curl -Ls https://raw.githubusercontent.com/daipeihust/im-select/master/install_mac.sh | sh | |
;; | |
;; make sure that Emacs can execute 'im-select'. (Is it on the right $PATH?) | |
(setq english-im "com.apple.keylayout.ABC") | |
(setq previous-im "com.apple.keylayout.ABC") |
This file contains hidden or 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
-- | Solve a given system of congruences using Chinese Remainder Theorem (CRT) | |
-- [(Integer, Integer)] = [(eq.1 residue, eq.1 modulo), (eq.2 residue, eq.2 modulo),..] | |
chineseRemainder :: [(Integer, Integer)] -> Maybe Integer | |
chineseRemainder congruences = | |
(`mod` _N) | |
. sum | |
. hadamard _Ni | |
. hadamard residues | |
<$> zipWithM getMi _Ni moduli | |
where |
This file contains hidden or 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
{- Example: How to use------------------------------------------------------------ | |
$ stack ghci | |
-- Import or load the below module: Shamir | |
> :l Shamir | |
-- Prepare any string "secret" less than 32-byte | |
> secret = "stop COVID-19" | |
-- Prepare parameter (n, k) |
This file contains hidden or 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
use std::hash::{BuildHasher, Hasher}; | |
use std::collections::HashMap; | |
fn bytes_to_int(bytes: &[u8]) -> usize { | |
let l = bytes.len(); | |
(0..l).fold(0, |sum, i| { | |
sum + (1 << ((l - i - 1) * 8)) * bytes[i] as usize | |
}) | |
} |
This file contains hidden or 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
extern crate rand; | |
use rand::Rng; | |
// Fisher-Yates shuffle | |
fn shuffle<T: Clone>(v: &mut Vec<T>) { | |
let mut rng = rand::thread_rng(); | |
let s = v.len(); | |
(0..s).for_each(|i|{ | |
let q = rng.gen_range(0, s); | |
v.swap(i, q); |
This file contains hidden or 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
use std::ops::{Add, Div, Mul, Range, Rem, Shl, Shr, Sub}; | |
pub trait Uint: | |
Add<Output = Self> | |
+ Sub<Output = Self> | |
+ Mul<Output = Self> | |
+ Div<Output = Self> | |
+ Rem<Output = Self> | |
+ Shl<Output = Self> | |
+ Shr<Output = Self> |