Skip to content

Instantly share code, notes, and snippets.

##########################
# => factorial iterative
##########################
def factorial_it(n)
return (1..n).inject(:*)
end
#puts factorial(10)
# actual funtion to get the prime factors
def prime_factors(n)
primes = []
until n == 1
primes << prime_number(n)
n /= primes.last
puts "primes_array:#{primes.inspect} | n:#{n}"
end
def birthday_timestamp(yyyy,mm,dd)
Time.new(yyyy,mm,dd)
end
#birthday_timestamp
def age(yyyy,mm,dd)
@flakyfilibuster
flakyfilibuster / regular_expression.rb
Created October 11, 2012 00:20
Regular Expression
# # Determine whether a string contains a Social Security number.
def has_ssn?(string)
/\d{3}-\d{2}-\d{4}/.match(string)
end
# Return the Social Security number from a string.
def grab_ssn(string)
string[/\d{3}-\d{2}-\d{4}/]
end
@flakyfilibuster
flakyfilibuster / binary_search.rb
Created October 12, 2012 03:39
binary search
def binary_search(num, array)
return -1 if !array.include?(num)
index = (array.length)/2
index_array = [(array.length), 0]
until num == array[index]
index = (index_array[0] + index_array[1])/2
@flakyfilibuster
flakyfilibuster / numbers_to_words.rb
Created October 15, 2012 02:18
Numbers to words
$numbers_hash = {1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eigth", 9 => "nine", 10 => "ten",
10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen", 14 => "forteen", 15 => "fifteen", 16 => "sixteen", 17 => "seventeen",
18 => "eighteen", 19 => "nineteen", 20 => "twenty", 30 => "thirty", 40 => "forty", 50 => "fifty", 60 => "sixty", 70 => "seventy",
80 => "eighty", 90 => "ninty", 100 => "hundred", 1_000 => "thousand", 1_000_000 => "million", 1_000_000_000 => "billion",
1_000_000_000_000 => "trillion"}
def ransom_below_100(number,modifier,result)
quotient,remainder = number.divmod(10)
@flakyfilibuster
flakyfilibuster / csv_after.csv
Created October 18, 2012 07:54
CSV methods for easy csv to array and array to csv conversion
Peggie Bernier (598)-885-3990 georgianna.casper@emardvolkman.org
Ines Roberts (717)-629-5254 alanis.hane@bayer.com
Carley Luettgen (153)-754-3308 jeramy_terry@kerluke.org
Evie Bashirian (684)-662-7552 herminia_jewe@schumm.biz
@flakyfilibuster
flakyfilibuster / hospital.rb
Created October 19, 2012 02:58
hospital app
class Hospital
attr_accessor :name, :employees, :patients
def initialize(name, location)
@name, @location, @employees, @patients = name, location, [], []
end
end
@flakyfilibuster
flakyfilibuster / todo.rb
Created October 21, 2012 22:17
Todo app
require 'yaml'
class TodoList < Hash
def initialize(file_name)
@file_name = file_name
File.open( @file_name, 'w' ) { |file| YAML.dump( self, file )}
end
@flakyfilibuster
flakyfilibuster / option_parser.rb
Created October 22, 2012 18:55
OptionParser Example
require 'optparse'
options = {date: false, sort: false, ddate: false, tag: false, prio: false}
optparse = OptionParser.new do|opts|
opts.on("--date") do
options[:date] = true
end
opts.on('--sort') do