Skip to content

Instantly share code, notes, and snippets.

View onelharrison's full-sized avatar

Onel Harrison onelharrison

View GitHub Profile
@onelharrison
onelharrison / moment_undefined_behavior.js
Last active January 27, 2018 16:19
Code snippet showing the behavior of passing undefined to the moment function.
// ................................
// behavior in moment.js v2.x.x
// ................................
const momentWithUndefinedArg = moment(undefined);
const momentWithNoArgs = moment();
// expected result
momentWithNoArgs.isValid(); // true
momentWithNoArgs.format("YYYY-MM-DD"); // today's date
@onelharrison
onelharrison / wat.js
Created January 27, 2018 18:41
Code snippet showing a few could-be-surprising expression evaluations in JavaScript.
[] + []
// ""
[] + {}
// "[object Object]"
{} + []
// 0
from collections import Counter
import math
def knn(data, query, k, distance_fn, choice_fn):
neighbor_distances_and_indices = []
# 3. For each example in the data
for index, example in enumerate(data):
# 3.1 Calculate the distance between the query example and the current
# example from the data.
Movie ID Movie Name IMDB Rating Biography Drama Thriller Comedy Crime Mystery History Label
58 The Imitation Game 8 1 1 1 0 0 0 0 0
8 Ex Machina 7.7 0 1 0 0 0 1 0 0
46 A Beautiful Mind 8.2 1 1 0 0 0 0 0 0
62 Good Will Hunting 8.3 0 1 0 0 0 0 0 0
97 Forrest Gump 8.8 0 1 0 0 0 0 0 0
98 21 6.8 0 1 0 0 1 0 1 0
31 Gifted 7.6 0 1 0 0 0 0 0 0
3 Travelling Salesman 5.9 0 1 0 0 0 1 0 0
51 Avatar 7.9 0 0 0 0 0 0 0 0
from knn_from_scratch import knn, euclidean_distance
def recommend_movies(movie_query, k_recommendations):
raw_movies_data = []
with open('movies_recommendation_data.csv', 'r') as md:
# Discard the first line (headings)
next(md)
# Read the data into memory
for line in md.readlines():
@onelharrison
onelharrison / aws_redshift_queries.sql
Created February 24, 2019 23:44
Useful queries for working with AWS Redshift
-- find recent queries that hit disk
SELECT query, substring
FROM svl_qlog join svl_query_summary using(query)
WHERE starttime > date(getdate()) - interval '1 day'
AND is_diskbased = 't';
-- see query execution details
SELECT query, step, rows, workmem, label, is_diskbased
FROM svl_query_summary
WHERE query = 321925
import heapq as heap
class HQNode:
def __init__(self, list_index, val_index, val):
self.list_index = list_index
self.val_index = val_index
self.val = val
def __eq__(self, other):
return self.val == other.val
@onelharrison
onelharrison / trie.py
Last active February 20, 2020 15:50
Implementation of the Trie data structure
class TrieNode:
def __init__(self, char=None, children={}, terminal=False):
self.char = char
self.children = children
self.terminal = terminal
class Trie:
def __init__(self):
self.root = TrieNode()
class Expr:
def eval(self, env):
raise NotImplementedError
class BinaryOperator(Expr):
def __init__(self, left, right):
super().__init__()
self.left = left
self.right = right
@onelharrison
onelharrison / numbertoggle.vim
Created April 13, 2020 11:27
Vimscript to enable hybrid line numbers and autotoggle relative line numbers
" https://jeffkreeftmeijer.com/vim-number/
" Show absolte line number for current line and
" relative line number for surrounding lines
set number relativenumber
" Automatically toggle relative line numbers
augroup
autocmd!
autocmd BufEnter,WinEnter,InsertLeave,FocusGained * set relativenumber