Classes
Sorting and Searching Algorithms
Data Structures
Lists
Misc
100

The syntax for creating a class.

What is:
class name:
    field1: typehint
    field2untildone: type hint

100

Python's built in sort

What is Timsort

100

Python’s built-in key-value data structure uses curly braces and colons.

What is a dictionary

100

This list method adds a single item to the end of the list.

What is append()?

100

The reason you can't hash a list

Lists are mutable, so their contents can change, and a hash value must never change while the object is in use.

200

The method that is called when an object is created

What is __init__

200

This simple search checks each element one by one from the start until it finds the target.

What is linear search?

200

Time complexity of all stack operations

What is O(1)

200

For nums = [10, 20, 30, 40], the expression nums[-1] returns this value.

What is 40?

200

Your favorite boardgame

Not monopoly 

300

The method that is called when you print an object

What is __str__

300

Given nums = [3, 8, 14, 21, 27, 35, 42, 50, 61], a binary search for the target 35 checks these values, in order.

What are 27, 42 and 35?

300

Calling this list method with no argument removes and returns the top item of a list-based stack.

What is FIFO (first-in, first-out)?

300
The elements inside the list after the code runs


nums = [4, 5, 7, 8]

nums2 = [10, 11, 12, 16]

nums.append(nums2)

What is [4, 5, 7, 8, [10, 11, 12, 16]]

300

What this code prints:

x = 10

def change():
    x = 20
    return x

print(change(), x)

What is 20 10?

400

The role of @dataclass

A decorator automatically generates __init__, __repr__, and __eq__ for a class that mainly stores data.

400

The Pivot, Less, Same, and Greater for this list if being sorted through quicksort: nums = [12, 18, 9, 16, 1, 2, 3, 5, 6, 90, 99, 12, 12, 3, 3, 9, 18]

Pivot: 12

Less: [9, 1, 2, 3, 5, 6, 3, 3, 9]

Same: [12, 12, 12]

Greater: [18, 16, 90, 99, 18]

400

This dictionary method returns a view of all the key-value pairs as tuples, which is handy in a for key, value in d.____(): loop.

What is items()?

400

The output of this code

a = [1, 2, 3]
b = a
c = a[:]

b.append(4)
c.append(5)

print(a, c)

What is [1, 2, 3, 4] [1, 2, 3, 5]?

400

The code below prints this output.

point = (3, 7)
x, y = point
x, y = y, x

coords = {point: "A", (x, y): "B"}

print(len(coords), coords[(7, 3)])

What is 2 B?

500

The result of this code Printing

from dataclasses import dataclass

@dataclass
class Student:
    name: str
    grade = 10

print(Student("Ana"))

What is "Student(name='Ana')"?

500

The best and worst case time complexities for these:

Quicksort

Mergesort

Insertion Sort

Bubble Sort

Linear Search

Binary Search


Quicksort    O(nlogn)    O(n^2)
Mergesort    O(nlogn)    O(nlogn)
Insertion Sort    O(n)    O(n^2)
Bubble Sort    O(n)    O(n^2)
Linear Search    O(1)    O(n)
Binary Search    O(1)    O(logn)

500

Fix this code:

sentence = "the cat and the hat and the bat"

words = sentence.split()

counts = {}


for word in words:

    counts[word] += 1


print(counts)

# Option A: check membership

if word in counts:

    counts[word] += 1

else:

    counts[word] = 1


# Option B: use get with a default

counts[word] = counts.get(word, 0) + 1

500

Fix the code, and give an explaination why it doesnt work

scores = (88, 92, 75)
scores.append(95)
scores[0] = 90
scores.sort()
print(scores)

scores = [88, 92, 75]
scores.append(95)
scores[0] = 90
scores.sort()
print(scores)

500

What does this code print?

print(type(5 / 2), 5 // 2, 5 % 2)

print(type(5 / 2), 5 // 2, 5 % 2)

M
e
n
u