Skip to content

Instantly share code, notes, and snippets.

View bstolte's full-sized avatar

Brian Stolte bstolte

  • San Francisco, CA
View GitHub Profile
@bstolte
bstolte / reverser_linked_list_1.rb
Last active September 25, 2015 16:02
Reversing a Linked List #1
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
end
def print_values(list_node)
@bstolte
bstolte / luhn.rb
Created October 21, 2015 18:08
Luhn Algorithm - validate credit card numbers
require 'minitest/autorun'
module Luhn
# number must be a string
# number = number.to_s
def self.is_valid?(number)
array = number.to_s.split('')
array = array.reverse
array = array.map.with_index do |x, i|
@bstolte
bstolte / collatz.rb
Created October 21, 2015 18:12
Collatz Sequence - find longest chain of collatz values from 1 to 1,000,000
def collatz(n)
length = 1
loop do
if n == 1
return length
end
if n % 2 == 0
n = n / 2
else
@bstolte
bstolte / fibonacci.rb
Created October 21, 2015 18:17
Use Benchmark to test Fibonacci Sequence Using Iteration and Recursion
# use iteration
def iterative_fib(n)
fib = [1, 1]
a = 0
b = 1
n.times do
fib << fib[a] + fib[b]
a += 1
b += 1