Skip to content

Ruby Exercises by Odogwudozilla #7

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 13 commits into
base: master
Choose a base branch
from
Open
34 changes: 17 additions & 17 deletions exercises/_SUGGESTED_ORDER.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
This is the **suggested order** for completing the exercises:

1. [max.rb](max.rb)
1. [min.rb](min.rb)
1. [longest_string.rb](longest_string.rb)
1. [shortest_string.rb](shortest_string.rb)
1. [word_count.rb](word_count.rb)
1. [count_in_list.rb](count_in_list.rb)
1. [count_max.rb](count_max.rb)
1. [sum.rb](sum.rb)
1. [mean.rb](mean.rb)
1. [print_square.rb](print_square.rb)
1. [print_triangle.rb](print_triangle.rb)
1. [print_pyramid.rb](print_pyramid.rb)
1. [max.rb](max.rb) => DONE
1. [min.rb](min.rb) => DONE
1. [longest_string.rb](longest_string.rb) => DONE
1. [shortest_string.rb](shortest_string.rb) => DONE
1. [word_count.rb](word_count.rb) => DONE
1. [count_in_list.rb](count_in_list.rb) => DONE
1. [count_max.rb](count_max.rb) => DONE
1. [sum.rb](sum.rb) => DONE
1. [mean.rb](mean.rb) => DONE
1. [print_square.rb](print_square.rb) =>DONE
1. [print_triangle.rb](print_triangle.rb) => DONE
1. [print_pyramid.rb](print_pyramid.rb) => DONE
1. [print_horizontal_pyramid.rb](print_horizontal_pyramid.rb)
1. [hot_or_cold.rb](hot_or_cold.rb)
1. [bottles.rb](bottles.rb)
1. [find_even.rb](find_even.rb)
1. [mode.rb](mode.rb)
1. [commas.rb](commas.rb)
1. [hot_or_cold.rb](hot_or_cold.rb) => DONE
1. [bottles.rb](bottles.rb) => DONE
1. [find_even.rb](find_even.rb) => DONE
1. [mode.rb](mode.rb) => DONE
1. [commas.rb](commas.rb) => DONE
1. [factorial.rb](factorial.rb)
1. [fibonacci.rb](fibonacci.rb)
1. [find_links.rb](find_links.rb)
Expand Down
12 changes: 11 additions & 1 deletion exercises/bottles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@
# etc.

def bottles(start_number)
puts "Counting down..."
start_number.downto(1) do |i|
if i > 1
print "#{i} bottles of beer on the wall, #{i} bottles of beer. Take one down, pass it around, "
puts "#{i-1} bottles of beer on the wall!"
else
print "#{i} bottles of beer on the wall, #{i} bottles of beer. Take one down, pass it around, no more bottles of beer on the wall!"
end
end
#I could not implement the method at the moment so I just coded it logically straight
end

if __FILE__ == $PROGRAM_NAME
# What *should* this print?
bottles(5)
bottles(50)
end

# Hint #1:
Expand Down
12 changes: 12 additions & 0 deletions exercises/commas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@
# insert the commas? Which comma would you insert first?

def commas(num)
num = num.to_s.reverse # converts to string and reverses the numbers

num = num.gsub(/(\d{3})/,"\\1,") # inserts a comma after every 3rd letter

num.chomp(",").reverse #reverses the number again
end

#one line solution <number.to_s.reverse.gsub(/(\d{3})/,"\\1,").chomp(",").reverse>

if __FILE__ == $PROGRAM_NAME
# What are the common cases? What are the corner cases?
# Your sanity checks should look like
# p commas(input) == ...expected return value...
p commas(123) == "123"
p commas(1234) == "1,234"
p commas(12345) == "12,345"
p commas(1234567) == "1,234,567"
p commas(1235649) == "1,235,649"
end

# Hint #1
Expand Down
14 changes: 14 additions & 0 deletions exercises/count_in_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@ def count_in_list(list, item_to_count)
# 1. A running total of the number of times you've seen the item
# 2. A way to loop/iterate through the list
# 3. A way to add to the running total as you see the item
mycount = [] # Sets the initial count of item_to_count to empty array

list.each do |str| # For each str in list
if str == item_to_count # If str is equal to item_to_count
mycount.push(str) # Push str to the mycount array
end

end

return mycount.count # Return the count of str in mycount
end

if __FILE__ == $PROGRAM_NAME
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
p count_in_list(["A", "way", "to", "add", "to", "the"], "to") == 2
p count_in_list([1,1,1], 1) == 3
p count_in_list([1,2,3], -1) == 0
p count_in_list([1,2,3], 1) == 1
end
11 changes: 11 additions & 0 deletions exercises/count_max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ def count_max(list)
#
# But remember: inelegant, working code is better than elegant,
# unfinished code.
item = max(list) # Sets item to the maximum number in the list

list.each do |num| # For each num in the list do
maxcount = count_in_list(list, item) # Sets citem equal to the count of item
return maxcount # returns the count
end

end

if __FILE__ == $PROGRAM_NAME
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
p count_max([1, 2, 3]) == 1
p count_max([50, -100, 50, -200]) == 2
p count_max([-200, -400, -100, -300]) == 1
p count_max([10, 1,2,10,10]) == 3
end
21 changes: 19 additions & 2 deletions exercises/find_even.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# find_even([10,10,10,11,11,11]) == [10,10,10]

def find_even(array)
array1 = []
array1 = array.find_all { |ar| ar % 2 == 0}
array = array1
end

# Note #1
Expand Down Expand Up @@ -43,14 +46,28 @@ def find_even(array)

# If the input is the empty array,
# find_even should return the empty array

p find_even([]) == []
# If the input array contains all EVEN numbers,
# find_even should return the input array
puts
p find_even([2, 4, 6, 8, 10]) == [2, 4, 6, 8, 10]
print find_even([2, 4, 6, 8, 10]) #Just to make double sure onscreen that I got it ;)
puts

# If the input array contains all ODD numbers,
# find_even should return the empty array

puts
p find_even([3, 5, 7, 9, 11]) == []
print find_even([3, 5, 7, 9, 11, 13]) #Just to make double sure onscreen that I got it ;)
puts
# If an even number appears N times in the input array,
# it should appear N times in the the array that find_even returns
puts
p find_even([2, 4, 4, 6, 8, 10]) == [2, 4, 4, 6, 8, 10]
print find_even([2, 4, 4, 6, 8, 10]) #Just to make double sure onscreen that I got it ;)
puts

puts
p find_even([1, 2, 3, 4, 5, -6, 7, -8, 9, 10]) == [2, 4, -6, -8, 10]
print find_even([1, 2, 3, 4, 5, -6, 7, -8, 9, 10]) #Just to make double sure onscreen that I got it ;)
end
6 changes: 3 additions & 3 deletions exercises/hot_or_cold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def hot_or_cold(num_to_guess)
guess = get_user_guess() # "guess" is now an integer

if guess < num_to_guess # The guess is too cold
____
puts "The guess is too cold. Try again"
elsif guess > num_to_guess # The guess is too hot
____
puts "The guess is too hot. Try again"
else # The guess is juuuust right
____
puts "The guess is juuuust right. You're the man!!!"

# This "return" will make the program return from hot_or_cold, even from
# inside the while loop.
Expand Down
18 changes: 17 additions & 1 deletion exercises/longest_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@
# str.length == 4

def longest_string(list)
# This is your job. :)

my_long_str = list[0] # assigns a varible to the first array index
list.each do |str| # for each str in list
if str.length > my_long_str.length # if the lengthof current str is greater than my_long_str
my_long_str = str # set str to my_long_str
end
end
return my_long_str
end

# A far better and shorter solution (on StackOverflow) is my_long_str = list.max_by(&:length)
# But I will stick with my own homegrown solution


if __FILE__ == $PROGRAM_NAME
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?

p longest_string(["We", "might", "have", "missed", "a", "corner", "case"]) == "missed"
p longest_string(["Remember", "that", "conceptually", "x"]) == "conceptually"

p longest_string(["123456789", "might", "have", "23564.25689", "a", "corner", "case"]) == "23564.25689"
end
4 changes: 2 additions & 2 deletions exercises/max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def max(list)
# "If this file is the file currently being executed, then..."
#
# __FILE__ is always the name of the current file.
# $0 is always the name of the program beind executed.
# $0 is always the name of the program being executed.
#
# This means that if we run this file directly, i.e.,
# ruby max.rb
Expand Down Expand Up @@ -61,7 +61,7 @@ def max(list)
# prints out "true."" For example,
# 1. We might have missed a corner case
# 2. The code does what it should, but is conceptually confused
# 3. Something else we haven't though of
# 3. Something else we haven't thought of
#
# Remember: Option #3 is *always* possible.
#
Expand Down
8 changes: 8 additions & 0 deletions exercises/mean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@
def mean(list)
total = sum(list) # This is the "sum" method from our sum.rb file
# result = ____ # Given the list's sum, how can we calculate the average?

result = (total)/list.count # Divides total by list count and set value to result

return result # return result
end

if __FILE__ == $PROGRAM_NAME
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
p mean([64, 10, 21, -69, 33]) == 11
p mean([44, -59, 98, 82, 13]) == 35
p mean([84, 55, -62, 3, 99]) == 35
p mean([12, 68, 21, -86, 80]) == 19
end
10 changes: 5 additions & 5 deletions exercises/min.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# This is going to be very similar to max, so don't be afraid if
# these two methods look almost identical
def min(list)
____ = ____
____.each do |____|
if ____
____ = ____
start_small = list[0] #or equivalently, list.first
list.each do |numba| # or equivalently, for each item in list, do something
if numba < start_small # if current numba is less than start_small
start_small = numba # set start_small to the numba
end
end

return ____
return start_small # Since we have gone through the list, return start_small
end

if __FILE__ == $PROGRAM_NAME
Expand Down
8 changes: 8 additions & 0 deletions exercises/mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
# Break it down as clearly as you can in your own head first.

def mode(array)
maxi = Hash.new(0) # Creates an empty hash
array.select{ |nums| maxi[nums] += 1} # For each element in the array, collects the element and frequencies (key/value pair) and appends to the maxi Hash
maxi.max_by { |a, b| b}.first # Checks each key/value pair in the maxi hash and returns the "first" key with the highest value.

end

if __FILE__ == $PROGRAM_NAME
Expand Down Expand Up @@ -49,4 +53,8 @@ def mode(array)
p mode(["a", "a", "a", "b"]) == "a"
p mode(["b", "a", "a", "a"]) == "a"
p mode(["a", "b", "a", "a"]) == "a"

# This `mode depends on the order of the data`
p mode(["a", "b", "b", "a"]) == "a"
p mode(["b", "a", "b", "a"]) == "b"
end
19 changes: 18 additions & 1 deletion exercises/print_horizontal_pyramid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@
# *
# ***
# *****
# *******
# # *******
# def asterisks(count)
# count.upto(2 * 1-i ) do
# print "*"
#
# end
# end

# def spaces(space)
# space.downto(1) do |s|
# print "*"
#
# end
# end

def print_horizontal_pyramid(height)
height.times {|n|
print ' ' * (height - n)
puts '*' * (2 * n + 1)
}
end

if __FILE__ == $PROGRAM_NAME
Expand Down
20 changes: 20 additions & 0 deletions exercises/print_pyramid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,29 @@ def print_pyramid(height)
# Suggestion: you can call print_triangle to print out the first, "upward"
# half of the pyramid. You'll have to write code to print out the second,
# "downward" half of the pyramid.

print_triangle(height)
height.downto(1) do |i| # or, equivalently, reverse the range from height to 1
print_line(i) # I called the print_line method to repeat the same process as in print_triangle.rb

end

end

if __FILE__ == $PROGRAM_NAME
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
print_pyramid(1)

print "\n\n\n" # This is here to make the separation between pyramids clearer

print_pyramid(2)

print "\n\n\n" # This is here to make the separation between pyramids clearer

print_pyramid(3)

print "\n\n\n" # This is here to make the separation between pyramids clearer

print_pyramid(10)
end
9 changes: 6 additions & 3 deletions exercises/print_square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@

# The print_line method is here to help you.
# Conceptually, it prints out a row of "count" *'s. Run it yourself to
# see how it works. Experiment with different inputs.
# see how it works. Experiment with xRails params explained?different inputs.
def print_line(count)
(1..count).each do |i| # or, equivalently, for i in (1..count)
print "*" # This prints a single "*"
end

print "\n" # This forces the output to the next line, like hitting "return"
#print "\n" # This forces the output to the next line, like hitting "return"
end

# We can call methods we've defined ourselves. In this case, we want
# to call the print_line method we've defined to help us print out a square.
def print_square(dimension)

(1..dimension).each do |i| # or, equivalently, for i in (1..dimension)
print_line(____) # Fill in the blank, here.
print_line(dimension) # Recursion. Kind of. I called the print_line method on the print_square method using the print_square arguement. I don't know what its called. It just works ;).
print "\n"
end

end

# There are no rumble strips this time. It's up to you to decide whether
Expand Down
6 changes: 5 additions & 1 deletion exercises/print_triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def print_line(count)
end

def print_triangle(height)
# You have to fill in the details here.
(1..height).each do |i| # or, equivalently, for i in (1..height)
print_line(i) # I called the print_line method on the print_triangle method using each item on print_triangle as arguement. I don't know what its called. It just works ;). I acctually solved this by mistake, trying to solve print_square.rb

end

end

# There are no rumble strips this time. It's up to you to decide whether
Expand Down
Loading