Skip to content

Instantly share code, notes, and snippets.

@iamsok
Created August 16, 2014 21:58
Show Gist options
  • Save iamsok/e3248cb783610674ba06 to your computer and use it in GitHub Desktop.
Save iamsok/e3248cb783610674ba06 to your computer and use it in GitHub Desktop.
Systems Check
scores = [75, 100, 85, 65, 84, 87, 95]
def avg(array)
total = 0
array.each do |grade|
total += grade
end
avg = total / array.length
avg
end
puts avg(scores)
def min(array)
min = array[0]
array.each do |grade|
if grade < min
min = grade
end
end
min
end
puts min(scores)
def max(array)
max = array[-1]
array.each do |grade|
if grade > max
max = grade
end
end
max
end
puts max(scores)
@hchood
Copy link

hchood commented Aug 18, 2014

Excellent work. These look great. Just a few comments:

Average method

  • Might want to return a float instead of an integer
  • Don't need the last line, since the return value of avg = total / array.length is the average. You also don't need to assign a variable in that case, since you aren't referencing it later, so your final method might look something like:
def avg(array)
  total = 0

  array.each do |grade|
    total += grade
  end

  total.to_f / array.length
end

Min and max methods

  • These look great. Only note I'd make is on naming conventions. I'd probably give your argument a more descriptive name than array, such as numbers or grades. Then in your each loop you can pass it number, num, or grade in the pipes. It's just a bit more descriptive.

Misc.

  • This is a personal preference thing, but I like leaving a blank line above and below each loops and most conditionals. I find it guides the eye and makes code easier to read. Ex.:
def max(numbers)
  max = array[-1] # you could also start with array[0] here

  numbers.each do |num|
    if num > max
      max = num
    end
  end

  max
end

@hchood
Copy link

hchood commented Aug 18, 2014

Also, in case it's helpful, here's the criteria we use in "grading" these things. The ones towards the bottom will apply to all systems checks:

average method has correct signature
average method calculates average
average method converts to float (OPTIONAL)
average method handles empty list (OPTIONAL)
average method does not modify array
max method has correct signature
max method calculates max
max method does not modify array
max method does not sort the array
max method does not use the Array#max method
min method has correct signature
min method calculates min
min method does not modify array
min method does not sort the array
min method does not use the Array#min method

Proper indentation (2 spaces, no tabs).
Methods and variables have clear, meaningful names.
Method and variable names use underscores, class and module names use camel-case.
No global or instance variables.
Newline included at end of file.
No consecutive (2+) blank lines.
No unnecessary "puts" or "binding.pry" statements

Late for standup without notifying mentor.
Missed standup without notifying mentor.
Absent without notifying mentor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment