The syntax for creating a class.
What is:
class name:
field1: typehint
field2untildone: type hint
Python's built in sort
What is Timsort
Python’s built-in key-value data structure uses curly braces and colons.
What is a dictionary
This list method adds a single item to the end of the list.
What is append()?
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.
The method that is called when an object is created
What is __init__
This simple search checks each element one by one from the start until it finds the target.
What is linear search?
Time complexity of all stack operations
What is O(1)
For nums = [10, 20, 30, 40], the expression nums[-1] returns this value.
What is 40?
Your favorite boardgame
Not monopoly
The method that is called when you print an object
What is __str__
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?
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)?
nums = [4, 5, 7, 8]
nums2 = [10, 11, 12, 16]
nums.append(nums2)
What is [4, 5, 7, 8, [10, 11, 12, 16]]
What this code prints:
x = 10
def change():
x = 20
return x
print(change(), x)
What is 20 10?
The role of @dataclass
A decorator automatically generates __init__, __repr__, and __eq__ for a class that mainly stores data.
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]
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()?
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]?
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?
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')"?
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)
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
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)
What does this code print?
print(type(5 / 2), 5 // 2, 5 % 2)
print(type(5 / 2), 5 // 2, 5 % 2)