This file contains hidden or 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
class Node | |
attr_accessor :parent, :left, :right, :value | |
def is_leaf? | |
left.nil? && right.nil? | |
end | |
def has_left_child? | |
!left.nil? | |
end |
This file contains hidden or 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
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] | |
}) |
This file contains hidden or 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
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 |
This file contains hidden or 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
source "https://rubygems.org" | |
gem "minitest" |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
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] } |
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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! |
This file contains hidden or 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
<!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> |
NewerOlder