teaching machines

CS 145 Lecture 24 – Accumulation

October 30, 2015 by . Filed under cs145, fall 2015, lectures.

Agenda

Note

We continue our discussion of common patterns we see with loops. Today we visit the accumulation pattern, where a loop iterates through a collection and accumulates values in some variable of larger scope. Accumulations tend to look like this:

initialize soFar
for each item
  integrate item into soFar
end
use soFar to some other end

We’ll look at an example of calculating a linear approximation of a dataset.

Code

Regression.java

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

Haiku

To accumulate:
while the bag is not empty
  eat you some candy