Skip to content

Kenny Kottenstette Version 1 Submission - learn_ruby #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 00_hello/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def hello
"Hello!"
end

def greet(who)
"Hello, #{who}!"
end
7 changes: 7 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def ftoc(f)
(f-32.0) * (5.0/9)
end

def ctof(c)
(c * (9.0/5)) + 32
end
26 changes: 26 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def add (num1, num2)
num1 + num2
end

def subtract (num1, num2)
num1 - num2
end

def sum (numbers)
#numbers.reduce(0) { |current_num, next_num| current_num + next_num }
numbers.reduce(0, :+)
end

def multiply (*args)
args.reduce(1, :*)
end

def power (base, exponent)
base ** exponent
end

def factorial number
accumulator = 1
number.downto(1) { |i| accumulator *= i }
accumulator
end
39 changes: 27 additions & 12 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,38 @@
# once the above tests pass,
# write tests and code for the following:

describe "#multiply" do
describe "multiply" do

it "multiplies two numbers"

it "multiplies several numbers"

it "multiplies two numbers" do
expect(multiply(3,4)).to eq(12)
end
it "multiplies several numbers" do
expect(multiply(3,4,2)).to eq(24)
end
end

describe "#power" do
it "raises one number to the power of another number"
describe "power" do
it "raises one number to the power of another number" do
expect(power(4,3)).to eq(64)
end

end

# http://en.wikipedia.org/wiki/Factorial
describe "#factorial" do
it "computes the factorial of 0"
it "computes the factorial of 1"
it "computes the factorial of 2"
it "computes the factorial of 5"
it "computes the factorial of 10"
it "computes the factorial of 0" do
expect(factorial(0)).to eq(1)
end
it "computes the factorial of 1" do
expect(factorial(1)).to eq(1)
end
it "computes the factorial of 2" do
expect(factorial(2)).to eq(2)
end
it "computes the factorial of 5" do
expect(factorial(5)).to eq(120)
end
it "computes the factorial of 10" do
expect(factorial(10)).to eq(3628800)
end
end
29 changes: 29 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def echo(word)
return word
end

def shout(word)
return word.upcase
end

def repeat(word, iterations=2)
repeats = []
iterations.times { repeats << word }
return repeats.join(" ")
end

def start_of_word(word, amount)
return word.slice(0, amount)
end

def first_word(words)
return words.split(" ")[0]
end

def titleize(string)
array = string.split(" ")

return array.map.with_index do |word, index|
(index == 0) || (word == array[-1]) || (word.length > 4) ? word.capitalize : word
end.join(" ")
end
43 changes: 43 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
VOWELS = ['a', 'e', 'i', 'o', 'u']

def translate(string)
words = string.split(" ")

return words.map do |word|
translate_word word
end.join(" ")

end


def translate_word(word)

working_index = nil
group1, group2, groups = '', '', ''

if word[0..1] == 'qu' || word[1..2] == 'qu'
regex = word.scan(/\As?q[a-z]/)
working_index = regex[0].length
group1 = word[0...working_index]
group2 = word[working_index..-1]

elsif !(VOWELS.include? word[0])
regex = word.scan(/\A[^aeiou]+/)
working_index = regex[0].length
group1 = word[0...working_index]
group2 = word[working_index..-1]

else
group2 = word
end

groups = group2 + group1
if groups.end_with? 'a'
return groups + 'y'
else
return groups + 'ay'
end

end


11 changes: 11 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def reverser
return yield.split(" ").map { |word| word.reverse }.join(" ")
end

def adder(int = 1)
return yield + int
end

def repeater(i=3)
return i.times { yield }
end
20 changes: 20 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def measure(i=1)
i = i.size if i.is_a?(Array)

total = 0

i.times do
if block_given?
start_time = Time.now
yield
total += Time.now - start_time
else
start_time = Time.now
total += Time.now - start_time
end
end

return total / i

end

6 changes: 6 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Friend
def greeting(name = nil)
name ? "Hello, #{name}!" : "Hello!"
end
end

26 changes: 26 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Book
#be better to seperate by exceptions, conjuctions, and prepositions?
#most effecient way to do an_array.include?)

#exceptions
CONJUNCTIONS = %w[and]
PREPOSITIONS = %w[in of]
OTHER_WORDS = %w[the a an]

ALL_EXCEPTIONS = CONJUNCTIONS + PREPOSITIONS + OTHER_WORDS

EXCEPTIONS = %w[the a an]

def title=(name)
@name = name
end

def title
#@name.split.map(&:capitalize).join(" ")

@name.split.map.with_index do |word, index|
!ALL_EXCEPTIONS.include?(word) || index == 0 ? word.capitalize : word
end.join(" ") #just keeping it playful with some spaghetti code :-)
end

end
50 changes: 50 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Timer

attr_accessor :seconds

def initialize(secs = 0)
@seconds = secs
@minutes = 0
@hours = 0
end

# def seconds
# @seconds
# end

# def seconds=(seconds)
# @seconds = seconds
# end

def seconds_to_minutes_hours
while @seconds >= 60
if (@seconds - 3600 > 0)
@hours += 1
@seconds -= 3600

elsif (@seconds - 60 > 0)
@minutes += 1
@seconds -= 60
end
end

end

def time_string

seconds_to_minutes_hours

#INT TO STRING CONVERSIONS
@sec_str = @seconds.to_s
@min_str = @minutes.to_s
@hour_str = @hours.to_s

@sec_str = '0' + @sec_str if @seconds < 10
@min_str = '0' + @min_str if @minutes < 10
@hour_str = '0' + @hour_str if @hours < 10


"#{@hour_str}:#{@min_str}:#{@sec_str}"

end
end
36 changes: 36 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Temperature

def initialize hash
@fahrenheit = hash[:f]
@celsius = hash[:c]
end

def in_fahrenheit
@fahrenheit ||= @celsius * (9.0/5) + 32
end

def in_celsius
@celsius ||= (@fahrenheit - 32.0) * (5.0 / 9)
end

def self.from_celsius(degrees)
self.new(:c => degrees)
end

def self.from_fahrenheit(degrees)
self.new(:f => degrees)
end

end

class Celsius < Temperature
def initialize degrees
@celsius = degrees
end
end

class Fahrenheit < Temperature
def initialize degrees
@fahrenheit = degrees
end
end
43 changes: 43 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Dictionary

attr_reader :entries, :keywords
def initialize
@entries = Hash.new()
@keywords = []
end

def add(entry)
if String === entry
@keywords << entry
@entries.store(entry, nil)
else
@entries.merge!(entry)
@keywords << entry.keys[0]
end
@keywords.sort!
end

def include?(keyword)
@keywords.include?(keyword)
end

def find(word)
# include?(word) ? match_it(word) : {}
matches = {}
@keywords.each do |keyword|
if keyword.start_with?(word)
matches[keyword] = @entries[keyword]
end
end
return matches
end

def printable
output = []
keywords.each do |keyword|
output << "[#{keyword}] \"#{entries[keyword]}\""
end
output * "\n"
end

end
Loading