This file contains 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
def perms(xs): | |
if len(xs) <= 1: | |
yield xs | |
else: | |
for i in range(len(xs)): | |
for perm in perms(xs[:i] + xs[i+1:]): | |
yield xs[i:i+1] + perm | |
print list(perms(range(3))) | |
print list(perms('ABC')) |
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def merge(xs, ys): | |
i, j = 0, 0 | |
new_list = [] | |
while i < len(xs) and j < len(ys): | |
if xs[i] < ys[j]: | |
new_list.append(xs[i]) | |
i += 1 |
This file contains 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
(defn merge- [xs-init ys-init] | |
(loop [xs xs-init ys ys-init res []] | |
(let [[x & xrest] xs [y & yrest] ys] | |
(cond | |
(empty? xs) (concat res ys) | |
(empty? ys) (concat res xs) | |
(< x y) (recur xrest ys (conj res x)) | |
(<= y x) (recur xs yrest (conj res y)))))) | |
(defn sort- [xs] |
This file contains 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
(defn perms [xs] | |
(if (<= (count xs) 1) | |
(list xs) | |
(apply | |
concat | |
(for [i (-> xs count range)] | |
(lazy-seq | |
(for [p (perms (concat (take i xs) (drop (inc i) xs)))] | |
(cons (nth xs i) p))))))) |
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def partition(xs, l, r): | |
pv = xs[r] | |
i = l-1 | |
for j in range(l, r): | |
if xs[j] <= pv: | |
i += 1 | |
xs[i], xs[j] = xs[j], xs[i] |
This file contains 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
var str = "I like turtles. Do you? Awesome! hahaha. lol!!! What's going on???? I went to U.S."; | |
var result = str.replace(/([^.!?A-Z]+[.?!]+\s*)/g, "$1|").split("|"); | |
console.log(result); |
This file contains 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
from sklearn.feature_extraction.text import TfidfVectorizer | |
def iter_docs(): | |
docs = [ | |
'This is a pen.', | |
'Hi, How are you?', | |
'May the force be with you.' | |
] | |
for doc in docs: | |
yield doc |
This file contains 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
#cJuman.init(['-B', '-e2']) | |
def _juman_parse_line(line): | |
xs = line.split(' ') if not line.startswith('@') else line[2:].split(' ') | |
xs = xs[:11] + [' '.join(xs[11:])] | |
if len(xs) != 12: | |
return None | |
# 表層 読み 原型 品詞 品詞ID 品詞2 品詞ID2 | |
labels = 'surface read orig pos pos_id pos2 pos_id2 pos3 pos_id3 pos4 pos_id4 detail'.split() | |
word = dict((l, x) for l, x in zip(labels, xs)) |
This file contains 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 networkx as nx | |
from networkx.exception import NetworkXError | |
from networkx.utils import not_implemented_for | |
@not_implemented_for('multigraph') | |
def divrank(G, alpha=0.25, d=0.85, personalization=None, | |
max_iter=100, tol=1.0e-6, nstart=None, weight='weight', | |
dangling=None): | |
''' |
This file contains 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 re | |
import pprint | |
def upp(obj, indent=2, **pp_args): | |
''' | |
stringify object that contains unicode | |
''' | |
printer = pprint.PrettyPrinter(indent=indent, **pp_args) | |
str_ = printer.pformat(obj) | |
str_ = re.sub( |
OlderNewer