Skip to content

Instantly share code, notes, and snippets.

@jfarmer
Forked from yoshprogrammer/Sum_Attempt
Last active August 29, 2015 14:01
Show Gist options
  • Save jfarmer/7fa801ac131bcc9fd686 to your computer and use it in GitHub Desktop.
Save jfarmer/7fa801ac131bcc9fd686 to your computer and use it in GitHub Desktop.

Questions

How do I get it to return per line, isn't that the point of this exercise? For instance, how would I get it to return each value, such as line 35's list "total = 0" on one line, next "total = 0 + 11", next line "total = 11+22".

I could not find a way to do it like, 'running_total = return item[0], return item[0] + item[1]...item[i]'.

First, it's good that you understand what you need to do and are referring to the last item of the list as item[i]. Keep in mind that if the list is in the variable list then to get the item at, say, index 10, we want list[10], not item[10]. Imagine if you could write code like this:

def sum(list)
  # The list has "n" elements in it
  running_total = 0
  running_total = running_total + list[0]
  running_total = running_total + list[1]
  running_total = running_total + list[2]
  # ...
  running_total = running_total + list[n-1]
  # Not list[n].  If a list has 10 elements, the last element is list[9].
  
  return running_total
end

Do you see how this would get you what you want? Of course, the ... is the tricky bit here. We don't know beforehand how many items are in the list. This is exactly what looping is for. We can write the loop "by hand" like so, if we really, really want:

def sum(list)
  i = 0
  running_total = 0
  while i < list.length
    running_total = running_total + list[i]
  end

  return running_total
end

The "counter variable" or "index variable" we've labeled i is just for bookkeeping. The computer needs it, but we as programms don't — it's noise. The concept we're trying to express is "for each number in the list...". Ruby, like many programming languages, lets us write this directly and skip the bookkeeping:

def sum(list)
  total = 0              # Create a "scratch variable" to keep track of the running total
 
  for num in list        # For each number in the list
    total = total + num  # Set the new value of "total" to the old value of "total" plus "num"
  end

  total                  # returns "total" — Ruby doesn't need the "return" statement
end

Where do you guys go to start when trying to find a method? Is there a method I should be following when I do not know where to start? I checked the technical documentation but didn't find it there. I will admit I am not comfortable just yet with reading/comprehending all of it.

I then googled 'each loop running total ruby' and looked around stack overflow which had a link to enumerables technical docs which then lead to some experimenting and possible success.

The important thing is first understanding what you want to do and why you want to do it, as precisely as possible. For each bit of work you want to do, you need to be able to break it down into smaller tasks that you can repeat, combine, and so on. Eventually you'll know how do to the smaller tasks.

When in doubt, imagine a real life scenario that would force you to "act like a computer." If I have you a list of 10,000 numbers, how would you add them up? You'd likely keep a separate sheet of paper with the running total and then add the current number on the list to the running total as you went down the list. Assuming you made no clerical errors, e.g., skipping a number, misreading a number, adding two numbers in correctly, etc., then you know whatever is written down as your "running total" will be the final total once you're through the entire list.

Especially when you're just starting, getting "the answer" is much, much, much, much less important than understanding the countours of the problem. The countours will remain forever, but "the answer" is much more situational.

For example, yes, you want to "keep a running total." There may or may not be a built-in Ruby method for this. When you're starting out it's honestly better to try to reason through how you'd build this yourself and then try to actually build it rather than finding a pre-built "recipe."

Don't worry about the fact that you can't understand the technical documentation right off the bat. Being able to read it is a learned skill — one we'll teach you — but it begins by being comfortable looking at it. What parts feel familiar and comfortable? What parts feel alien and uncomfortable?

A good rule of thumb is that you souldn't be copying & pasting code unless you understand exactly what it does and have a decent idea of how it does it. Otherwise, when something goes wrong — and it will — you have no hope of fixing it beyond trying to find a new "recipe" that "doesn't break."

Of course, how do you know the new recipe "doesn't break" if you don't understand how the original recipe broke? Just because the canary in the coal mine has stopped chirping doesn't mean there isn't still deadly gas.

def sum(list)
total = 0 # Create a "scratch variable" to keep track of the running total
for num in list # For each numner in the list
total = total + num # Set the new value of "total" to the old value of "total" plus "num"
end
total # returns "total" — Ruby doesn't need the "return" statement
end
# Instead of total = total + num we could write
# total += num. This is just shorthand — they mean the same thing.
p sum([1])
p sum([0])
p sum([-1])
p sum([1, -1])
p sum([0, 10, 0, 20])
p sum([-111, -111, -111])
p sum([11,22,33])
# Here are other implementations of the sum method. See if you can understand them all.
def sum(list)
total = 0
list.each do |num|
total += num
end
total
end
# The following methods all use inject. They're 100% equivalent.
# Notice that we don't need "total += num", only "total + num"
# See if you can understand why
#
# Keep in mind that inject is a pretty advanced method. Even
# most intermediate developers don't really understand it well.
def sum(list)
list.inject(0) do |total, num|
total + num
end
end
def sum(list)
list.inject do |total, num|
total + num
end
end
def sum(list)
list.inject { |total, num| total + num } # We can use { ... } instead of do ... end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment