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
function memoize(fn) { | |
return function () { | |
var args = Array.prototype.slice.call(arguments) | |
fn.cache = fn.cache || {}; | |
return fn.cache[args] ? fn.cache[args] : (fn.cache[args] = fn.apply(this,args)) | |
} | |
} |
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
# In-place binary search example | |
def binary_search(sorted_list, left_pointer, right_pointer, target): | |
# this condition indicates we've reached an empty "sub-list" | |
if left_pointer >= right_pointer: | |
return "value not found" | |
# We calculate the middle index from the pointers now | |
mid_idx = (left_pointer + right_pointer) // 2 | |
mid_val = sorted_list[mid_idx] |
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
# Example of Graph implementation | |
class Graph: | |
def __init__(self, directed = False): | |
self.graph_dict = {} | |
self.directed = directed | |
def add_vertex(self, vertex): | |
self.graph_dict[vertex.value] = vertex | |
def add_edge(self, from_vertex, to_vertex, weight = 0): |
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
# Example of implementing a linked list | |
class Node: | |
def __init__(self, value, next_node=None): | |
self.value = value | |
self.next_node = next_node | |
def get_value(self): | |
return self.value | |
def get_next_node(self): |
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
# Example of tree implementation | |
class TreeNode: | |
def __init__(self, value): | |
self.value = value # data | |
self.children = [] # references to other nodes | |
def add_child(self, child_node): | |
# creates parent-child relationship | |
print("Adding " + child_node.value) | |
self.children.append(child_node) |
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
# Example of a Min-Heap | |
class MinHeap: | |
def __init__(self): | |
self.heap_list = [None] | |
self.count = 0 | |
def parent_idx(self, idx): | |
return idx // 2 | |
def left_child_idx(self, idx): |
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
# CLOSURES IN RUBY Paul Cantrell http://innig.net | |
# Email: username "cantrell", domain name "pobox.com" | |
# | |
# 翻译: kenshin54 http://kenbeit.com | |
# I recommend executing this file, then reading it alongside its output. | |
# 我强烈建议执行此脚本,然后根据它的输出来理解它。 | |
# | |
# Alteratively, you can give yourself a sort of Ruby test by deleting all the comments, | |
# then trying to guess the output of the code! |
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
$stack, $draws = [], {} | |
def method_missing *args | |
return if args[0][/^to_/] | |
$stack << args.map { |a| a or $stack.pop } | |
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :< | |
end | |
class Array | |
def +@ |
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
require 'rubygems' | |
require 'stanford-core-nlp' | |
require 'uuidtools' | |
StanfordCoreNLP.jvm_args = ['-Xmx3g'] | |
StanfordCoreNLP.use(:english) | |
class TextTokenizer | |
@@pipeline = StanfordCoreNLP.load(:tokenize, :ssplit, :pos, :lemma, :parse) |
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 ruby | |
require 'csv' | |
require 'mysql2' | |
require 'stanford-core-nlp' | |
require 'treat' | |
require 'unidecoder' | |
require 'yaml' | |
include Treat::Core::DSL |
OlderNewer