-
Notifications
You must be signed in to change notification settings - Fork 0
submit week5 assignment #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,8 @@ | ||
| #write your code here | ||
| def hello | ||
| "Hello!" | ||
| end | ||
|
|
||
| def greet(name) | ||
| "Hello, #{name}!" | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,8 @@ | ||
| #write your code here | ||
| def ftoc(temperature) | ||
| return (temperature - 32) * 5 / 9 | ||
| end | ||
|
|
||
| def ctof(temperature) | ||
| return temperature * 9.0 / 5 + 32 | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good also. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,32 @@ | ||
| #write your code here | ||
| def add(num1, num2) | ||
| return num1 + num2 | ||
| end | ||
|
|
||
| def subtract(num1, num2) | ||
| return num1 - num2 | ||
| end | ||
|
|
||
| def sum(array) | ||
| return array.sum | ||
| end | ||
|
|
||
| def multiply(*nums) #ruby treats "*nums" as an array | ||
| multiply = 1 | ||
| nums.each do |n| | ||
| multiply *= n | ||
| end | ||
| return multiply | ||
| end | ||
|
|
||
| def power(num1, num2) | ||
| num1**num2 | ||
| end | ||
|
|
||
| def factorial(num) | ||
| result = 1 | ||
| num.times do |number| | ||
| result*= (number+1) | ||
| end | ||
| return result | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well done! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,22 +78,35 @@ | |
| # write tests and code for the following: | ||
|
|
||
| describe "#multiply" do | ||
|
|
||
| it "multiplies two numbers" | ||
|
|
||
| it "multiplies several numbers" | ||
|
|
||
| it "multiplies two numbers" do | ||
| expect(multiply(2,6)).to eq(12) | ||
| end | ||
| it "multiplies several numbers" do | ||
| expect(multiply(2,6,5)).to eq(60) | ||
| 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,3)).to eq(8) | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is properly done! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,36 @@ | ||
| #write your code here | ||
| def echo(string) | ||
| return string | ||
| end | ||
|
|
||
| def shout(string) | ||
| return string.upcase | ||
| end | ||
|
|
||
| def repeat(string, num=2) | ||
| result = "" | ||
| result = (string + " ") * num | ||
| return result.strip | ||
| end | ||
|
|
||
| def start_of_word(string, num) | ||
| return string[0, num] | ||
| end | ||
|
|
||
| def first_word(string) | ||
| return string.split[0] | ||
| end | ||
|
|
||
| def titleize(string) | ||
| little_words= ["and", "over", "the"] | ||
| result = "" | ||
| string.split.each do |word| | ||
| if !little_words.include?(word) | ||
| result += word.capitalize + " " | ||
| else | ||
| result += word + " " | ||
| end | ||
| end | ||
| result = result[0].upcase + result[1,result.length-1] | ||
| return result.strip | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,56 @@ | ||
| #write your code here | ||
| def translate(string) | ||
| result = [] | ||
| vowels = ["a", "e", "i", "o", "u"] | ||
|
|
||
| string.split.each do |word| | ||
| word_copy = word | ||
| min = word.length | ||
| vowels.each do |vowel| | ||
| if word.downcase.index(vowel) != nil && word.downcase.index(vowel) <=min #find the first vowel's index | ||
| min = word.downcase.index(vowel) | ||
| end | ||
| end | ||
| temp = "" #to save the punctuation | ||
| if !("a".."z").include?(word[word.length-1]) && !("A".."Z").include?(word[word.length-1]) | ||
| temp = word[word.length-1] #save the punctuation | ||
| word = word.chop #remove the punctuation | ||
| end | ||
| new_word = word[min, word.length] + word[0, min] | ||
| if new_word[0].downcase == "u" && new_word[new_word.length-1].downcase == "q" #deal with "qu" case | ||
| new_word = new_word.delete_prefix("u") + "u" | ||
| end | ||
| if ("A".."Z").include?(word_copy[0]) #capitalized words were capitalized | ||
| new_word = new_word.downcase.capitalize | ||
| end | ||
| result.push(new_word + "ay" + temp) | ||
| end | ||
| return result.join(" ") | ||
| end | ||
|
|
||
| # Below is the code shared by one student using regex, keep it as reference. | ||
| # vowels = ["a", "e", "i", "o", "u"] | ||
| # #vowels = 'aeiou'.split('') | ||
| # #vowels = %w[a e i o u] | ||
|
|
||
| # def translate (string) | ||
| # vowels = %w[a e i o u] | ||
| # words = string.split | ||
| # new_words = [] | ||
|
|
||
| # words.each do |word| | ||
| # new_word = | ||
| # if word.match /^.*qu/ | ||
| # first_part = word[/^.*qu/] | ||
| # last_part = word.delete first_part | ||
| # last_part + first_part + 'ay' | ||
|
|
||
| # else !vowels.include?(word[0]) | ||
| # first_part = word[/[^aeiou]*/] | ||
| # last_part = word.delete first_part | ||
| # last_part + first_part + 'ay' | ||
| # end | ||
| # new_words.push new_word | ||
| # end | ||
| # new_words.join ' ' | ||
| # end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,4 +69,14 @@ | |
| # * 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 "capitalize the words that capitalized before" do | ||
| s = translate("The Quick Brown Fox") | ||
| expect(s).to eq("Ethay Ickquay Ownbray Oxfay") | ||
| end | ||
|
|
||
| it "retains the puctuation from the original phrase" do | ||
| s = translate("Ha! I got you!") | ||
| expect(s).to eq("Ahay! Iay otgay ouyay!") | ||
| end | ||
|
|
||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,36 @@ | ||
| class Book | ||
| # write your code here | ||
| # write your code here | ||
| attr_accessor :title | ||
| def initialize | ||
| @title = "" | ||
| end | ||
|
|
||
| def title | ||
| little_words= ["and", "over", "the", "an", "a", "in", "of"] | ||
| result = "" | ||
| @title.split.each do |word| | ||
| if !little_words.include?(word) | ||
| result += word.capitalize + " " | ||
| else | ||
| result += word + " " | ||
| end | ||
| end | ||
| result = result[0].upcase + result[1,result.length-1] | ||
| title = result.strip | ||
| end | ||
| end | ||
|
|
||
| #Another student's code using each_with_index method | ||
| # def title | ||
| # little_words = ["and","the","a","an","in","of","in"] | ||
| # c= "" | ||
| # @title.split.each_with_index do |string, index| | ||
| # if index == 0 | ||
| # c=string.capitalize | ||
| # elsif little_words.include? string | ||
| # c+= " " + string | ||
| # else c+= " " + string.capitalize | ||
| # end | ||
| # end | ||
| # return c | ||
| # end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code in the title method looks like it would work, but how are you getting input (as in a string) for the method to operate on? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| class Timer | ||
| #write your code here | ||
| attr_accessor :seconds | ||
|
|
||
| def initialize | ||
| @seconds = 0 | ||
| end | ||
|
|
||
| def time_string | ||
| hours = @seconds / 60 / 60 | ||
| seconds = @seconds % 60 | ||
| tmp = @seconds / 60 | ||
| minutes = tmp % 60 | ||
|
|
||
| padded(hours) + ":" + padded(minutes) + ":" + padded(seconds) | ||
| end | ||
|
|
||
| def padded(num) | ||
| return result = num >= 10 ? num.to_s : "0#{num}" | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,16 +45,16 @@ | |
| # Uncomment these specs if you want to test-drive that | ||
| # method, then call that method from inside of time_string. | ||
| # | ||
| # describe 'padded' do | ||
| # it 'pads zero' do | ||
| # expect(@timer.padded(0)).to eq('00') | ||
| # end | ||
| # it 'pads one' do | ||
| # expect(@timer.padded(1)).to eq('01') | ||
| # end | ||
| # it "doesn't pad a two-digit number" do | ||
| # expect(@timer.padded(12)).to eq('12') | ||
| # end | ||
| # end | ||
| describe 'padded' do | ||
| it 'pads zero' do | ||
| expect(@timer.padded(0)).to eq('00') | ||
| end | ||
| it 'pads one' do | ||
| expect(@timer.padded(1)).to eq('01') | ||
| end | ||
| it "doesn't pad a two-digit number" do | ||
| expect(@timer.padded(12)).to eq('12') | ||
| end | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good. |
||
|
|
||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct.