Skip to content

Instantly share code, notes, and snippets.

View m80126colin's full-sized avatar

Hsu Subang m80126colin

View GitHub Profile
from itertools import product
from functools import reduce
class Number():
def __init__(self):
self.num = 0
def succ(self):
self.num += 1
return self
@m80126colin
m80126colin / auth.js
Last active October 30, 2018 09:21
An example for token-based authentication through passport.js
const Promise = require('bluebird')
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
const passport = require('passport')
const passportJWT = require('passport-jwt')
const LocalStrategy = require('passport-local').Strategy
const JWTStrategy = passportJWT.Strategy
const extractJWT = passportJWT.ExtractJwt
@m80126colin
m80126colin / yonh1.js
Created August 8, 2018 16:02
Extract data from Guangyun
const _ = require('lodash')
const fs = require('fs')
fs.readFile('./sbgy.xml', 'utf-8', (err, data) => {
const info = data.match(/(onyomi=\u0022([^\u0022]+)\u0022|\u003Cword_head id=\u0022[^\u0022]+\u0022\u003E[^\u003C]+\u003Cnote\u003E)/ug);
const slice_point = _.concat([0], _.chain(info).map( (r, i) => (/onyomi/u.test(r)) ? i : undefined ).compact().value(), info.length);
const chunks = _.map(_.zip(_.initial(slice_point), _.tail(slice_point)), ([L, R]) => _.slice(info, L, R));
const result = _.chain(chunks).map(c => {
const onyomi = _.head(c).match(/onyomi=\u0022([^\u0022]+)\u0022/u)[1]
const rhymes = _.chain(c)
@m80126colin
m80126colin / nub.hs
Created April 26, 2018 18:21
An amortised linear-time solution by hashing
import Data.Hashable
import Data.HashSet
-- Amortised linear-time by hashing
uniqhs :: (Hashable a, Eq a) => HashSet a -> [a] -> [a]
uniqhs _ [] = []
uniqhs h (x:xs) = if member x h then uniqhs h xs else x:uniqhs (insert x h) xs
nub :: [Int] -> [Int]
nub = uniqhs empty
import Control.Monad
import Data.Char
lst2Tup :: [a] -> (a, a)
lst2Tup [x, y] = (x, y)
judge :: Char -> Bool
judge x = if x == '+' then True else False
-- O(n) time, O(n) space
type GenFunc = [Double]
solve :: (Int, [String]) -> Double
solve (h, ss) = foldr1 max $ map (possible . pattern) ss
where possible [x, y, z] = sum $ drop (h - z) (gfTable !! x !! y)
table :: [Int]
table = [0 .. 20]
gfTable :: [[GenFunc]]
gfTable = map (\f -> f <$> gfDice <$> table) (gfExp <$> table)
gfProd :: [GenFunc] -> GenFunc
gfProd = foldr1 gfMul
gfExp :: Int -> GenFunc -> GenFunc
gfExp n = gfProd . replicate n
gfMul :: GenFunc -> GenFunc -> GenFunc
gfMul xs ys = foldr1 (zipWith (+)) . map f . zip [0..] $ ys
where
len = sum (minus 1 . length <$> [xs, ys]) + 1
f (x, d) = gfPad x len (map (* d) xs)