Exam 1 Review
Study Topics
This is just a list of topics to help guide your studying. The list is not in order of importance nor should it be considered a complete list of all topics you should know.
Computational thinking and modeling
- Understanding and articulating the problem
- Identifying inputs and their types
- Specifying outputs and their relations to inputs
- Designing algorithm
- Problem decomposition
- Breaking problem into smaller pieces -- benefits?
- Implementing algorithm
- Representing mathematical models as functions — why?
- Progressively decomposing into smaller and smaller functions — why?
- Representing processes as conditionals and loops — why?
- Representing data — techniques? (Numbers, strings, characters, lists, dictionaries, Booleans, etc)
- Representing mathematical models as functions — why?
Kinds of data
- Numbers
- Integer
- Floating point
- Booleans
- Strings
- str()
- Lists
- Tuples
- Dictionaries
Math operations
- Operators such as multiplication
- Functions such as pow() and abs()
- float()
List operations
- Indexing
- Syntax
- Index range
- Negative indices
- Slicing
- Bounded on one end only
- Bounded on both ends
- Length of a list
- Lists of lists
- Appending to and inserting into a list
- Strings as lists of characters
- Using range() to generate indices
Dictionaries
- Keys and values
- What kinds of data can be keys or values
- keys(), values(), items()
- Syntax for creating, accessing, and updating
Functions
- Defining a function
- Return from a function, with and without a return value
- Return single or multiple values
- Returning vs. printing
- input parameters
- local variables
- Documentation strings and comments
Loops
- for-loop syntax
- looping through elements of a list, string, or dictionary
- using range() to loop over indices
- returning from function within a loop
Conditionals
if-elif-else
statements- Boolean comparison operations
Misc
- importing python files (modules)
- proper indentation
- global variables vs. local variables
- mutation
- Lists vs tuples
- How and why time steps improved the accuracy of the predator-prey code
- Plotting
- Using CodeSkulptor and its documentation
Sample problems
The following problems are typical of those that will be on the exam. In addition to answering these questions, you should also review the course's online notes and the solutions to the daily exercises and assignments.
The following is a formula for computing compound interest: a = p(1+r/n)n·t, where,
- p = principal amount (initial investment),
- r = annual nominal interest rate (e.g., 10% is represented as 0.1),
- n = number of times the interest is compounded per year,
- t = number of years, and
- a = amount after time t.
-
def compounded_amount(p, r, n, t): """Given the principal amount p, annual interest rate r, number of compounding periods per year n, and number of years t, return the investment amount with compounded interest.""" return p * (1 + r/n) ** (n*t)
Complete the following function that takes a list of numbers
[x0, …]
and a number n.
It returns a list that is
[x0n, …]
.
-
def map_exponent(a_list, exponent): """Given a_list=[x0,...] and a number exponent, returns [x0^exponent,...].""" ### You need to complete this function. ###
-
def map_exponent(a_list, exponent): """Given a_list=[x0,...] and a number exponent, returns [x0^exponent,...].""" result = [] for x in a_list: result.append(x ** exponent) return result
Complete the following function that returns the number of positive elements in a list.
-
def countPositives(aList): """Returns the number of positive elements in aList.""" ### You need to complete this function. ###
-
def countPositives(aList): """Returns the number of positive elements in aList.""" count = 0 for x in aList: if x>0: count = count+1 return count
Complete the following function that takes a list of pairs of symbols, representing a directional relationships from the first element to the second, and returns a dictionary that maps all the first elements of the pairs to a list of
all the second elements of the pairs. Technically, these sorts
of relationships constitute something called a "graph",
where each value is called a "node" and each directional relationship (the pair)
from one node to another node is called and "edge".
Thus make_graph([('a', 'b'), ('b', 'c'), ('c', 'a'), ('a', 'c'), ('c', 'c')])
returns
{'a': ['b', 'c'], 'b': ['c'], 'c': ['a', 'c']}
-
def make_graph(edge_list): """ Returns a dictionary representation of a graph from a list of pairs representation of a graph. """
-
def make_graph(edge_list): """ Returns a dictionary representation of a graph from a list of pairs representation of a graph. """ result_dict = {} for edge in edge_list: if edge[0] in result_dict: result_dict[edge[0]].append(edge[1]) else: result_dict[edge[0]] = [edge[1]] return result_dict
Why do we use documentation strings, and what should be described in them?
-
Documentation strings describe what the function does. Thiss should include describing its inputs, its outputs, and how these are related. It should also describe any other behavior like printed output and errors, and when these occur. We should always include documentation strings because they remind ourselves and tell others what this function does (or at least should do). A primary benefit is that this allows others to know whether a given function is of interest to them without reading the function's code.