Skip to content

Instantly share code, notes, and snippets.

@mkweick
mkweick / luhn.rb
Created November 26, 2015 17:15
The Luhn Algorithm
class Luhn
attr_reader :cd, :luhn, :luhn_sum
def initialize(num, create = nil)
@luhn = num.to_s.split('').map(&:to_i)
@cd = luhn.pop unless create
end
def addends
num = (luhn.size - 1)
@mkweick
mkweick / sieve.rb
Last active November 26, 2015 17:17
Sieve of Eratosthenes
class Sieve
attr_reader :max, :range
def initialize(max)
@max = max
@range = {}
(2..max).each { |n| range[n] = 1 }
end
def primes
@mkweick
mkweick / series.rb
Last active November 26, 2015 17:18
Series
class Series
attr_reader :series
def initialize(series)
@series = series
end
def slices(slice)
valid?(slice)
last_valid_index = series.length - slice
@mkweick
mkweick / tealeaf_course2_lesson2_quiz.rb
Last active November 1, 2015 19:00
Tealeaf - Course 2 - Lesson 2 Quiz
1.) index, new, create, show, edit, update, delete
GET, POST, PATCH/PUT, DELETE
GET '/photos', to: photos#index
GET '/photos/new, to: photos#new
POST '/photos, to: photos#create
GET '/photos/:id', to: photos#show
GET '/photos/:id/edit', to: photos#edit
PATCH '/photos/:id', to: photos#update
DELETE '/photos/:id', to: photos#delete
"can use as: 'something' to create a custom named route"
@mkweick
mkweick / minesweeper.rb
Last active November 26, 2015 17:18
Minesweeper
class Board
def self.transform(input)
output = []
fail ValueError unless valid_game?(input)
input.each_with_index do |row, row_index|
if row_index == 0 || row_index == (input.size - 1)
output << row
else
output_row = ''
@mkweick
mkweick / tealeaf_course2_lesson1_quiz
Last active November 1, 2015 18:02
Tealeaf - Course 2 - Lesson 1 Quiz
1.) Data is stored in tables in a manner where it can retrieved by a unique ID (primary ID)
and can be related to data in other tables via foreign key.
2.) Structured Query Language is language used to interact with relational databases
3.) schema view --> structure of the table (column name, column type, PK, FK, etc...)
data view ----> actual data in columns from the table
4.) Primary Key
@mkweick
mkweick / simulator.rb
Last active November 26, 2015 17:18
Robot Simulator
class Robot
attr_accessor :bearing, :coordinates
def orient(direction)
if [:north, :east, :south, :west].include? direction
self.bearing = direction
else
raise ArgumentError, "Invalid direction"
end
end
@mkweick
mkweick / cipher.rb
Last active November 26, 2015 17:18
Simple Cipher
class Cipher
VALUES = [('a'..'z').to_a, (0..25).to_a].transpose.to_h
attr_reader :key
def initialize(key = new_key)
@key = key
raise_error_conditions
end
def encode(msg)
@mkweick
mkweick / triplet.rb
Last active November 26, 2015 17:19
Pythagorean Triplet
class Triplet
attr_reader :a, :b, :c
def initialize(a, b, c)
@a = a
@b = b
@c = c
end
def sum