Questions about anything so far?
On the agenda today are several topics to get us going :
All of this should serve to (a) give us some coding warmups, and (b) define some concepts to get us started with the course content, and (c) give you some hints about the homework exercises.
Let's try this one together as a starting point:
The idea here is to look at another "brute force" (i.e. search all possibilities).
Is this a reasonable algorithm (in a hypothetical language)?
find maximum of
loop over all pairs (i,j) of integers up to N
keep (i,j) if i*j is a palindrome in base 10
What are the "primitive operations" that we are allowed to build our algorithms from?
It should be a reflection of the atomic operations that our computer hardware can do, so we need a "model of computation" It's not likely that is_palindrome() is an assembly language op.
Our programming model will be roughly based on a linear memory structure :
address value
------- -----
1000 0
1001 10
1002 3
...
And some sequence of simple instructions (the program):
address
-------
5000 subtract 1 from address 1001
5001 add address 1001 to address 1000
5002 skip next if address 1001 is 0
5003 jump to 5000
5004 continue
And a few fundamental (assembly language) operations which take roughly the same amout of time:
(1) Allocate some memory (i.e. give it a name and let us use it)
(2) Do arithmetic on values.
(3) Move values from one address to another.
(4) Compare values and make choices (i.e. do loops).
But ... in practice we'll just settle for "simple" operations in a language like Python or C, and call it close enough. We'll need to be careful though to see if some language features are simple enough - things like a built-in sort() or even array.append() have enough complexity that we'll want to unpack what's going on if we want to put those things into an algorithm.
See for example wikipedia: algorithm - expressing algorithms.
Next up: what exactly do we mean by a "data structure"? Two things.
First, we want to know what the operations are for a data structure, typically storing data, fetching (or searching for) data, and creating one of these data structures.
This description of "what can we do" is often called the API (Application Programmer's Interface). In Python, we'll use a class and it's methods to define this interface. In C, we'll use functions to create and modify a struct, accomplishing the same thing.
Second, we will want to know how these data structures work on the inside - how to implement them using some programming language. That will be the code that goes inside the methods of the class.
See wikipedia: abstract data type and the first chapter of the runestone pythonds to read about this.
Our first example of a data structure is the "indexed array" - which you have all already used in Intro CS courses; these are just Python's lists ... or at least a subset of the features packed into Python's lists.
These are blocks of memory with values stored sequentially, with an index - an integer - used to jump to one particular value, to get or set it.
In C, and in the way that operating systems typically work, when one of these arrays is first allocated you must define it's size. The array fits within a block of memory. Growing that memory by changing the size of the array may or may not work ... and so operations like "append" are actually not part of the bare-bones indexed array API. (But we will see another data structure soon, the linked list, which does have simple appending, at the cost of not having simple "give me the n'th element").
We'll implement an indexed array in python, with some live coding in class all together. The methods are
array = IndexedArray(size) # create one
array.put(index, value) # put data into it
x = array.get(index) # get data out of it
size = array.size # read a property
Let's do another class exercise:
smallest(array)
function which finds the smallest value in an IndexedArray.Depending on how much time we have and how burned out you folks are, we may repeat some of this in C with some more live coding :
We ended up discussing and working on the first part of the homework, the (a,b,c) Pythagorean triples, and an IndexedArray python class.
The files we worked on (with a few minor edits) are attached.
Here are my more fully fleshed out versions :