241
What is a precondition and what is a postcondition?
A precondition indicates the state of an object or the state of an argument before an operation is applied to the object. A postcondition is the state of the object after an operation is applied.
What is the difference between a simple data type and a complex data type?
A simple data type consists of a single value like an integer or floating-point value while a complex data type consists of multiple components. Examples of complex types include containers and objects.
What is an abstract data type?
An ADT is a user-defined data type which specifies a set of data values and a collection of well-defined operations.
What is an array structure?
An array is a common sequence type that is implemented at the hardware level and provided by many programming languages as a built-in type.
When should an array structure be used instead of a list?
An array should be used when the size of the container is known before hand and is fixed. Using an array in such a situation will save memory since the additional space is allocated for the list to allow for quick expansion.
Container
Any data structure or ADT that stores and organizes a collection
Exercise 2.6 Chapter 2
Assuming the Python list is initially created to be twice the specified size: (a) array of arrays. 75×100 + 75 = 7575 list of lists. 75×200 + 150 = 15150 (b) array of arrays. 10, 000×25 + 10, 000 = 260000 list of lists. 10, 000×50 + 20, 000 = 520000 (c) array of arrays. 10, 000×10, 000 + 10, 000 = 100, 010, 000 list of lists. 10, 000×20, 000 + 20, 000 = 200, 020, 000
How does an array differ from Python's list type?
Both an array and the list type are containers, but the array has a fixed size that can not be changed while the list can grow or shrink as needed. In addition, an array is implemented at the hardware level whereas the list is an abstract data type.
What is the difference between row-major order and column-major order.
Both are approaches used to store the elements of a 2-D array within a single 1-D array. The row-major order technique stores each row individually within the 1-D array, in sequential order. While column-major order stores each column individually within the 1-D array, in sequential order.
How does a Set differ from a Bag?
Both are containers that store elements, but there are two major differences between the two ADTs. First, a Set can only store unique elements whereas a Bag can store duplicates. Second, a Bag is a basic container with a limited number of operations, while a Set represents an actual mathematical set.
What is a Bag ADT and how does it differ from Python's list type?
Both the Bag ADT and Python's list are containers that store collections, but the list is also a sequence in which the elements can be accessed by position. There is no specific ordering to the items stored in a Bag ADT.
Exercise 3.10
Given a 2-D array of size m x n, the equation for column-major ordering is index2( i, j ) = j * m + i
What is the difference between the set union and the set intersection operations?
Given two sets, A and B, the set union produces a new set that contains a unique copy of all elements in both A and B. The set intersection, on the other hand, produces a new set that contains only those elements that are in both A and B.
Collection
Group of values with no implied organization or relationship between the individual values
Precondition
Indicates the condition or state of the ADT instance and inputs before the operation can be performed
Postcondition
Indicates the result or ending state of the ADT instance after the operation is performed
Elements
Individual values of the collection
What is information hiding and why is it used?
Information hiding is the concept of hiding the implementation details of an ADT. By hiding the details, it forces you to think about the use of the ADT instead of how it's implemented.
Abstraction
Mechanism for separating the properties of an object and restricting the focus of those relevant in the current context
Mutators
Modify the contents of an ADT instance
Explain why a multi-dimensional array is considered an abstraction.
One-dimensional arrays are implemented at the hardware-level, but arrays of larger dimensions are not. In order to implement multi-dimensional arrays, we must use one or more 1-D arrays. Thus, a multi-dimensional array is not a physical structure, but instead an abstraction that can be used as if it were multi-dimensional.
Data Structure
Physical representation of how data is organized and manipulated
Computer Program
Place which holds algorithms are implemented by translating the step-by-step instructions
Sorted Sequence
Position of the elements based on a prescribed relationship between each element and its successor
Iterators
Process individual data componets sequentially
ADT
Programmer-defined data type that specifies a set of data values and a collection of well-defined operations that can be performed on those values.
Accessors
Return data contained in an instance without modifying it
Information hiding
Separation enforced by requiring interaction with the abstract data type through an interface or defined set of operations
Data Abstraction
Separation of the properties of a data type (its values and operations) from the implementation of that data type
Algorithm
Sequence of clear and precise step-by-step instructions for solving a problem in a finite amount of time
What is the difference between the length and capacity of a list?
The length of a list is the number of items currently stored in the container while its capacity is the number of elements available in the underlying array used to implement the list.
Programming Language
The median where computer programs are constructed
Consider the Student File Reader ADT which extracts student records from a text file. Suppose we want to extract the student records from a text file in which the records are formatted as follows: 10015, John, Smith, 2, 3.01 10334, Jane, Roberts, 4, 3.81 10208, Patrick, Green, 1, 3.95 What changes would be needed to the implementation provided in the chapter?
The only change needed is in the fetchRecord() method. It would have to be changed to extract each record based on the new format.
Why is the array structure a better choice than the list for implementing the 2-D array?
The size of a 2-D array is fixed at the time it is created. By using the array structure, we can allocate the exact number of elements needed to implement the 2-D array. If we were to use a list, additional elements would to allow for future expansion, which would be wasteful since the extra elements would not be needed.
Give an example of procedural abstraction and an example of data abstraction.
The sqrt() function is an example of procedural abstraction while Python's complex type is an example of data abstraction.
Computer Programming
translation process where algorithms are implemented by translating the step-by-step instructions
Type
used to refer to a collection of values
Data Type
used to refer to a given type along with a collection of operations for manipulating values of the given type
Exception
Event that can be triggered and optionally handled during program execution
What are the steps required when a list has to expand to make room for new elements?
(1) A new larger array has to be created; (2) the elements in the original array must be copied to the new array, one element at a time; and (3) the new array replaces the original array used to store the list elements.
Complex Data Types
- Are constructed of multiple components consisting of simple types or other complex types (objects, strings, lists, and dictionaries)
We examined two different approaches for organizing the elements of a 2-D array. What is the advantage of each approach?
A block of memory is allocated for the entire contents of each 1-D array. The array of arrays approach allows for the allocation of smaller blocks for each row. But the single 1-D array has the advantage that the entire contents can be copied to file or transmitted over a network with a single operation.
Define each of the following terms: collection, container, and sequence.
A collection is a group of values with no implied organization or relationship. A container is an abstract data type that stores and organizes a collection with some prescribed relationship between individual elements. A sequence is a container in which the elements are arranged in linear order from front to back, with each element accessible by position within the linear order.
The Map ADT is a container, but is it also a sequence? Explain your answer.
A container is a structure that stores individual elements. A Map is a container since it also stores elements. But it is not a sequence because the elements of a sequence are ordered by position. The elements in a Map can only be accessed by the key component
What is a data structure?
A data structure is the physical representation of how data is organized and manipulated.
What are some of the advantages to defining and using abstract data types?
Allows the programmer to focus on solving the problem at hand instead of getting bogged down in the implementation details. The implementation of the abstract data type can be changed without having to modify the program code that uses the ADT.
User-Defined Types
Construction of additional data types not created by programming language.
List the four categories into which the operations of an ADT can be divided.
Constructors, accessors, mutators, and iterators.
Sequence
Container in which the elements are arranged in linear order from front to back, with each element accessible by position
Empty
Container with no elements
Constructors
Create and initialize new instances of the ADT
Primitives
Data types as part of the language itself which have two categories- simple and complex
Interface
Defined set of operations
Procedural Abstraction
Use of a function or method knowing what it does but ignoring how it's accomplished
Bag
simple container like a shopping bag that can be used to store a collection of items
Exercise 3.6
class Map : # ... def keyArray( self ): theArray = Array( len(self) ) i = 0 for entry in self._entryList : theArray[i] = entry.key i += 1 return theArray
Exercise 3.3
class Set : # ... def isProperSubsetOf( self, otherSet ): return self.isSubsetOf( otherSet ) and self != otherSet
Complex ADT
composed of a collection of data values such as the Python list or dictionary
Simple ADT
composed of a single or several individually named data field such as those used to represent a date or rational number
One-Dimensional Array
composed of multiple sequential element stored in contiguous bytes of memory and allows for random access to the indivual elements
Simple Data Types
consist of values that are in the most basic form and cannot be decomposed into smaller values (Integer and Real types)
Define and implement a function named allPositive(), which accepts a 2-D array of integers and returns a boolean value indicating if the array contains all values >= 0.
def allPositive( table ): for row in range( table.numRows() ): for col in range( table.numCols() ): if table[row,col] < 0 : return False return True
Define and implement a function named rotateLeft( table ) which accepts a 2-D array table and returns a new 2-D array that is the result of rotating the table 90 degrees to the left.
def rotateLeft( table ): newTable = Array2D( table.numCols(), table.numRows() ) for row in range( newTable.numRows() ) : origCol = table.numCols() - row - 1 for col in range( newTable.numCols() ) : origRow = col origElem = table[origRow, origCol] newTable[row,col] = origElem return newTable
Implement the following function which accepts a 1-D array of integer values and computes and returns the sum of the values in the array. Assume an iterator class has not been defined for the Array ADT. def sum( intSeq ):
def sum( intSeq ): total = 0 for i in range( len(intSeq) ): total += intSeq[i] return total
What is a storage class? Give an example of where one would be used.
look up