COP3502C - M6 (starting at slide 12), M7, M8, M9

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

what's the output? class Account: >>> interest = 0.02 >>> def __init__(self, account_holder): >>>>>> self.holder = account_holder >>>>>> self.balance = 0 >>> def deposit(self, amount): >>>>>> self.balance = self.balance + amount >>>>>> return self.balance >>> def withdraw(self, amount): >>>>>> if amount > self.balance: >>>>>>>>> return 'Insufficient funds' >>>>>> self.balance = self.balance - amount >>>>>> return self.balance class CheckingAccount(Account): >>> interest = 0.01 >>> withdraw_fee = 1 >>> def withdraw(self, amount): >>>>>> return Account.withdraw(self, amount + self.withdraw_fee) a = Account("Jim") b = CheckingAccount("Tom") print(a.balance) print(b.balance) print(a.deposit(100)) print(b.deposit(100)) print(a.withdraw(10)) print(b.withdraw(10))

0 0 100 100 90 89

QC: What is the output of the following program? class Monster: >>> count = 0 >>> def __init__(self, health): >>>>>> self.health = health >>>>>> self.count += 1 >>> @classmethod >>> def get_count(cls): >>>>>> return cls.count m1 = Monster(11) m2 = Monster(3) print(m2.count, Monster.count)

1 0

QC: What is the output of the following program? def func(n): >>>num = 0 >>>if n == 0: >>>>>>num = 0 >>>elif n == 1: >>>>>>num = 1 >>>else: >>>>>>num = func(n - 1) + func(n - 2) >>>print(num, end=" ") >>>return num print(func(4))

1 0 1 1 2 1 0 1 3

QC: What is the output of the following program? def magic(n): >>>if n == 1: >>>>>>return n >>>return n + magic(n - 1) print(magic(5))

15

QC: Given a list a = [5, 6, 77, 88, 99] and key = 88; How many iterations are done until the element is found using binary search?

2

QC: What is the output of the following program? def magic(n): >>>if n < 10: >>>>>>return n >>>return magic(n // 10) + n % 10 print(magic(24187))

22

QC: What is the output of the following program? If you think this program will generate an error, please list "error" as your output def magic(arr, size, target): >>>if size == 0: >>>>>>return 0 >>>if arr[size - 1] == target: >>>>>>return 1 + magic(arr, size - 1, target) >>>return magic(arr, size - 1, target) arr = [3, 4, 1, 4, 5, 12, 4] target = 4 print(magic(arr, len(arr), target))

3

QC: What is the output of the following program? class Monster: >>> def __init__(self, health): >>>>>> self.health = health >>> @staticmethod >>> def increase_health(monster, health): >>>>>> monster.health += 20 >>>>>> health = 40 m = Monster(10) health = 30 Monster.increase_health(m, health) print(m.health, health)

30 30 (@staticmethod is the same as just outdenting that method, it just makes it independent from the class)

QC: What is the output of the following program? class MyClass: >>> x = 9 >>> def __init__(self, x=2, y=1): >>>>>> self.y = y >>>>>> self.x = x >>> def sum_nums(self): >>>>>> self.y += self.x >>>>>> return self.y m = MyClass(4) print(m.sum_nums())

5

QC: What is the output of the following program? def mystery(b, e): >>>if e == 0: >>>>>>return 1 >>>else: >>>>>>return b * mystery(b, e - 1) print(mystery(4, 3))

64

QC: What is the output of the following program? If you think this program will generate an error, please list "error" as your output class Parent: >>> def __init__(self, x, y=3): >>>>>> self.x = x >>>>>> self.y = y class Child(Parent): >>> def __init__(self, a, b): >>>>>> super().__init__(a) >>>>>> self.b = b c = Child(5, 7) print(c.b, c.x, c.y)

7 5 3

QC: Give the list below, how will the list elements look like after second pass for selection sort? 34, 8, 64, 51, 32, 21

8, 21, 64, 51, 32, 34

QC: Give the list below, how will the list elements look like after second pass for bubble sort? 34, 8, 64, 51, 32, 21

8, 34, 32, 21, 51, 64

QC: Give the list below, how will the list elements look like after second pass for insertion sort? 34, 8, 64, 51, 32, 21

8, 34, 64, 51, 32, 21

Given a list a = [45, 77, 89, 90, 94, 99, 100] and key = 100; What are the mid values (corresponding to list elements) generated in the first and second iterations using binary search?

90 and 99

examples of "is a" relationship between a superclass and an inherited class

A grasshopper "is a" insect. A poodle "is a" dog. A car "is a" vehicle.

what two things must recursive algorithms have in simple words?

Always have a base case and a step that brings you closer to the base case

what type of growth is this Big-O notation? Why? O(1)

Constant growth because no matter the size of the input, the time it takes is the same Ex: a = 1 same as a = 20 and a < 100 etc

type of growth and big-o notation for this example: Deeper nested integrations

Cubic growth, O(n3)

Recurrence Relation purpose

Decompose the task into subtasks until it can fulfill the base case

Fibonacci sequence

Each number is the sum of the two numbers before it

QC: What is the output of the following program? class Circle: >>> pi = 3.14 >>> def __init__(self, radius): >>>>>> self.radius = radius >>> def area(self): >>>>>> return pi * self.radius ** 2 c = Circle(2) print(c.area())

Error (because pi needs to be self.pi or circle.pi to be recognized)

type of growth and big-o notation for this example: Finding the solution to the travelling salesman problem using dynamic programming

Exponential growth, O(an) for some a > 1

type of growth and big-o notation for this example: Finding an item in a list using sequential search

Linear growth, O(n) (Ex: worst case scenario: if there's a million items, sequential search would have to do it a million times)

type of growth and big-o notation for this example: Some faster sorting algorithm (e.g., merge sort)

Log-linear growth, O(n log n)

type of growth and big-o notation for this example: Finding an item in a sorted list using binary search

Logarithmic growth, O(log n) (Ex: worst case scenario: if there's a million items, binary search would have to do just 19 times)

What is the time complexity (in Big-O notation) of the following code snippets? for i in range(0, n): >>>j = 1 >>>>>>while j < m: >>>>>>>>>a, b = 'good', 'job' >>>>>>>>>print(a + b) >>>>>>>>>j = j * 2

O(n * log m)

What is the time complexity (in Big-O notation) of the following code snippet? for i in range(100, 0, -1): >>>for j in range(0, n): >>>>>>print(i + j)

O(n)

how to write a file

Open file in write or append mode . Then write a string to a file using file.write() method

file.open(filename, "w") does what while file.open(filename, "a") does what

Opens file for write (deletes previous contents) Opens files for append (new data goes after previous contents)

type of growth and big-o notation for this example: Some simple sorting algorithm (e.g., bubble sort)

Quadratic growth, O(n2)

file.readlines()

Read file's content as a list of lines

file.read() does what

Read file's entire contents as a string and Each line in the file is ended with "\n"

file.readline() does what

Read next line from file as a string

what is a Superclass

The more general class which holds the info of common features and behaviors

what is a Subclass

The more specialized class that inherits from the superclass

what is a Composition and what type of relationship?

a class that references one or more objects from other classes in instance variables This models a has-a relationship

when writing files, If the file is missing, what happens?

a new file is created

what does A specialized object have

all characteristics of the general object plus additional characteristics that make it special

In object-oriented programming, what is inheritance used to create

an "is a" relationship among classes

Derived Class (Subclass)

any class that inherits attributes and methods from any other derived class or base class

what's the base case? recurrence relation? output? def fibonacci(n): >>>if n == 0: >>>>>>return 0 >>>if n == 1: >>>>>>return 1 >>>return fibonacci(n - 1) + fibonacci(n - 2) print(fibonacci(3))

base case: Fib(0) = 0, Fib(1) = 1 recurrence relation: Fib(n) = Fib(n-1) + Fib(n-2) output: 2 to help visualize, make a tree diagram and go down each branch from left to right. basically you keep going until n = one of the base cases so that you can find out the answers for each recurrence relation

How to reduce redundant code when multiple classes share similar attributes

by defining a base class and then subclasses can inherit from the base class

file.close() method does what

closes the file so no more reads or writes to the file are allowed

How does bubble sort work?

compares two elements at a time from left to right and swapping them to where the larger element is second requires multiple passes until everything is in order

type of growth and big-o notation for this example: Assigning a number to a variable

constant growth, O(1)

how does Linear/Sequential Search work

each element in the sorted or unsorted list is iterated over and if the element is not found, iteration is repeated

getter vs setter in other languages, and what is python's version called?

getter: reads the value of a variable @property def .... >>> return ... setter: updates the value of a variable @property_name.setter def ... >>> ... = ...

What is Big O notation?

in Computer Science, it describes the performance or complexity of an algorithm. It describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm

what happens when an attribute isn't found in a subclass

it goes one level up, up to base class if needed

what is sequential search algorithm

iterating through a sequence of data to find a certain element

built-in open() function does what? file = open(filename)

let's Input information be obtained from a file (within the same directory)

Algorithm Analysis is the process of

measuring efficiency of algorithms

Order of Big O notation

need to know since low-order terms are dropped

what type of algorithm references itself as part of the solution? what are two things they have and describe each of those two things?

recursive 1) Has one or more base cases (can be computed without self-reference) 2) Has one or more recurrence relations (self-referencing computation) (in the example, the output is the sum of the last two numbers after the base cases over and over again)

we can use strip() to....

remove all whitespace from the start and end of a string

How does a binary search work?

requires a sorted array and a target item - if the middle item in the list == the target, its index is returned - if it's less than target, left half of list is eliminated and if it's greater, right half is eliminated - process repeats until target is found

What does palindrome mean

same sequence forwards and backwards like "racecar" and "level"

what are the three types of sort?

selection, bubble, and insertion sort

how does selection sort work?

smallest value in the array is moved to element 0, then the next smallest to element 1, and so on

How does insertion sort work?

sorted region: starts off w first element unsorted region: rest of elements takes each item in the unsorted region and inserts them in the right order in the sorted region (ignore every second line in pic)

Program often writes to a file to...

store data permanently

to Account.deposit(self, amount) is the pythonic way to access shadowed methods of the superclass, but a better way it can be written is

super().deposit(amount)

A ___ statement can be used to open a file, execute a block of statements, and automatically close the file when complete. It creates a ____, which manages the usage of files

with context manager

The file.write() method does what

writes a string to a file


Set pelajaran terkait

Chapter Quizzes (Chapter 4. The Tissue Level of Organization Chapter 5. The Integumentary System, Chapter 6. Osseous Tissue and Bone Structure)

View Set

Chapter 6 Practice Test- Part Two (Principles of Management)

View Set

CIM Course - K01V Windows Operating Systems Chapter 1/5 Getting Started with Windows 10

View Set

Delmars Unit 17 Resistive-Inductive Series Circuits

View Set

Poll Everywhere Questions Exam 1

View Set