Skip to content

Instantly share code, notes, and snippets.

View ryanveroniwooff's full-sized avatar

Ryan Wooff ryanveroniwooff

View GitHub Profile
@ryanveroniwooff
ryanveroniwooff / sum_of_array.rb
Last active January 23, 2017 23:30
Find the sum of an array with mixed data types
def iterative_sum(array)
array.map{|e| is_num?(e) ? e : string_value(e)}.inject(:+)
end
def recursive_sum(array, sum)
return sum if array.empty?
sum += is_num?(array[0]) ? array.shift : string_value(array.shift)
recursive_sum(array, sum)
end
#generates a collatz sequence for input n
def generate_sequence(n)
seq = [n]
while (n != 1) do
n.even? ? n /= 2 : n = (3 * n + 1)
seq << n
end
return seq
end
#finds number with the longest collatz sequence between 1 and 1 million
#Benchmark Test for Recursive vs Iterative Functions
# of the Fibonacci Sequence Written in Ruby by Ryan Wooff
def recursive_fib(n)
n == 0 || n==1 ? n : recursive_fib(n-1) + recursive_fib(n-2)
end
def iterative_fib(n)
table = [1,1]
n.times do
@fib_table = table[table.length-1] + (table[table.length - 2])
@ryanveroniwooff
ryanveroniwooff / quiz.rb
Created October 13, 2016 13:33
Quiz #3
class Spider
attr_accessor :totalSpiders
def initialize (totalSpiders)
@totalSpiders = totalSpiders
end
def spinWeb
#make spiders spin web
puts "#{totalSpiders} spiders began to spin webs.."
end