From 18ce47b8c2dbd5c8d2567d9449b55a87afd14656 Mon Sep 17 00:00:00 2001 From: Loris Aranda Date: Wed, 27 Jul 2016 14:35:33 +0200 Subject: [PATCH] exercises completed --- 00_hello/hello.rb | 7 +++++++ 01_temperature/temperature.rb | 7 +++++++ 02_calculator/calculator.rb | 17 ++++++++++++++++ 03_simon_says/simon_says.rb | 37 +++++++++++++++++++++++++++++++++++ 04_pig_latin/pig_latin.rb | 18 +++++++++++++++++ 05_book_titles/book.rb | 20 +++++++++++++++++++ 06_timer/timer.rb | 25 +++++++++++++++++++++++ 7 files changed, 131 insertions(+) diff --git a/00_hello/hello.rb b/00_hello/hello.rb index 251797306..1af487b15 100644 --- a/00_hello/hello.rb +++ b/00_hello/hello.rb @@ -1 +1,8 @@ #write your code here +def hello + return "Hello!" +end + +def greet(name) + return "Hello, #{name}!" +end diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb index 251797306..7eaf3e704 100644 --- a/01_temperature/temperature.rb +++ b/01_temperature/temperature.rb @@ -1 +1,8 @@ #write your code here +def ftoc(f) + c = (f.to_f - 32.0) * (5.0/9.0) +end + +def ctof(c) + f = (c.to_f * 9.0 / 5.0) + 32.0 +end \ No newline at end of file diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb index 251797306..454cbaa4b 100644 --- a/02_calculator/calculator.rb +++ b/02_calculator/calculator.rb @@ -1 +1,18 @@ #write your code here +def add (a, b) + return a + b +end + +def subtract (a, b) + return a - b +end + +def sum (array) + sum = 0 + array.each do |a| + sum += a.to_i + end + return sum +end + + diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb index 251797306..20b18787b 100644 --- a/03_simon_says/simon_says.rb +++ b/03_simon_says/simon_says.rb @@ -1 +1,38 @@ #write your code here +def echo (text) + return text +end + +def shout (text) + return text.upcase +end + +def repeat (text, n=2) + + result = "" + n.times{result += text + " "} + + return result.chomp(" ") +end + +def start_of_word (word, n=1) + return word[0..n-1] +end + +def first_word (string) + string.split(" ")[0] +end + +def titleize (title) + result = "" + title = title.split(" ") + title.each_with_index do |word, index| + if word == "and" or word == "over" or word == "the" and index != 0 + result.concat(word + " ") + else + result.concat(word.capitalize + " ") + end + end + + return result.chomp(" ") +end diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb index 251797306..84a247dd6 100644 --- a/04_pig_latin/pig_latin.rb +++ b/04_pig_latin/pig_latin.rb @@ -1 +1,19 @@ #write your code here +def translate (string) + + def transWord (word) + if word[0] == "a" or word[0] == "e" or word[0] == "i" or word[0] == "o" or word[0] == "u" and word[-1] != "q" + word = word.concat("ay") + else + word = word.concat(word[0]) + word = word[1..word.length-1] + translate(word) + end + end + + result = string.split(" ") + result.each_with_index do |word, index| + result[index] = transWord(word) + end + return result.join(" ") +end \ No newline at end of file diff --git a/05_book_titles/book.rb b/05_book_titles/book.rb index 009f2643e..293d2ca90 100644 --- a/05_book_titles/book.rb +++ b/05_book_titles/book.rb @@ -1,3 +1,23 @@ class Book # write your code here + attr_accessor :title + + def initialize + @title = "" + end + + def title + result = "" + @title = @title.split(" ") + @title.each_with_index do |word, index| + if word == "and" or word == "over" or word == "the" or word == "in" or word == "of" or word == "a" or word == "an" and index != 0 + result.concat(word + " ") + else + result.concat(word.capitalize + " ") + end + end + + return result.chomp(" ") +end + end diff --git a/06_timer/timer.rb b/06_timer/timer.rb index 987ae7603..52d7636ce 100644 --- a/06_timer/timer.rb +++ b/06_timer/timer.rb @@ -1,3 +1,28 @@ class Timer #write your code here + attr_accessor :seconds + + def initialize + @seconds = 0 + end + + def time_string + + string = "" + + def convertTime (time) + if time < 10 + time = "0" + time.to_s + else + time = time.to_s + end + end + + hours = seconds / 3600 + minutes = (seconds % 3600) / 60 + secondsleft = (seconds % 3600) % 60 + + string = convertTime(hours) + ":" + convertTime(minutes) + ":" + convertTime(secondsleft) + end + end