This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder