Skip to content

Instantly share code, notes, and snippets.

View moserrya's full-sized avatar

Ryan Moser moserrya

View GitHub Profile
@moserrya
moserrya / clean_chain.rb
Created January 9, 2013 17:07
A solution to the ZeroCater math-y challenge that is capable of finding the fifth clean chain of primes.
# Take a sequence of N numbers. We'll call the sequence a "Clean Chain of length N"
# if the sum of the first N - 1 numbers is evenly divisibly by the Nth number.
# For example, the sequence [2, 4, 6, 8, 10] forms a clean chain of length 5,
# since 2 + 4 + 6 + 8 = 20, which is divisible by 10, and the sequence has 5 numbers.
# The first clean chain formed out of the sequence of primes is simply [2], with length 1.
# The second is [2, 3, 5] with length 3.
# The third is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
# 53, 59, 61, 67, 71], with length 20
@moserrya
moserrya / gist:4671370
Created January 30, 2013 07:14
best fizzbuzz ever
(1..100).map {|i| (fb = [["Fizz"][i%3],["Buzz"][i%5]].compact.join).empty? ? i : fb}

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@moserrya
moserrya / gist:5053341
Created February 28, 2013 01:06
lots of html
<!DOCTYPE html>
<html lang="en">
<head>
<!--
normalize.css removes cross-browser differences in defaults, e.g.,
differences in how form elements appear between Firefox and IE
See: http://necolas.github.com/normalize.css/
-->
<link rel="stylesheet" href="normalize.css">
@moserrya
moserrya / rpn_calc.rb
Last active December 14, 2015 18:39
Stabby RPN
class RPNCalculator
def initialize
@special_cases = [->(op, arr) {arr.include?('0') && op == '*' && '0'},
->(op, arr) {arr.first == ('0') && op == '/' && '0'},
->(op, arr) {arr.include?('1') && op == '*' && (arr-['1']).first},
->(op, arr) {arr.include?('0') && op =~ /[+-]/ && (arr-['0']).first}]
end
def evaluate(*inputs)
class CalculatesRoute
def self.calculate(points)
remaining_points = points
route = []
route << remaining_points.slice!(0)
until remaining_points == [] do
next_point = shortest_distance(route.last, remaining_points)
route << remaining_points.slice!(remaining_points.index(next_point))
require_relative 'geocoder'
require 'shoulda-matchers'
require 'geocoder'
require 'debugger'
class ActivityCluster
MAX_ROUTE_LENGTH = 4
MAX_DISTANCE = 20
@moserrya
moserrya / justify.js
Last active December 16, 2015 10:58
Justify strings to a given width. Works in-place without breaking the string into an array.
function justify(str, width) {
var whitespace = ' ';
while (str.length < width) {
var oldLength = str.length;
var spacing = new RegExp("(\\w)" + whitespace + "(\\w)");
str = str.replace(spacing, '$1 ' + whitespace + '$2');
if (oldLength === str.length) {
whitespace += ' ';
}
}
@moserrya
moserrya / rearrange.rb
Created April 19, 2013 23:19
Rearrange the elements of one array according to an index in another array without using additional space in memory. Note: This solution works but destroys the positions array in the process!
characters = [ 'o', 'h', 'e', 'd', 'n', 'r', 'y', 'g', 'a', 'b', 'e' ]
positions = [ 1, 0, 4, 2, 6, 9, 8, 3, 7, 10, 5]
def rearrange(characters, positions)
positions.length.times do |i|
positions[i] = characters[positions[i]]
end
positions.each_with_index {|e, i| characters[i] = e}
end
@moserrya
moserrya / shifted_sorted.rb
Created April 22, 2013 22:39
Find the smallest number in a shifted sorted array that runs in log(n) time
def find_index(ary, index = 0)
midpt = ary.length / 2
if midpt <= 1
return ary.first < ary.last ? index : index + 1
end
left, right = ary[0...midpt], ary[midpt..-1]
if left.last > left.first && ary.last < ary.first
index += midpt
find_index(right, index)
else