UAB CS103 Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

If a and b are floats, why is a==b a bad question? (Lecture 6)

Because floats are always an approximation and approximations are never equivalent.

Consider the following assignment statement in Python: x = '5' What is the type of x? (Lecture 13) A) int B) float C) bool D) string E) list

D) string

What is the search problem (give input and output)? (Lecture 12)

INPUT: sequence of n Numbers A=<a0, a1,..., an-1> and variable v OUTPUT: index i such that v=A[i] or none if v is not in A

Write a for loop to test the value for a=0...20. (Lecture 8)

for a in range(0,20): if a==5: return True else: return False

define the getter/setter methods for the member variable foo of the class bar. (Lecture 21)

get(), set() to retrieve data, to assign data to variable (?)

Binary search shrunk the search space by ____________ in each step. (Lecture 12)

half

What is a vector? (Lecture 7)

having magnitude and direction

What is the main purpose of a docstring? (Lecture 8)

help the reader (not computer) better understand the code

How do you express 4√3 in Python interpreter? (Lecture 1)

import math 4*math.sqrt(3) answer: 6.928203230275509

How would you import random? (Lecture 10)

import random

How do you randomly choose a float in (0,1)? (Lecture 9)

import random random.uniform(0,1)

What is aliasing? (Lecture 16)

When a variable is assigned to a variable

When do you use a while loop? (Lecture 12)

When the number of iterations is unknown

Do two vectors in n-space always define an angle? (Lecture 7)

Yes

is x or y True if both x and y are True? (Lecture 5)

Yes

Do you have to add your own whitespace when writing a file? (Lecture 12)

Yes, string.whitespace

push(1), push (2), push(3), pop(), push(4), push(5), pop() what are the contents of the stack? (Lecture 17)

[1, 2, 4]

What is the first line of a class called foo (if it is a base class)? (Lecture 20)

__call__ method (?)

What is a base case? (Lecture 20)

a case where the problem can be solved without further recursion

What is a (univariate) function f(x) in mathematics? (Lecture 1)

a function of a single variable; distance is a function of time

Define time complexity of an algorithm (Lecture 12)

a measure of the number of steps the algorithm will take in the worst case

What is a scalar? Of what is it an abstraction? (Lecture 7)

a point with only magnitude, no direction. a vector.

Define algorithm (Lecture 6)

a sequence of steps to solve a problem

What is Heron's algorithm? (Lecture 6)

a square root algorithm

What is a variable x in mathematics? (Lecture 1)

a value that may change; an abstraction/generalisation

What is a unit vector? (Lecture 7)

a vector of length 1

n!=# of ____ of a list of length n (Lecture 17)

permutations

What are the valid operations on a stack? (Lecture 19)

pop() and push()

How do you print a string, adding a comma rather than a newline? (Lecture 11)

print ('new', end=', ') print ('string')

What is the scalar type of 4.2 (in mathematics/computer science)? (Lecture 2)

real/float

what are two ways of referring to the 'd'? (Lecture 5) s='garden'

s[3] s[-3]

Algorithm for linear search. (Lecture 12)

search from the first element towards the end, until you find the element or reach the end see linear_search.py

What is the first parameter of every method? (Lecture 20)

self

What is the convex hull of a set of points? (Lecture 19)

simple approximation to a shape

in Graham scan, convex hull reduces to ____, which reduces to ____ (Lecture 19)

stack, points (?)

How do you translate a string to lowercase? (Lecture 10)

string.lower()

Where does parenthesis continuation come in useful (even though it violates the general Python principle of command=line)? (Lecture 19)

strings (?)

What constraints must a valid variable name satisfy? (Lecture 4)

•first character must be a letter or _ (but don't use _) •other characters must be a letter, digit, or _ •not a reserved word or built in function (e.g., True, print, for, while, etc.) **Note: same rules apply to function names

What are the standard arithmetic operators (6 of them)?

addition (+), subtraction (-), multiplication (*), division (/), exponentiation (**), modulo (%), floor division (//)

logarithmic algorithm (Lecture 12)

algorithm that takes at most ~ log n steps on an input of size n

What is an exponential algorithm? (Lecture 16)

algorithm that takes ~ 2n steps

Linear Algorithm (Lecture 12)

algorithm that takes ~ n steps on input of size n

What are the 2 key components of a recursive function? (Lecture 17)

base case and recursive call

What is the base case of sum(n)? (Lecture 17)

bc: base case assert n >= 1 if n == 1: ###bc return 1 else: return n + sum (n-1)

What is the base case for string reversal? (Lecture 17)

bc: base case t='' ###bc for c in s: t=c+t return t

What is the base case and recursive call of argmax(L)? (Lecture 17)

bc: base case rc: recursive call assert len(L) > 0 if len(L) == 1: ###bc return 0 else: ###rc imax = argmax (L[:len(L)-1]) if L[-1] > L[imax]: imax = len(L) - 1 return imax

What is the base case of lint(L)? (Lecture 17)

bc: base case rc: recursive call if L == []: ###bc 1 return True elif type(L[0]) != int: ###bc 2 return False else: ###rc return lint(L[1:])

prove: if n is even, n**2 is also even (Lecture 6)

because if n was odd, n=2*k+1

Referring to the 3 methods of string reversal in reverse.py, why do we prefer reverse1? (Lecture 9)

because it is clean, simple code

How can the stack be used to solve for the convex hull of a set of points? (Lecture 19)

because the whole stack is the convex hull (?)

What are the two styles of for loop? (Lecture 9)

by element (L), by index of element (L[0])

What is the purpose of the special method __str__? (Lecture 20)

called when object requires a print statement

define rational number (Lecture 6)

can be expressed as a fraction

What is a child class? (Lecture 20)

case involving object of inheritance

The time complexity of binary search is: (Lecture 13) (A) logarithmic (B) linear (C) quadratic (D)cubic

A) logarithmic

Consider the following code: x = ([5,1], [2,3]) What is the type of x? (Lecture 13) (A) tuple (B) list (C) int (D) A or B (E) A and B

A) tuple

Implement argmin (Lecture 10)

def argmin (L): assert len(L) > 0 imin = 0 for i in range(1,len(L)): if L[i] < L[imin]: imin = i return imin

Implement minimum of an int list using a for loop (min.py). (Lecture 9)

def myMin (L): assert len(L) > 0 m = L[0] for i in range(1,len(L)): if L[i] < m: m = L[i] return m

Write a function to build a list of n floats in (0,1). (Lecture 9)

def randList (n, lo, hi): '''Params: n (float): floats ''' L=[] for i in range(n) L.append(random.unif orm(0,1))** return L **one line

Implement string reversal using a for loop. (Lecture 9)

def reverse (s): t='' for c in s: t=c+t return t

When using string.split() function, what is a token? (Lecture 11)

each word as a string

What is the second purpose of a docstring? (Lecture 8)

establish parameters and returns of a function (?)

What is the inspiration for Fibonacci numbers? (Lecture 17)

exercise in his text 'Book of the Abacus': 'how many pairs of rabbits can be produced from a single pair in a year's time?'

Give a good reason why we use object-oriented programming (Lecture 20)

explain the code even without having to know the underlying behavior

What is type casting and what are two valid reasons to use it? (Lecture 12)

explicitly changing the type of a variable. 1.) variable into a string 2.) variable into float/int

How do you read a file into a single string? (Lecture 11)

f=open('filename.txt', 'r', encoding='utf-8') string=f.read()

How do you open a file? (Lecture 11)

f=open('filename.txt', 'r',. encoding='utf-8')

What is the type of 5/3 (answer: 1.66...)? (Lecture 2)

float

what is the type of 6/3 (answer: 2)? (Lecture 2)

float, because division always results in a float.

what is the type of 5.6+3.4 (answer: 9)? (Lecture 2)

float, because float + float = float, even if the answer is an int.

How many else statements can an if statement have? (Lecture 5)

1

linear search shrunk the search space by ____________ in each step. (Lecture 12)

1

which are strings? (Lecture 5) 'garden' "garden" 'garden"

'garden' and "garden"

Build a random string of length n. (Lecture 10)

(?)

let (v1, v2) and (w1, w2) be vectors in two space, what is their scalar product? (Lecture 7)

(v1*w1)+(v2*w2)

Give some good test data for scalar product. (Lecture 7)

(v1, v2, w1, w2) (2, 5, 10, 4) answer: (2*10)+(5*4)= 40

Let a=5 1.) What is the evaluation order of 1<a<3? 2.) What is the value and type of 1<a? 3.) What is the value of 1<a<3? (Lecture 8)

1.) 1<a then a<3 2.) True, bool (?) 3.) False (?)

Three import methods (Lecture 10)

1.) import math 2.) from turtle import * 3.) import numpy as np

Which operator is applied first? (Lecture 3) 2*3*4

2*3

How do you express 2/3π? Without parentheses? (Lecture 1)

2*π/3

Which operator is applied first in each? (Lecture 3) 2+3+4

2+3

Compute the cube of 234. (Lecture 2)

234^3=12812904

Which operator is applied first? (Lecture 3) 2**3**4

3**4, because exponents work backwards

Which operator is applied first? (Lecture 3) 2+3*4

3*4

how many powers of 10 to span the size difference between quark and universe? (Lecture 16)

40 orders of magnitude

5!=? (Lecture 17)

5*4*3*2*1

Give an example of a problem that would use a vector as data. (Lecture 7)

Distance, length

How does binary search use recursion? (Lecture 16)

Divide and Conquer strategy

Name three useful computations that use scalar product. (Lecture 7)

Euclidean length, euclidean distance, (?)

what is the first problem in graph theory, who introduced it, and in what year? (Lecture 21)

Euler's bridges of Konigsberg, Leonhard Euler, 1735

The list is immutable. (T or F) (Lecture 13)

F

What is Fibonacci's reccurence relation? (Lecture 17)

F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) if n>=2

Algorithm for binary search. (Lecture 12)

If search space is size 1, check this one element. Otherwise probe the middle element - if x is middle elt, return this element - if x < middle element, search in the first half - if x > middle element, search in the second half - have split the search space in half in one step! (logarithmic) see binary_search.py

What is the purpose of the special method __init__ (also called a constructor)? (Lecture 20)

Initialises member variables

What is cloning? How does it solve aliasing? (Lecture 16)

L_clone=L[:] mutate a list while looping over a cloned copy to avoiding effing up the original list

int is an immutable type (T or F) (Lecture 13)

T

How do you autocomplete in the terminal? (Lecture 8)

Tab key

What is the type of 5+3 (answer: 8)? (Lecture 2)

int

What is the type of 5 (in mathematics/computer science)? (Lecture 2)

integer/int

What is the relation of logarithmic and exponential? (Lecture 16)

inverse

What does it mean for a type to be immutable? (Lecture 7)

it cannot be changed once it is set

What is the algorithm style of Heron's algorithm? (Lecture 6)

iterative alogirthm

Explain stacking (push and pop). (Lecture 18)

last-in-first-out (LIFO)

Give Heron's Algorithm (Lecture 6)

make a guess g while guess g is not good enough improve the guess g return g OR g=x/2 while |x-g**2| > e: g=average(g, x/g) return g

What is the Python cosine function? (Lecture 4)

math.cos()

what does a=b mean in mathematics/computer science? (Lecture 2)

math: a is the same value as b comp sci: b is assigned to a

define even number (Lecture 6)

multiple of 2

how many elif statements can an if statement have? (Lecture 5)

multiple, but usually not an excessive amount

What operators does scalar product use? (Lecture 7)

multiplication (*) and addition (+)

What types have we studied are mutable? immutable? (Lecture 7)

mutable: lists immutable: tuples, ints, floats, bools, strings

Double the variable n. (Lecture 3)

n * 2

Increment the variable n. (Lecture 3)

n += 1

Assign the value 10 to n. (Lecture 3)

n=10

What is returned from the open function? (Lecture 11)

nothing unless specified to read (r) or write (w)

What are the acceptable resources for coding? (Lecture 2)

official Python documentation

Define overloading (Lecture 10)

the ability to create multiple functions of the same name with different implementations (multiplication * vs concatenation *)

Define scope (Lecture 10)

the part of a program where a variable binding is visible (within function, within for loop, within if statement)

How can functions and variables change in the context of OO programming? (Lecture 20)

thru methods (?)

What is the precedence of the basic arithmetic operators? (Lecture 1)

when there is more than one arithmetic operator in an expression: multiplication, division, and modulo are calculated first, followed by subtraction and addition. If they have the same level of precedence, it is executed left to right.

How do you introduce a member variable? (Lecture 20)

within the __init__ function in a class

After you have saved Python code in a python script, how do you run it? (Lecture 4)

write code in the editor, run it in the canopy terminal (tools->terminal) in the directory in which the files you are working with are located.

What is a docstring? (Lecture 8)

written instructions for the reader (not computer) to better understand your code

Set y to the quotient of n and x. (Lecture 3)

y=n/x

Give an example of an irrational number (Lecture 6)

π, e, √2, etc.


Ensembles d'études connexes

Alta - Ch. 2 - Descriptive Statistics

View Set

Accounting Final Exam Study Guide

View Set

Statistics True/False with corrections

View Set