Skip to content

Instantly share code, notes, and snippets.

@thyeem
thyeem / bleu.py
Last active October 7, 2023 13:13
A simple BLEU implementation
# 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
@thyeem
thyeem / FindPattern.hs
Last active March 6, 2022 10:16
sng2c's problem
-- | 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 =
@thyeem
thyeem / sane-im-switch.el
Created January 28, 2022 03:21
Less annoying way to switch input method when using evil-mode
;; 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")
@thyeem
thyeem / chineseRemainder.hs
Created November 21, 2021 11:53
Solve a given system of congruences using Chinese Remainder Theorem (CRT)
-- | 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
@thyeem
thyeem / Shamir.hs
Last active June 12, 2020 21:17
My own impl of secret-sharing (Shamir scheme)
{- 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)
@thyeem
thyeem / build_hasher.rs
Created April 4, 2020 03:15
with a manual BuildHasher
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
})
}
@thyeem
thyeem / shuffle.rs
Last active April 2, 2020 17:00
impl shuffle fn based on Fisher-Yates
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);
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>