CSE 2050 Final Exam

Ace your homework & exams now with Quizwiz!

deque

- a doubly ended queue - acts like both a stack and a queue, storing an ordered collection of items with the ability to add or remove from both the beginning and the end

unit tests

- are supposed to test a specific -This means you will have many tests and you will run them all, every time you change the code

dequeue

- remove and return the next item in First In First Out (FIFO) ordering def dequeue(self): return self._L.pop(0) (This isn't the best way to do it tho)

peek

- return the next item in LIFO ordering. def peek(self): return self._L[-1]

size

- returns the number of items in the stack def __len__(self): reutrn len(self._L)

Red-Green-Refactor

-Red: The tests fail. They better! You haven't written the code yet! -Green: You get the tests to pass by changing the code. -Refactor: You clean up the code, removing duplication.

class

-a data type -object constructor -ex. python has a list class. If I make a list called mylist . Then, mylist is an object of type list .

method

-a function defined in a class -It is a standard convention to use self as the name of the first parameter to a ______. This parameter is object that will generally be operated on by the _______. -When, calling the _______, you don't have to pass a parameter explicitly for self. Instead, the dot notation fills in this parameter for you.

strings

-a sequences of characters and can be used to store texts of all kinds -can concatenate strings to create a new string using the plus sign -can access the individual characters using square brackets and an index

enqueue

-add a new item to the queue def enqueue(self, item): self._L.append(item)

push

-add a new item to the stack def push(self, item): self._L.append(item)

lists

-an ordered sequence of objects (objects don't have to be the same types) -elements are separated by a comma -L.append(newitem) adds newitem to end of list

public interface

-collection of public attributes -what the user of a class should interact with

encapsulation

-combining into a single thing, data and the methods that operate on that data (ex. classes) - the boundary between the inside and the outside of the class, specifying what is visible to the users of a class

object

-has an identity, a type, and a value -cannot change identity and type -can't change type but it might seem like you did -an instance of class

name of a function

-is just the name and it refers to an object -functions can be treated like objects If you run the module directly (i.e. as a script), then the __name__ variable is automatically set to __main__ .defaults to the module name. If the module is being imported, the __name__ defaults to the module name.

composition

-mean "has a" - where one class stores an instance of another class and it allows us to produce more complex objects

polymorphism

-means we can pass any type of object we want to a function -based on duck typing

duck typing

-name comes from an old expression that if something walks like a duck and quacks like a duck, then it is a duck -The idea is that you don't need a type in order to invoke an existing method on an object

ADT

-not a data structure but tells us about data structure -answers two main questions: 1. What is the data to be stored or represented? 2. What can we do with the data? -may also describe error situations and what should happen if they occur -tells us what methods the data structure will implement

type

-often determines what you can do with the variable type()

control flow

-refers to the commands in a language that affect the order or what operations are executed -for loops, while loops, if statements, try blocks, function calls

pop

-remove and return the next item in Last in First Out (LIFO) ordering def pop(self): return self._L.pop()

isempty

-return True if the stack has no items and return False otherwise def isempty(self): return len(self) == 0

yield

-sometimes used instead of return -the result will be something called a generator and not a function

assert

-statement will raise an error if the predicate that follows it is False. Otherwise, the program just continues as usual. -are much better than just printing because you don't have to manually check to see that it printed what you expected it to print

dictionaries

-store key-value pairs -known as maps, mapping, or hash tables -if you have the key, you can get the value -keys are immutable types b/ you don't wanna change them -We use curly braces to denote them and commas to separate elements -consequential collections -S = {2, 1}

cost

-the ______ of the whole program will be the sum of the _____s of all the operations executed by the program -Often, the _____ will be a function of the input size, rather than a fixed number.

variable

-where information is stored -created by an assignment statement

symmetric differences in a set

A ^ B cost = nA + nB

linked list

A linear data structure, much like an array, that consists of nodes, where each node contains data as well as a link to the next node, but that does not use contiguous memory.

doubly linked list

A linked list in which each node is linked to both its successor and its predecessor

union in a set

A | B cost = nA + nB

add a new item to a set

A.add(newitem) cost = 1

delete an item in a set

A.delete(item) cost = 1

semantic

function behavior

expressions

get evaluated and produce a value

big-O

gives a very convenient way of grouping this (running time) functions into classes that can easily be compared

abstraction

hiding details (the user of our code does not have to know anything about our implementation in order to use the class)

insert at index i

insert(i, newitem) cost = n - i

atomic types

integers, floats, booleans

membership testing

item in L cost = n

(key) membership testing for dictionary

key in D cost = 1

if __name__ == '__main__'

makes sure that the tests will not run when the module is imported from somewhere else

inheritance

means 'is a'

magic/dunder methods

name for methods that start and end with two underscores

mutable

object's value can be cahnged

immutable

object's value cannot be changed

order of precedence

order of operations

sequence

performing operations one at a time in a specified order

object oriented programming

primary goal is to make it possible to write code that is close to the way you think about the things your code represents.

selection

using conditional statements such as if to select which operations to execute

instance

when you make your own object that is like a python class (mylist is a list)

intersection in a set

A & B cost = min{nA, nB}

set differences

A - B cost = nA

features of big-O usage

1. The big-O hides constant factors. Any term that does not depend on the size of the input is considered a constant and will be suppressed in the big-O. 2. The big-O tells us about what will eventually be true when the input is sufficiently large.

private

Any attribute that starts with an underscore is considered _________

get item in dictionary

D[key] cost = 1

set item in dictionary

D[key] = value cost = 1

DRY

Don't Repeat Yourself

Binary Search

Iterative: def bs(L, item): left, right = 0, len(L) while right - left > 1: median = (right + left) // 2 if item < L[median]: right = median else: left = median return right > left and L[left] == item Recursive: def bs(L, item): if len(L) == 0: return False median = len(L) // 2 if item == L[median]: return True elif item < L[median]: return bs(L[:median], item) else: return bs(L[median + 1:], item)

append

L.append(newitem) cost = 1

pop (from end of list)

L.pop() cost = 1

pop (from index i)

L.pop(i) cost = n - i

sort

L.sort() cost = nlogbase2n

concatenate two lists

L1 + L2 cost = n1 + n2

slice

L[a:b} cost = b - a

index access

L[i] cost = 1

index assignment

L[i] = newvalue cost = 1

n Log n

O (n log n)

constant functions

O(1)

exponential functions

O(2^n)

logarithmic functions

O(log n)

factorial functions

O(n!)

linear functions

O(n)

linear time

O(n) ex. 2n + 3

quadratic functions

O(n^2)

quadratic time

O(n^2) ex. 3n^2 + 2

polynomial functions

O(n^k)

TDD

Test-Driven Development -you can write the tests before you write the code -Red, Green, Refactor = three phases of testing process

interface

The classes have public methods. We call these public methods the ___________ to the class.

factoring out a superclass

The process of removing duplication by putting common code into a superclass

Recursion

When a function calls itself. Use a base case to begin execution after evaluation

initalizer

__init__ method

subclass

a class defined in another class (ex baby_class(parent_class):)

data structure

an implementation of ADT (aka concrete data structure)

Mergesort

def merge(L1, L2, L): n = len(L1) i, j = 0, 0 while i < n and j < n: if j == n or (i < len(L1) and L1[i] < L2[j]): L[i + j] = L1[i] i += 1 else: L[i + j] = L2[j] j += 1

Quicksort

def quickSort(L, left = 0, right = None): if right is None: right = len(L) if right - left > 1: middle = partition(L, left, right) quickSort(L, left, middle) quickSort(L, middle + 1, right)

Quickselect

def quickselect(L, k, left = 0, right = None): if right is None: right = len(L) pivot = partition(L, left, right) if k <= pivot: return quickselect(L,k,left,pivot) elif k == pivot + 1: return L[pivot] else: return quickselect(L,k,pivot+1, right)

delete an item by its key for dictionaries

del D[key] cost = 1

delete an item (at index i)

del(item) cost = n - i

The Queue ADT

enqueue, dequeue, len, isempty

The Stack ADT

push, pop, peek, size, isempty

iteration

repeating some operations using loops or recursion

state

stored information

refactoring

the process of cleaning up code, most often referring to the process of removing duplication

method resolution order

the search for the correct function

public

there is no formal mechanism to keep one from accessing attributes of a class from outside that class so everything is ____________


Related study sets

(FON) Unit 9, Chapter 18/23 Study Guide

View Set

Ch. 25: Chinese and Korean Art after 1279

View Set

Econ 320 Lecture 08 Practice Problems

View Set