Skip to content

Instantly share code, notes, and snippets.

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'))
#!/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
(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]
(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)))))))
#!/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]
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);
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
#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))
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(
@iinm
iinm / divrank.py
Last active November 9, 2015 06:33
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):
'''