Skip to content

Instantly share code, notes, and snippets.

View ryanveroniwooff's full-sized avatar

Ryan Wooff ryanveroniwooff

View GitHub Profile
@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
#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])
#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
@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
@ryanveroniwooff
ryanveroniwooff / word-counter.rb
Last active February 1, 2017 00:07
Word Counting Algorithm - Utilizing the Hash
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
@ryanveroniwooff
ryanveroniwooff / output.rb
Last active February 1, 2017 10:12
Outputs variable to file in ruby language
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
@ryanveroniwooff
ryanveroniwooff / sort.rb
Last active April 21, 2017 10:34
Array Sorting Method in Ruby (one liner included for funsies)
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
@ryanveroniwooff
ryanveroniwooff / string_to_dollars.rb
Created June 14, 2017 00:19
Format ruby variable to us dollar
# 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
# 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
@ryanveroniwooff
ryanveroniwooff / christmas_tree.rb
Last active October 8, 2017 10:56
Download this file and run in your console for a merry christmas message!
# 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