Skip to content

Instantly share code, notes, and snippets.

View mfpiccolo's full-sized avatar

Mike Piccolo mfpiccolo

View GitHub Profile
@mfpiccolo
mfpiccolo / Leap_Year.rb
Last active December 11, 2015 05:48
You can enter in a year and the code will tell you if it is a leap year or not.
def leap_year?(year)
if year % 4 == 0 && year % 100 != 0
year == true
elsif year % 400 == 0
year = true
else
year = false
end
end
@mfpiccolo
mfpiccolo / translate_into_pig_lattin.rb
Last active December 11, 2015 06:09
Enter a word as the argument for translate_into_pig_latin and the program will translate it into pig latin.
def translate_into_pig_latin(word1)
vowels = ["a","e","i","o","u"]
consonants = ["q","w","r","t","y","p","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
if vowels.include?(word1[0])
word1 << "ay"
else
if word1[0..1] == "qu"
word1[2..word1.length] << "qu" << "ay"
elsif consonants.include?(word1[0]) && consonants.include?(word1[1]) && consonants.include?(word1[2])
word1[3..word1.length] << word1[0..2] << "ay"
@mfpiccolo
mfpiccolo / triangle.rb
Last active December 11, 2015 06:19
This program will take three numbers and tell you if it can be represented as a triangle and if so what kind of triangle.
def triangle(number1, number2, number3)
if number1 > number2 + number3 || number2 > number1 + number3 || number3 > number1 + number2
"This is not a triangle"
else
if number1 == number2 && number2 == number3
"You have got yourself an equalateral triangle. You should try and be more creative."
elsif number1 == number2 || number1 == number3 || number2 == number3
'This is an isosceles triangle. So you got that there going for you.'
elsif number1 != number2 && number2 != number3 && number3 != number1
'Oh snap! That is a scalene'
@mfpiccolo
mfpiccolo / factorial.rb
Last active December 11, 2015 06:58
Enter an integer and the program will return the factorial.
#defining the method and argument
def factorial(number1)
#string exception
if number1.kind_of? String
"Enter a number, smartass."
#array exception
elsif number1.kind_of? Array
"Just one number buddy."
#negative numbers exception
else
@mfpiccolo
mfpiccolo / title_case.rb
Created January 18, 2013 05:20
This program takes a sentence and capitalizes each word.
def title_case(sentence1)
if sentence1.kind_of? String
sentence1 = sentence1.downcase.split.to_a.map {|word| word.capitalize }
sentence1.join(" ")
else
"You should try and enter in a sentence."
end
end
@mfpiccolo
mfpiccolo / fibonacci_sequence_recursion.rb
Last active December 11, 2015 09:58
You can use the fibonacci method on positive integers an it will return the correlating fibonacci number.
class Integer
def fibonacci
if self < 0
"You can only use the fibonacci method with positive integers"
else
return self if (0..1).include? self
(self -1).fibonacci + (self - 2).fibonacci
end
end
end
@mfpiccolo
mfpiccolo / fibonacci_sequence1.rb
Created January 21, 2013 05:00
You can use the fibonacci method on a positive integer and it will return the corresponding Fibonacci number
class Integer
def fibonacci
if self.kind_of? Integer
if self < 0
"You can only use the fibonacci method with positive integers"
else
current = 0
successor = 1
@mfpiccolo
mfpiccolo / numbers_to_words.rb
Last active December 11, 2015 10:08
This program turns Integers into english.
class Fixnum
def in_words
#defining all the possible options for numbers < 1000
first_twenty = { 0 => "Zero", 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five", 6 => "Six", 7 => "Seven",
8 => "Eight", 9 => "Nine", 10 => "Ten", 11 => "Eleven", 12 => "Twelve", 13 => "Thirteen", 14 => "Fourteen",
15 => "Fifteen", 16 => "Sixteen", 17 => "Seventeen", 18 => "eighteen", 19 => "Nineteen" }
tens = { 2 => "Twenty", 3 => "Thirty", 4 => "Fourty", 5 => "Fifty", 6 => "Sixty", 7 => "Seventy", 8 => "Eighty",
9 => "Ninety" }
if ((self < 20) && (self.is_a? Integer))
first_twenty[self] #
@mfpiccolo
mfpiccolo / numbers_to_words.rb
Created January 23, 2013 05:27
Use this method on a positive integer less than one quadrillion and it will return it in english.
class Fixnum
def in_words
first_twenty = { "0" => "Zero", "1" => "One", "2" => "Two", "3" => "Three", "4" => "Four", "5" => "Five", "6" => "Six", "7" => "Seven",
"8" => "Eight", "9" => "Nine", "10" => "Ten", "11" => "Eleven", "12" => "Twelve", "13" => "Thirteen", "14" => "Fourteen",
"15" => "Fifteen", "16" => "Sixteen", "17" => "Seventeen", "18" => "eighteen", "19" => "Nineteen" }
tens = { "2" => "Twenty", "3" => "Thirty", "4" => "Fourty", "5" => "Fifty", "6" => "Sixty", "7" => "Seventy", "8" => "Eighty",
"9" => "Ninety" }
big_number = { 3 => "-Hundred and ", 4 => "-Thousand", 5 => "-Thousand", 6 => "-Thousand", 7 => "-Million", 8 => "-Million", 9 => "-Million", 10 => "-Billion", 11 => "-Billion", 12 => "-Billion",
13 => "-Trillion", 14 => "-Trillion", 15 => "-Trillion" }
number_string = self.to_s
@mfpiccolo
mfpiccolo / Dice.rb
Created January 24, 2013 05:33
my_dice.roll will give you a random number from one to six. to change the number of sides change the argument in Die.new()
class Die
attr_accessor :sides
def initialize(number)
@number = number
end
def roll
rand(1..@number)
end