Skip to content

Instantly share code, notes, and snippets.

View stevenbeales's full-sized avatar

Steven Beales stevenbeales

View GitHub Profile
@stevenbeales
stevenbeales / .js
Last active December 27, 2018 03:02
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))
}
}
@stevenbeales
stevenbeales / .py
Last active December 27, 2018 03:10
With a sorted data-set, we can take advantage of the ordering to make a sort which is more efficient than going element by element. Binary search requires a sorted data-set. We then take the following steps: 1. Check the middle value of the dataset
# 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]
@stevenbeales
stevenbeales / .py
Last active December 27, 2018 03:10
Graphs are the perfect data structure for modeling networks, which make them an indispensable piece of your data structure toolkit. They're composed of nodes, or vertices, which hold data, and edges, which are a connection between two vertices. A sin
# 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):
@stevenbeales
stevenbeales / .py
Last active December 27, 2018 03:11
Linked lists are one of the basic data structures used in computer science. They have many direct applications and serve as the foundation for more complex data structures. The list is comprised of a series of nodes as shown in the diagram. The head
# 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):
@stevenbeales
stevenbeales / .py
Last active December 27, 2018 03:11
Trees are an essential data structure for storing hierarchical data with a directed flow. Similar to linked lists and graphs, trees are composed of nodes which hold data. Nodes also store references to zero or more other tree nodes. Data moves down
# 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)
@stevenbeales
stevenbeales / .py
Last active December 27, 2018 03:12
Heaps are used to maintain a maximum or minimum value in a dataset. Heaps are commonly used to create a priority queue. Heaps tracking the maximum or minimum value are max-heaps or min-heaps. Think of the min-heap as a binary tree with two qualities
# 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):
@stevenbeales
stevenbeales / .rb
Created December 27, 2018 04:31
Closure examples
# 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!
@stevenbeales
stevenbeales / .rb
Created December 27, 2018 04:35
Ruby list comprehensions
$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 +@
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)
#!/usr/bin/env ruby
require 'csv'
require 'mysql2'
require 'stanford-core-nlp'
require 'treat'
require 'unidecoder'
require 'yaml'
include Treat::Core::DSL