Python is a _____ typed language
Statically
range(1, 10, 3) includes what numbers?
1, 4, 7
Given the string s="JARED", what do the following equate to:
- s[0:2]
- s[0:-1]
- s[0::2]
- s[::]
- s[::-1]
- JA
- JARE
- JRD
- JARED
- DERAJ
Can binary search be done on any list?
What would a data class for your SI Leader look like?
(potential answer)
@dataclass
class SI_Leader:
name: str
age: int
major: str
What is the result of 10/3? What is the result of 10//3?
- 3.33
- 3
What does a base case do?
It stops the recursion from continuing the work
- Set: a collection of unordered, unindexed, unique values
- Dictionary: a collection of unordered, unique (key, value) pairs
Bonus Points: What are the access time complexities of both?
What are the time complexities of binary search and linear search?
Binary: O(logn)
Linear: O(n)
What method removes all leading and trailing whitespace of a string? What method can split a string into a list of strings?
- strip()
- split()
What is the result of 5*2? What is the result of 5**2?
- 10
- 25
Write a function, using a loop, to sum all the numbers between 1-n
def sum(n :int) -> int:
total = 0
for num in range (1,n):
total+= num
return total
Define a tuple and list in Python
Tuple: a collection of ordered and immutable values
List: a collection of ordered and mutable values
Both are heterogeneous (able to store varied types)
What are the time complexities of the following in regards to a list:
- creating a new list
- appending
- pop() and insert()
- O(n)
- O(1)
- O(1)
What are the 2 different ways of handling collisions in a hash table? What's the difference between the 2?
- Open Addressing: if there's a collision, the value is stored in the next available space in the hash table
- Chaining: if there is a collision, the value is stored in a list stored at the same index where the collision occurred
What denotes the blocking of code in Python?
White space
Write a recursive function to sum all the numbers between 1-n
def sum_rec(n:int)->int:
if n == 1:
return 1
return n + sum_rec(n-1) # will include n; to exclude n, do n-1
- x = len(s)
- y = s[-len(s)]
- s[2] = 'P'
- x = 5
- y = D
- ERROR/ILLEGAL, strings are immutable!
What are the best, avg, and worst time complexities of
- selection sort
- insertion sort
Selection Sort: Best, Avg, Worst - O(n^2)
Insertion Sort: Best - O(n); Avg, Worst - O(n^2)
How would you read from a file called "exam_review.txt"? How would you write to the same file?
with open("exam_review.txt", "r") as file:
content = file.read()
file.write("Hello there")
(if we didn't use 'with open', it would look like)
file = open("exam_review.txt")
content = file.read()
file.write("Hello there")
file.close()
What symbols can be used to create valid variable names?
Numbers, characters, _
Write the substitution trace for the following recursive function:
def x2 (n:int)->int:
if n == 0:
return 0
return 2 * x2(n-1)
print( x2(3) )
x2 (3) = 2 * x2(2)
= 2 * (2 * x2(1) )
= 2 * (2 * (2 * x2(0) ) )
= 2 * (2 * (2 * 0) )
= 2 * (2 * 0)
= 2 * 0
= 0
Given the dictionary, bag = {'apple': 6, 'banana': 7, 'lime': 2}, what do the following equate to:
- bag['apple']
- bag.keys()
- 'lime' in bag
- 3 in bag
- bag.values()
- 6
- apple, banana, lime
- True
- False
- 6, 7, 3
What are the best, avg, and worst time complexities of
- mergesort
- quicksort
Mergesort: O(nlogn)
Quicksort: Best and Avg - O(nlogn); Worst - O(n^2)
When do we rehash? What does rehashing mean?
When the load factor exceeds a certain threshold. We expand the size of the table and then hash the values already in the hash table again (rehash) and put them in the new table. We rehash to reduce the number of collisions