Skip to content
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

Completed Test First ruby examples #91

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8e888b6
completed hello tests
andie5 Nov 7, 2016
d943b23
temperature functions created and tests passed
andie5 Nov 13, 2016
0f015ab
calculator with extra factorial, muitply and powere functions - mutip…
andie5 Nov 13, 2016
a82649e
all cases for simon_says test passes
andie5 Nov 13, 2016
7f04290
first 2 tests passed and multiple words for the traslate pig_latin
andie5 Nov 13, 2016
0d87501
translated all the cases for pig_latin
andie5 Nov 14, 2016
d2b0e56
translated all the cases for pig_latin
andie5 Nov 14, 2016
bc6d0b5
translated all the cases for pig_latin
andie5 Nov 14, 2016
0181ecc
initial performance monitor and blocks code
andie5 Nov 15, 2016
69f0d39
completed tests for silly blocks
andie5 Nov 16, 2016
c287cb7
completed tests for silly blocks
andie5 Nov 16, 2016
44b3d90
performance monitor some tests
andie5 Nov 17, 2016
886e2f6
performance tests complete
andie5 Nov 17, 2016
b0d6f86
Book titles tests complete
andie5 Nov 20, 2016
b4efb08
time 1 test
andie5 Nov 20, 2016
8203270
timer code tests passed
andie5 Nov 23, 2016
8a4d271
Temperture object structure
andie5 Dec 7, 2016
83ae1de
completed temperature tests
andie5 Dec 8, 2016
673420e
different implemantations of the find method to find an exact value
andie5 Dec 8, 2016
50e7353
all dictionary tests - refining printable function
andie5 Dec 9, 2016
4be12eb
all dictionary tests
andie5 Dec 9, 2016
5470ff7
all dictionary tests
andie5 Dec 9, 2016
e04113a
all tests for reverse polish calculator complete
andie5 Dec 9, 2016
a968bf8
completed extension to array class
andie5 Dec 10, 2016
0088450
all tests up to thousands
andie5 Dec 10, 2016
42a7d99
done tests up to thousands
andie5 Dec 10, 2016
d496415
completed tests for numbers in words
andie5 Dec 11, 2016
5f92d38
friends tests complete
andie5 Dec 11, 2016
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
Binary file added .DS_Store
Binary file not shown.
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(name)
"Hello, #{name}!"
end
9 changes: 9 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Convert celsius to farenheit
def ftoc(n)
((n - 32) * 5) / 9
end

# Convert farenheit to celsius
def ctof(n)
((n.to_f * 9) / 5) + 32
end
59 changes: 59 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# TDD - RSpec
# Add 2 numbers
def add(num1, num2)
num1 + num2
end

# Subtract 2 numbers
def subtract(num1,num2)
num1 - num2
end


# Sum an array of numbers
def sum(nums)
total = 0
nums.each do |n|
total+= n
end
return total
end

# Multiply numbers with a variable number of arguments
def multiply(*nums)
total = 1

num_of_args = nums.length

if(num_of_args == 1)
total = nums[0]
else
nums.each do |n|
total *= n
end
end
return total
end

# Power of a number
def power(base, exponent)
total = 1

exponent.times do
total *= base
end

return total
end

# Factorial of a number
def factorial(n)
if(n==1)
return 1
elsif (n<1)
return -1
elsif(n>1)
return(n*factorial(n-1))
end
end

37 changes: 28 additions & 9 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,40 @@

describe "#multiply" do

it "multiplies two numbers"
it "multiplies two numbers" do
expect(multiply(5,7)).to eq(35)
end

it "multiplies several numbers"

it "multiplies several numbers" do
expect(multiply(2,3,5)).to eq(30)
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(2,4)).to eq(16)
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
56 changes: 56 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# TDD RSpec
# simon_says.rb

# Prints the word
def echo(word)
return word
end

# Converts the word to upper case
def shout(word)
upper_case = word.upcase
end

# Repeat a word a particular number of times
def repeat(word, num=2)
word = word + " "
word *= num

# Removes trailing whitespace
word.rstrip
end

# Returns the first n characters of a string
def start_of_word(word, num_chars)
word[0, num_chars]
end

# Get the first word of a sentence
def first_word(word)
words = word.split(" ")
words[0]
end

# Capitalizes first letter of each word in a title and ignores little words - the first word in the title is always capitalized
def titleize(word)
words = word.split(" ")
little_words = ["and", "or", "the", "but", "over"]
first_word = words.first

words.each do |word|
banned_word = false

# Check if the word is a little word
little_words.each do |banned|
if(banned == word)
banned_word = true
end
end

# If its the first word of the title or not a banned word it can be capitalized
if((first_word == word) || (banned_word == false))
word.capitalize!
end
end
words.join(" ")
end
50 changes: 50 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# TDD RSpec
# pig_latin_spec.rb

def translate(words)

word_arr = words.split(" ")

# Check one word at time
word_arr.each_with_index do |word, i|
j = 0
not_vowels = ""
continguous_con = true

# If it is a vowel, just append "ay"
if(is_vowel(word,0))
word.concat("ay")
else
# Checks first 3 characters of a word
while j < 3
if((is_vowel(word,j) == false) && continguous_con)
not_vowels += word[j]
else
continguous_con = false
end
j += 1
end

num_of_con = not_vowels.length
word_arr[i] = word.slice!(num_of_con..-1).concat(not_vowels) + "ay"
end
end

word_arr.join(" ")
end


# Returns true or false if the letter is a vowel
def is_vowel(word, pos)
# "u" is not considered a vowel in pig latin
vowels = ["a", "e", "i", "o"]
is_vowel = false
vowels.each do |vowel|
if(word[pos] == vowel)
is_vowel = true
end
end
return is_vowel
end


5 changes: 4 additions & 1 deletion 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@

# Test-driving bonus:
# * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course)
# * retain the punctuation from the original phrase
it "translates many words and retain the punctuation from the original phrase" do
s = translate("The quick brown Fox")
expect(s).to eq("eThay ickquay ownbray oxFay")
end

end
38 changes: 38 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# TDD RSpec
# silly_blocks.rb

# Reverse words in string
def reverser
str = yield
words = str.split(" ")

words.each do |word|
word.reverse!
end
words.join(" ")
end

result = reverser do
"hello"
end

reverse_words = reverser do
"hello dolly"
end


def adder(n=1)
yield + n
end

adder(3) do
5
end


def repeater(n=1)
n.times do
yield
end
end

24 changes: 24 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# TDD RSpec
# performance_monitor.rb

def measure(n=1)
time_start = Time.now

if (n>1)
n.times do
yield
end
else
yield
end

# Get the time elapsed if the function is called once otherwise
# get the average time
(Time.now - time_start) / (n==1 ? 1 : n)
end

measure do
end

measure(4) do
end
12 changes: 12 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# TDD RSpec
# friend.rb

class Friend
def greeting(who=nil)
if(who.nil?)
"Hello!"
else
"Hello, " + who +"!"
end
end
end
42 changes: 42 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# TDD RSpec
# book.rb

class Book

# List of words not to capitalize
@@conjunctions = ["a", "an", "and", "the", "in", "of"]

# Return the title
def title
@title
end

# Set the title
def title=(t)
@title = titleize(t)
end


# Titleize every word except a list of stored conjunctions
# Always titleize the first word
def titleize(t)
words = t.split(" ")
first_word = words.first
words.each do |word|
special_word = false
@@conjunctions.each do |x|
# puts "conjunction is #{x} and word is #{word}"
if(x == word)
special_word = true
end
end

if((first_word == word) || !special_word)
word.capitalize!
end
end

words_upper = words.join(" ")
end

end
Loading