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 |
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
#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
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
def count_words(s) | |
return nil if s.nil? || s.split.empty? | |
s.downcase.gsub(/[^a-z0-9'!\s-]/i, '').split(" ").each_with_object(Hash.new(0)){ |word,counts| counts[word] += 1 } | |
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
def self.output_to_file(item) | |
fname = "program-output.txt" | |
time = Time.new.strftime("%a, %b %d @ %I:%M:%S %p") | |
file = File.open(fname, "a") # a = append, w = wipe | |
file.puts "#{time}: #{item} \n\n" | |
file.close | |
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
def sort(a, a_is_unsorted = true) | |
# (a_is_unsorted = false; (a.size - 1).times{ |i| j = i + 1; a[i], a[j], a_is_unsorted = a[j], a[i], true if a[i] > a[j]}) while a_is_unsorted | |
while a_is_unsorted do | |
a_is_unsorted = false | |
(a.length - 1).times{ |i| | |
a[i], a[i + 1], a_is_unsorted = a[i + 1], a[i], true if a[i] > a[i + 1] | |
} | |
end | |
a | |
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
# use the .to_dollars method on any string and get appropriate | |
# dollar amount returned with two decimal places rounded to the hundreths | |
# format is for us dollar | |
class String | |
def to_dollars | |
d = self.decimals_present | |
return "$" + self + "00" if d == 0 | |
return "$" + self + "0" if d == 1 | |
return "$" + self if d == 2 | |
return "$" + self.decimals[1] + '.' + self.round_decimals if d > 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
# readFile sets up a new file right away by calling writeFile | |
# then stores the file as readfile and returns the formatFile method on the readfile | |
def readFile | |
writeFile | |
file = File.open(File.expand_path File.dirname(__FILE__) + "/output.txt") | |
readfile = file.read | |
file.close | |
readfile.formatFile | |
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
# This Program creates and outputs a formatted and colorized chirstmas tree with "Merry Christmas!" below it | |
# This code handles colorization of console output | |
class String | |
# colorization | |
def colorize(color_code) | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
def red |
OlderNewer