Skip to content

Behdad Analui's solution #90

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 2 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
8 changes: 8 additions & 0 deletions 00_hello/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#write your code here
def hello
"Hello!"
end

def greet which_person
"Hello, " + which_person + "!"
end
8 changes: 8 additions & 0 deletions 01_temperature/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#write your code here
def ftoc farenheit
centigrade = (farenheit - 32.0) * 5.0 / 9.0
end

def ctof centigrade
farenheit = centigrade * 9.0 / 5.0 + 32.0
end
49 changes: 49 additions & 0 deletions 02_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#write your code here
def add first_num, second_num
result = first_num + second_num
end

def subtract first_num, second_num
result = first_num - second_num
end

def sum array
result = 0;
array.length.times do |i|
result = result + array[i]
end
result
end

def multiply first_num, second_num
result = first_num * second_num
end

def multiply_arr array
result = 1;
array.length.times do |i|
result = result * array[i]
end
result
end

def power first_num, second_num
result = first_num
(second_num - 1).times do
result = result * first_num
end
result
end

def fact number
result = 1
number.times do |i|
result = result * (i + 1)
end
if(number == 0)
0
else
result
end
end

46 changes: 46 additions & 0 deletions 03_simon_says/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#write your code here
def echo what_to_echo
what_to_echo
end

def shout what_to_shout
what_to_shout.upcase
end

def repeat what_to_repeat, how_many_times = 2
result = ""
(how_many_times - 1).times do
result = result + what_to_repeat + " "
end
result = result + what_to_repeat
end

def start_of_word the_word, how_many_letters
how_many_letters_m_1 = how_many_letters - 1
result = the_word[0..how_many_letters_m_1]
end

def first_word the_sentence
the_first_word = the_sentence.split
the_first_word[0]
end

def titleize the_sentence
little_words = ["and", "or", "over", "but", "the"]
the_split_version = the_sentence.split
(the_split_version.length - 1).times do |i|
if(i != 0)
if(little_words.include? the_split_version[i])
the_split_version[i] = the_split_version[i] + " "
else
the_split_version[i].capitalize!
the_split_version[i] = the_split_version[i] + " "
end
else
the_split_version[i].capitalize!
the_split_version[i] = the_split_version[i] + " "
end
end
the_split_version[the_split_version.length - 1].capitalize!
the_split_version.join
end
45 changes: 45 additions & 0 deletions 04_pig_latin/pig_latin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
def translate the_sentence
vowels_array = ["a", "e", "i", "o" , "u"]
the_words = the_sentence.split
the_words.length.times do |i|
the_word = the_words[i]
if (vowels_array.include? the_word[0])
the_word = the_word + "ay"
else
the_word = word_piggy(the_word)
if (vowels_array.include? the_word[0])
the_word = word_piggy(the_word) + "ay"
else
the_word = word_piggy(the_word)
if (vowels_array.include? the_word[0])
the_word = word_piggy(the_word) + "ay"
else
the_word = word_piggy(the_word)
if (vowels_array.include? the_word[0])
the_word = word_piggy(the_word) + "ay"
else
the_word = word_piggy(the_word)
end
end
end
end
the_word = the_word + " "
the_words[i] = the_word
end
new_words = the_words.join
new_words.chomp(" ")
end

def word_piggy the_word
vowels_array = ["a", "e", "i", "o" , "u"]
if (the_word[0..1] == "qu")
x = the_word.length - 1
new_word = the_word[2..x] + "qu"
elsif (vowels_array.include? the_word[0])
new_word = the_word
else
x = the_word.length - 1
new_word = the_word[1..x] + the_word[0]
end
new_word
end
22 changes: 22 additions & 0 deletions 05_silly_blocks/silly_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def reverser
str = yield
str_arr = str.split(" ")
str_arr.each do |string|
string.reverse!
end
new_arr = []
str_arr.size.times do |index|
new_arr << str_arr[index]
new_arr << " "
end
new_arr.join.strip
end

def adder (num = 1)
value = yield
value += num
end

def repeater (num = 1)
num.times {yield}
end
11 changes: 11 additions & 0 deletions 06_performance_monitor/performance_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def measure(num = 1)
cur_time = Time.now
all_times = []
num.times {
cur_time = Time.now
yield
after_time = Time.now
all_times << after_time - cur_time
}
all_times.inject(0) { |sum, el| sum + el } / all_times.size
end
5 changes: 5 additions & 0 deletions 07_hello_friend/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Friend
def greeting (string = "")
(string.size > 0) ? "Hello, #{string}!" : "Hello!"
end
end
21 changes: 21 additions & 0 deletions 08_book_titles/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Book
# write your code here
attr_accessor :title
def initialize

end
def title=(title)
exceptions = ["a", "the", "and", "an", "in", "and", "of"]
title_words = title.split
title_words.length.times do |i|
if((i != 0) && (exceptions.include? title_words[i]))
title_words[i] = title_words[i] + " "
else
title_words[i].capitalize!
title_words[i] = title_words[i] + " "
end
end
@title = title_words.join
@title = @title.chomp(" ")
end
end
32 changes: 32 additions & 0 deletions 09_timer/timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Timer
#write your code here
attr_accessor :hours, :minutes, :seconds, :time_string

def initialize
@seconds = 0
@hours = 0
@minutes = 0
@time_string = "00:00:00"
end
def seconds=(seconds)
@seconds = seconds
if(seconds > 3599)
@hours = seconds / 3600
@minutes = (seconds - (@hours * 3600)) / 60
@seconds = seconds - (@hours * 3600) - (@minutes * 60)
elsif(seconds > 59)
@seconds = seconds % 60
@minutes = seconds / 60
@hours = 0
else
@seconds = seconds
@hours = 0
@minutes = 0
end
second_string = "%0.2d" % [@seconds]
minute_string = "%0.2d" % [@minutes]
hour_string = "%0.2d" % [@hours]
@time_string = hour_string + ":" + minute_string + ":" + second_string
end

end
41 changes: 41 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Temperature
@in_fahrenheit
@in_celsius
def initialize(options={})
if options[:f]
@in_fahrenheit = options[:f]
in_celsius
end
if options[:c]
@in_celsius = options[:c]
in_fahrenheit
end
end

def self.from_celsius(cel_deg)
new(:c => cel_deg)
end
def self.from_fahrenheit(far_deg)
new(:f => far_deg)
end

def in_fahrenheit
@in_fahrenheit = (@in_celsius) * 9.0 / 5.0 + 32.0
end

def in_celsius
@in_celsius = (@in_fahrenheit - 32.0) * 5.0 / 9.0
end
end

class Celsius < Temperature
def initialize(deg)
super(:c => deg)
end
end

class Fahrenheit < Temperature
def initialize(deg)
super(:f => deg)
end
end
45 changes: 45 additions & 0 deletions 11_dictionary/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Dictionary
@entries
@printable_output
def initialize
@entries = {}
end
def entries
@entries
end
def add(hash={})
if hash.is_a? Hash
hash.each do |key, value|
@entries[key] = value
end
else
@entries[hash] = nil
end
@entries
end
def keywords
@entries.keys.sort
end

def include?(string)
@entries.include? string
end

def find(string)
result = Hash.new
if @entries.empty? || string == 'nothing'
{}
else
@entries.select{|key, value| key.include? string }
end
end

def printable
@printable_output = ""
@sorted_entries = @entries.sort_by {|key, value| key}
@sorted_entries.each do |key, value|
@printable_output = @printable_output + "\[#{key}\]" + " \"#{value}\"" + "\n"
end
@printable_output.chomp
end
end
Loading