Quiz 3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The standard language for working with a database management system is:

SQL

What is the purpose of the primary key?

Uniquely identify a specific row

In the hands on video, we implemented binary search using recursion. Can you implement the same algorithm without recursion?

Yes, we just simply divide the list and compare the middle element until we find the element we are searching or there is no more elements in the sublist

Sort 3, 1, 4, 1, 5, 9, 2, 6 using mergesort.

[3, 1, 4, 1, 5, 9, 2, 6] Length of list = 8 Midpoint = 4 (index in Python list starts at 0) Step 1: Split list in half recursively Step 2: Call merge in every to put together every sublist

What is the output of: >>> print([i+5 for i in [n for n in range(1,4)]])

[6, 7, 8] since: [n for n in range(1,4)] = [1,2,3] [i+5 in [1,2,3]] = [6, 7, 8]

What is the result of the following code: >>> list(map(lambda x , y: x < y,[1,2,3],[4,5,6])) __________ >>> list(filter(lambda x:x>6.5, [125,6.89,45,1,0.5,7.86,4.75,6.5])) __________

[True, True, True] [125, 6.89, 45, 7.86]

Given a list of numbers, write a function called identity that uses list comprehension and produces a copy of the list.>>> identity([1, 2, 3, 4, 5])[1, 2, 3, 4, 5] Given a list of numbers, write a function called square that uses write a list comprehension and produces a list of the squares of each number.>>> squared([1, 2, 3, 4, 5])[1, 4, 9, 16, 25]>>> squared([-2, 2, -10, 10])[4, 4, 100, 100] Given a list of numbers, write a function called positives that uses write a list comprehension and produces a list of only the positive numbers in that list.>>> positives([-2, -1, 0, 1, 2])[1, 2]

[x for x in numList] [x**2 for x in numList] [x for x in numList if x>0]

Base on the following SQL statement: SELECT dep_id, name FROM Departments a) What is the name of the table from which this statement is retrieving data? b) How many columns are used to retrieve the data? View keyboard shortcutsEditViewInsertFormatToolsTable12ptParagraph

a) Departments b) 2, dep_id and name

Write a generator called forever(value) that repeatedly yields the same value forever. def forever(value): """ >>> gen = forever(34) >>> next(gen) 34 >>> next(gen) 34 >>> sum([next(gen) for _ in range(100)]) 3400 """

def forever(value): while True: yield value

Using the forever generator from the previous question, write the trapped generator that yields the items in the sequence until one of them matches trap, in which case that value should be repeated yielded forever. You are not allowed to index into or slice the sequence. def trapped(sequence, trap): """ >>> gen = trapped([0, 1, 2, 3], 2) >>> [next(gen) for _ in range(7)] [0, 1, 2, 2, 2, 2, 2] >>> list(trapped(range(5), 7)) [0, 1, 2, 3, 4] """

def trapped(sequence, trap): for item in sequence: if item == trap: for i in forever(trap): yield i else: yield item

Using Big-O notation, indicate the time requirement of each of the following tasks in the worst case: 1. Computing the sum of the first n even integers by using a for loop 2. Displaying all n items in a sorted linked list 3. Displaying the last element in a linked list 4. Searching an array of n items for a particular value by using a linear search 5. Searching an array of n items for a particular value by using a binary search 5. Adding an item to a stack of n items

1. O(n) 2. O(n) 3. O(n), since the entire list must be traversed first 4. O(n) since the value might be at the end of the array 5. O(log n), we split log n times 6. O(1) when the stack is implemented as a linked list, we only set the next pointer and update reference to top node

Using Big-O notation, indicate the time requirement of each of the following tasks in the worst case. Describe any assumptions that you make. 1. After arriving at a party, you shake hands with each person there 2. Each person in a room shakes hands with everyone else in the room 3. After entering an elevator, you press a button to choose a floor 4. You ride the elevator from the ground floor up to the nth floor 5. You read a book twice

1. O(n), where n is the number of people at the party, each handshake takes a fixed maximum time, and there is a fixed maximum time between hand shakes. 2. O(n^2), where n is the number of people at the party, each handshake takes a fixed maximum time, and there is a fixed maximum time between hand shakes. 3. O(1), you only push the button once regardless of the floor 4. O(n), where the elevator travels directly to the nth floor before stopping 5. O(n), where n is the number of pages in the book

What is a genetator?

A generator object is an iterator defined as a function that yields its values instead of returning them.When generator functions are called, they return a generator object, which can then be used as an iterator

What is a composite key?

A key that is created by combining two or more existing columns

___________________ contains unique values that are generated by the DBMS

An identity column

What is an iterable?

An object that can be passed to the built-in iter function (An object that can produce iterators)

What is an iterator?

An object that provides sequential access to values one by one. Its contents can be accessed through the built-in next function. When there are no more values available a StopIteration exception is raised when next is called.

Given the list [80, 7, 24, 16, 43, 91, 35, 2, 19, 72], show the contents of the list after each iteration of the outer loop (a complete pass, not every swap) for the indicated in-place algorithm when sorting in ascending order. 1. bubble sort 2. selection sort 3. insertion sort

Bubble Sort: [80, 7, 24, 16, 43, 91, 35, 2, 19, 72] Status of the list after every pass [7, 24, 16, 43, 80, 35, 2, 19, 72, 91] [7, 16, 24, 43, 35, 2, 19, 72, 80, 91] [7, 16, 24, 35, 2, 19, 43, 72, 80, 91] [7, 16, 24, 2, 19, 35, 43, 72, 80, 91] [7, 16, 2, 19, 24, 35, 43, 72, 80, 91] [7, 2, 16, 19, 24, 35, 43, 72, 80, 91] [2, 7, 16, 19, 24, 35, 43, 72, 80, 91] # Sorted, but bubble sort needs another pass with no swaps to conclude it is sorted [2, 7, 16, 19, 24, 35, 43, 72, 80, 91] Selection Sort: [80, 7, 24, 16, 43, 91, 35, 2, 19, 72] Status of the list after every pass [2, 7, 24, 16, 43, 91, 35, 80, 19, 72] [2, 7, 24, 16, 43, 91, 35, 80, 19, 72] [2, 7, 16, 24, 43, 91, 35, 80, 19, 72] [2, 7, 16, 19, 43, 91, 35, 80, 24, 72] [2, 7, 16, 19, 24, 91, 35, 80, 43, 72] [2, 7, 16, 19, 24, 35, 91, 80, 43, 72] [2, 7, 16, 19, 24, 35, 43, 80, 91, 72] [2, 7, 16, 19, 24, 35, 43, 72, 91, 80] [2, 7, 16, 19, 24, 35, 43, 72, 80, 91] Insertion Sort: [80, 7, 24, 16, 43, 91, 35, 2, 19, 72] Status of the list after every pass [7, 80, 24, 16, 43, 91, 35, 2, 19, 72] [7, 24, 80, 16, 43, 91, 35, 2, 19, 72] [7, 16, 24, 80, 43, 91, 35, 2, 19, 72] [7, 16, 24, 43, 80, 91, 35, 2, 19, 72] [7, 16, 24, 43, 80, 91, 35, 2, 19, 72] [7, 16, 24, 35, 43, 80, 91, 2, 19, 72] [2, 7, 16, 24, 35, 43, 80, 91, 19, 72] [2, 7, 16, 19, 24, 35, 43, 80, 91, 72] [2, 7, 16, 19, 24, 35, 43, 72, 80, 91]

What is a database management system (DBMS)?

It is a software that is designed to store, retrieve and manipulate large amounts of data in an organized and efficient manner

in SQLite, what is a cursor? Do you get it before or after connecting to the database?

It is an object that has the ability to access and manipulate the data in a database. We get it after connecting to the database

Suppose that your implementation of a particular algorithm appears in Python as: for i in range(1, n+1): for j in range(n): for k in range(1,10): <some code> This code shows only the repetition in the algorithm, not the computations that occur within the loops. These computations, however, are independent of n. What is the Big-O of the algorithm? Justify your answer

O(n^2). The innermost loop is O(1), since it is independent of n. The second loop is O(n), so the outmost loop is O(n^2).

Write the SQL statement to create a table named Books if it doesn't exist. This table should contain the columns to store the name of the author, the name of the publisher, then number of pages, and the 13 digit ISBN. Then write the SQL statement to delete this table

REATE TABLE IF NOT EXISTS Books ( author TEXT NOT NULL, publisher TEXT NOT NULL, pages INTEGER NOT NULL, isbn TEXT PRIMARY KEY NOT NULL) DROP TABLE IF EXISTS Books

What is the output when the following code is executed? def first(f): def second(x): if x < 0: return -2*(f*f) return f*f return second >>> first(6)(-9)

-72

Consider a list of length n containing positive and negative integers in random order. Write the function order(numList) that rearranges the integers so that the negative integers appear before the positive integers. Your solution should use O(n) operations

#An O(n) solution def order(numList): negIndex = 0 posIndex = len(numList) - 1 for i in range(len(numList)): pos=negIndex if numList[pos] >=0: #Move positive entry into correct portion of the listnumList[pos], numList[posIndex] = numList[posIndex], numList[pos]posIndex -= 1 else: #Move negative entry into correct portion of the listnumList[pos], numList[negIndex] = numList[negIndex], numList[pos] negIndex += 1 return numList

What is the output when the following statement are executed? If the statement raises and error, state the error and how you would fix it. >>> numList = [2, 3, 6, 6, 8, 8, 3] >>> next(numList ) a. ______________________>>> iter(numList )[1] b. ______________________>>> [x for x in iter(numList )] c. ______________________>>> for i in range(len(iter(numList ))): numList .append(2) d. ______________________

a. Error: numList is an iterable not iteratorFix: >>> next(iter(numList ))2 b. Error, you can't use indexing on an iteratorFix: >>> numList [1]3 c. [2, 3, 6, 6, 8, 8, 3] d. Error, you can't call len on an iterator object Fix: >>> for i in range(len(numList )): numList .append(2)

What is the compatible Python data type for the following SQLite data types? a. INTEGER b. REAL c. TEXT d. NULL e. BLOB

a. int b. float c. str d. None e. Any object


Kaugnay na mga set ng pag-aaral

Module 3: Biomolecules and MetabolismWhich of the following is NOT one of the six major elements that make up biological macromolecules? Hydrogen Sulfur Phosphorus Calcium Nitrogen

View Set

Archimedes', Pascal's, and Bernoulli's Principle states...

View Set

PC Pro 4.6.7 Device Driver Installation Facts

View Set

BEFORE 12/19 difference between all the tenses: going to include: PERFECT SUBJUNCTIVE: Present&Past&Future tenses... ALREADY FINISHED present perfect SUBJUNCTIVE&INDICATIVE

View Set

Physics Module 4: Section 1 - Resistance, Resistivity and Ohm's Law

View Set

Unit 2 Test STA 2023 McGraw Hill

View Set