Skip to content

Instantly share code, notes, and snippets.

@aokolish
aokolish / closest_web_safe.rb
Created November 3, 2017 21:14
closest web safe color...v2
WEB_SAFE_NUMBERS = [
0, # 00
51, # 33
102, # 66
153, # 99
204, # cc
255 # ff
]
def closest_web_safe_color(string)
@aokolish
aokolish / closest_web_safe.rb
Created November 3, 2017 20:35
closest web safe color
WEB_SAFE_NUMBERS = [
0, # 00
51, # 33
102, # 66
153, # 99
204, # cc
255 # ff
]
def closest_web_safe_color(string)
# https://leetcode.com/problems/count-primes/description/
# return the number of prime numbers less than a positive number n
def count_primes(n)
primes = []
(2..n-1).each do |i|
primes[i] = true
end
(2..Math.sqrt(n)).each do |i|
class Solution
def initialize
@known_strings = {}
end
def word_break(string, word_dict)
if @known_strings[string]
return @known_strings[string]
end
def replace_words(dict, sentence)
regex = /\A(#{dict.join('|')})/
sentence.split.inject([]) do |new_words, word|
if match_data = word.match(regex)
new_words << match_data.captures[0]
else
new_words << word
end
end.join(' ')
@aokolish
aokolish / _layout.html.haml
Last active August 29, 2015 14:19
Nested layout in rails
-# This file contains the 'layout' for this section of the site
%section.how-it-works-page.content-section
.container
.row
.col-md-12
%h2.section-title How It Works
.sub-nav.row
.col-sm-3
= active_link_to how_we_find_kids_path, class: 'link-wrap' do
.img.school
class GeneratedPDF
def generate
# pdf stuff...
pdf_file = Tempfile.new(['something', '.pdf'])
pdf_file.binmode
pdf_file.write(pdf_string)
pdf_file
end
end
@aokolish
aokolish / example.rb
Last active August 29, 2015 14:02
rails 4 argument out of range issue
# rails 3, invalid times are set to nil
> m = Model.new some_timestamp: '2010-33-22T09:30:25Z'
=> #<Model ...>
> m.some_timestamp
=> nil
# rails 4, invalid times raise an exception
> m = Model.new some_timestamp: '2010-33-22T09:30:25Z'
=> ArgumentError: argument out of range
@aokolish
aokolish / search.exs
Last active June 16, 2016 23:34
binary search. `elixir search.exs` to run this. assuming you have elixir (`brew install elixir`)
defmodule Search do
def chop(_val, []), do: -1
def chop(val, [head|tail]) when tail == [] do
if val == head, do: 0, else: -1
end
def chop(val, list) do
chop(val, 0, length(list) - 1, list)
end
class Dependencies
def initialize
@dependencies = Hash.new {|h,k| h[k] = []}
end
def add_direct item, new_dependencies
@dependencies[item] += new_dependencies
end
def dependencies_for item