SI100B Midterm Revision Notes
1. Fast-Slow pointer
Definition(quoted fromhttps://www.educative.io/interview-prep/coding/introduction-to-fast-and-slow-pointers)
‘Similar to the two pointers pattern, the fast and slow pointers pattern uses two pointers to traverse an iterable data structure, but at different speeds, often to identify cycles or find a specific target. The speeds of the pointers can be adjusted according to the problem statement. The two pointers pattern focuses on comparing data values, whereas the fast and slow pointers method is typically used to analyze the structure or properties of the data.’
For example, if we have a list called nums ,and we want to implement a function that moves all 0 to the end of the list, we can think of two pointers.
right:(the ‘Fast’ one) This pointer’s job is to scan the entire list.left:(the ‘Slow’ one) You can see this pointer as a mark, which keeps track of the position where the next non-zero element should be inserted.
So the entire code should be:
1 | class Solution: |
2.Reminders when using join() and split() method
Note that the .split() and .join() are BOTH methods of STRING, when using .join(), the iterable object should be passed as a parameter!
DO NOT write a command likeres=list.join('')
CORRECT usage example:
Input:
1 | ls = ('One','Two','Three') |
Output:
1 | OneTwoThree |
3. The sorted() function and .sort() method
BIG IDEA: sorted() is a built-in function in python, while sort() is a method of list.
Syntax
.sort()
1 | list.sort(reverse=False, key=None) # Note that the parameter 'key' takes a function |
Reminder: DO NOT pass this expression to any variable! If you want a new ordered list, use sorted() instead
sorted()
1 | numbers = [5, 2, 9, 1] |
- 标题: SI100B Midterm Revision Notes
- 作者: AlexWei
- 创建于 : 2025-11-13 17:52:00
- 更新于 : 2025-11-13 22:28:23
- 链接: https://www.alexwei.top/2025/11/13/SI100B-midterm-revision-notes/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。