Skip to content

Instantly share code, notes, and snippets.

class Node
attr_accessor :parent, :left, :right, :value
def is_leaf?
left.nil? && right.nil?
end
def has_left_child?
!left.nil?
end
const Node = {
value: null,
parent: null,
left: null,
right: null,
create: function(props={}) {
let node = Object.create(this);
Object.keys(this).forEach( (key)=> {
node[key] = props[key] || this[key]
})
@jaeming
jaeming / split.rb
Created October 9, 2016 19:51
split one liner
def split(str, key)
(list, word = [], '') and str.chars.to_a.map { |i| i == key ? (list << word and word = '') : word << i } and list << word
end
@jaeming
jaeming / Gemfile
Last active September 26, 2016 20:36
source "https://rubygems.org"
gem "minitest"
# I was working on a client project not long back and the client expressed an interest in having thread-able comments, where someone can reply to one comment and it starts off a new thread of comments, which could then be branched even further by other replies. It occurred to me that I already had a polymorphic association with my comments so that different resources could be commented on (articles, photos, statuses, etc...). It seemed plausible I could just go ahead and use this on a self-referential basis as well.
# With polymorphic associations, instead of having a foreign key like, article_id, you have a type column representing the table and an id column for whatever type table that is. For instance:
:commendable_type => 'Article', :commendable_id => 1
# Would be comparable to a foreign key reference of
:article_id => 1
#thus polymorphic models can be associated with any other model type, including themselves it turns out.
:commendable_type => 'Comment'
# Represents a comment that belongs to a comme
def valid?(pattern)
pattern.map(&:uniq!).map(&:nil?).include?(false) ? false : true
end
def validSolution(board)
rows, columns, grids = board, [], []
9.times do |i|
column, grid = [], []
board.map { |row| column << row[i] }
class Abbreviator
def self.abbreviate(s)
s.split(/\b/).map{|w|w.size>3?"#{w[0]}#{w.size-2}#{w[-1]}":w}.join
end
end
def list names
name_list = names.map(&:values).flatten
if name_list.length > 1
with_ampersand = " & #{name_list.pop}"
solution = name_list.join(', ') << with_ampersand
else
solution = name_list.join
end
end
@jaeming
jaeming / plugboard.rb
Created October 28, 2015 13:12
Enigma machine
class Plugboard
def initialize(wires = '')
@wires = wires
validate unless @wires.empty?
end
def validate
return raise "wire error" unless @wires.is_a?(String)
return raise "wire error" if @wires.chars.uniq!
@jaeming
jaeming / index.html
Created September 21, 2015 16:42
opal 101 ajax
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Opal Experiments</title>
<link rel="stylesheet" href="main.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/opal/0.3.43/opal.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/opal-parser/0.3.43/opal-parser.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/opal-jquery/0.0.8/opal-jquery.min.js"></script>