Created
September 24, 2012 23:06
-
-
Save sgottlieb/3779002 to your computer and use it in GitHub Desktop.
pig latin and numbers to words excercises
This file contains hidden or 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
#pig latin | |
def translate(word) | |
vowels = ["a", "e", "i", "o", "u"] | |
if vowels.include?(word[0]) | |
return word <<"ay" | |
elsif word[0..2] =="sch" | |
return word[3..-1] <<word[0..2]<<"ay" | |
elsif vowels.include?(word[1]) == false | |
return word[2..-1] << word[0..1] <<"ay" | |
elsif word[0..1] =="qu" | |
return word[2..-1] << word[0..1] <<"ay" | |
else | |
return word[1..-1] << word[0] << "ay" | |
end | |
end | |
#numbers to words | |
class Integer | |
def in_words | |
numbers = {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 => 'forty', 5 => 'fifty', 6 => 'sixty', 7 => 'seventy', 8 =>'eighty', | |
9 =>'ninety'} | |
other = { 1000000000000 =>' trillion', 1000000000 =>' billion', 1000000 =>' million',1000 => ' thousand', 100=>' hundred' } | |
ourNum = self | |
returnNum = '' | |
#goes through other hash and uses recursion | |
other.each do |key, value| | |
if ourNum/key >= 1 | |
nums = (ourNum/key) | |
ourNum = ourNum%key | |
if ourNum> 0 | |
returnNum = returnNum + (nums).in_words + value +' ' | |
else | |
returnNum = returnNum + (nums).in_words + value | |
end | |
end | |
end | |
#checks tens | |
if ourNum/10 > 1 | |
num = ourNum/10 | |
ourNum = ourNum%10 | |
if ourNum>0 | |
returnNum = returnNum + tens[num]+ ' ' | |
else returnNum = returnNum + tens[num] | |
end | |
end | |
#checks teens and single digits | |
if ourNum >= 1 | |
returnNum = returnNum + numbers[ourNum] | |
end | |
#returns the total number in words | |
returnNum | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment