Created
February 23, 2014 17:38
-
-
Save eveadele/9174548 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = [75, 100, 85, 65, 84, 87, 95] | |
def highest(list_given) | |
maximum = 0 | |
list_given.each do |item| | |
if item > maximum | |
maximum = item | |
end | |
end | |
return maximum | |
end | |
def lowest(list_given) | |
minimum = list_given[0] | |
list_given.each do |item| | |
if item < minimum | |
minimum = item | |
end | |
end | |
return minimum | |
end | |
def add_up_list(list_given) | |
total = 0 | |
list_given.each do |item| | |
total += item | |
end | |
total | |
end | |
def list_size(list_given) | |
size = 0 | |
list_given.each do |item| | |
size += 1 | |
end | |
size | |
end | |
def average(sum, list_given_size) | |
sum / list_given_size | |
end | |
puts "Highest: #{highest(list)}" | |
puts "Lowest: #{lowest(list)}" | |
puts "Average: #{average(add_up_list(list), list_size(list))}" |
Nice job!
I think gists add whitespace? I have all the Sublime Text settings, and when I open up the file in Sublime there's no whitespace. But thanks for all the feedback!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would probably refactor your
#add_up_list
method to be more generic/reusable:Another option here could be to use the Enumerable#inject.
You could also take it a step further and use a shorthand version of inject like this:
More info about the crazy shorthand at http://www.potstuck.com/2011/08/06/ruby-symbols-instead-of-blocks/
Also, it's totally cool to write it out the way you did.
#inject
can be tricky to get the hang of. I just wanted to point out that it exists.