Created
February 24, 2014 00:46
-
-
Save gsheppard/9179716 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
# report average, lowest, and highest scores | |
require 'pry' | |
def bubble_sort scores | |
bubbled = scores | |
outer = 0 | |
while outer < bubbled.length - 1 | |
inner = 0 | |
while inner < bubbled.length - 1 | |
if bubbled[inner] > bubbled[inner+1] | |
bubbled[inner], bubbled[inner+1] = bubbled[inner+1], bubbled[inner] | |
end | |
inner += 1 | |
end | |
outer += 1 | |
end | |
bubbled | |
end | |
def get_average scores | |
total = 0 | |
scores.each {|num| total += num } | |
total / scores.length | |
end | |
# ------- Method Barrier | |
test_scores = [75, 100, 85, 65, 84, 87, 95] | |
sorted = bubble_sort(test_scores.dup) | |
print " Test scores: " | |
puts test_scores.join(' ') | |
print "Sorted scores: " | |
puts sorted.join(' ') | |
print " Lowest score: " | |
puts sorted[0] | |
print "Highest score: " | |
puts sorted[-1] | |
print "Average score: " | |
puts get_average(sorted) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work!
One formatting note: Setting your tab size in Sublime to 2 should avoid the big indents. (A tab size of 2 is standard for Ruby code.)
A few suggestions / notes:
outer
andinner
, I'd useouter_counter
andinner_counter
. (Good naming is one of the hardest things in coding, I've found -- definitely a learned skill.)Good job!