Skip to content
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
4 changes: 2 additions & 2 deletions lib/00_hello.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def hello

return "Hello!"
end

def greet(name)

return "Hello, #{name}!"
end
9 changes: 9 additions & 0 deletions lib/01_temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def ftoc(number)
return ((number - 32) * 0.5556).round(1)
end

def ctof(number)
return ((number * 1.8) + 32).round(1)
end

puts(ctof(302))
29 changes: 29 additions & 0 deletions lib/02_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def add(x, y)
return x + y
end

def subtract(x, y)
return x - y
end

def sum(arr)
return arr.sum
end

def multiply(x, y)
return x * y
end

def power(x, y)
y -= 1
copy = x
y.times {x *= copy}
return (x)
end

def factorial(x)
return 1 if x == 0 || x == 1
return (1..x).inject(:*)
end

puts(factorial(5))
18 changes: 18 additions & 0 deletions lib/03_basics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def who_is_bigger(x, y, z)
return "nil detected" if x == nil || y == nil || z == nil
return "a is bigger" if x > y && x > z
return "b is bigger" if y > x && y > z
return "c is bigger"
end

def reverse_upcase_noLTA(s)
return s.upcase.tr('LTA', '').reverse
end

def array_42(arr)
return arr.include?(42)
end

def magic_array(arr)
arr = arr.flatten.uniq.sort.delete_if{|a| a % 3 == 0 && a != 0}.collect {|n| n * 2}
end
36 changes: 36 additions & 0 deletions lib/04_simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def echo(s)
return s
end

def shout(s)
return s.upcase
end

def repeat(s, x=2)
copy = s
x -= 1
x.times do
s += " " + copy
end
return s
end

def start_of_word(s, x)
return s[0..x-1]
end

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

def titleize(s)
little_words = ["and", "the", "or"]
arr = s.split(/(\W)/)
arr.each do |n|
if little_words.any?(n) == false
n[0] = n[0].upcase
end
end
arr[0][0] = arr[0][0].upcase
return arr.join
end
3 changes: 3 additions & 0 deletions lib/05_timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def time_string(time)
return Time.at(time).utc.strftime("%H:%M:%S")
end
31 changes: 31 additions & 0 deletions lib/06_pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def translate(s)
arr = s.split(" ")
arr.each do |mot|
if mot[0].match(/a|e|i|o|u/i)
mot << "ay"
next
end
if mot[0..2] == "sch"
mot[0..2] = ''
mot << "schay"
next
end
if mot[0..1] == "qu"
mot[0..1] = ''
mot << "quay"
next
end
if mot[1..2] == "qu"
mot << mot[0] + "quay"
mot[0..2] = ''
next
end
cons=""
while mot[0].match(/\A[^aeiou]/i)
mot << mot[0]
mot[0] = ''
end
mot << "ay"
end
return arr.join(" ")
end
35 changes: 24 additions & 11 deletions spec/02_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,36 @@
expect(multiply(3, 4)).to eq(12)
end

it "multiplies two other numbers"
# one other test here, don't forget do end ;)

it "multiplies two other numbers, one of them being 0"
# one last test, with 0 in it
it "multiplies two other numbers" do
expect(multiply(1, 2)).to eq(2)
end

it "multiplies two other numbers, one of them being 0" do
expect(multiply(0, 5)).to eq(0)
end
end

describe "#power" do
it "raises one number to the power of another number"
it "raises one number to the power of another number" do
expect(power(5, 3)).to eq(125)
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