CSC108H1(Introduction to Computer Programming)和 CSC148H1(Introduction to the Study of Computer Science I)是多伦多大学计算机科学系的两门入门级别核心课程。
- CSC108H:Python 语言基础,面向零编程经验的学生
- CSC148H:在 CSC108H 基础上,深入面向对象编程和基础数据结构
这两门课是 CS 专业必修,也被大量商科、统计、认知科学专业的学生选修。
CSC108H1 课程概况
| 项目 | 内容 |
|---|---|
| 课程代码 | CSC108H1 |
| 先修要求 | 无(真正零基础可以上) |
| 授课语言 | Python 3 |
| 评分方式(通常) | 作业 40% + 期中考试 20% + 期末考试 40% |
| 工具 | Python 3 + Wing IDE 或 VS Code |
CSC108H 核心考点
模块 1:Python 基础语法
必掌握内容:
- 变量类型(
int,float,str,bool) - 条件语句(
if / elif / else) - 循环(
for,while) - 函数定义与调用(
def,return, 参数,默认参数)
常见考题:
# 写一个函数,接收一个字符串,返回其中所有大写字母的个数
def count_uppercase(s: str) -> int:
count = 0
for char in s:
if char.isupper():
count += 1
return count
模块 2:字符串操作
UofT CSC108 的字符串题非常高频:
# 常用字符串方法
s = "Hello, World!"
s.lower() # 'hello, world!'
s.upper() # 'HELLO, WORLD!'
s.strip() # 去首尾空白
s.split(',') # ['Hello', ' World!']
s.replace('o', '0') # 替换
len(s) # 13
s[0:5] # 'Hello'(切片)
s[::-1] # 字符串反转
典型 CSC108 作业题型:给一段文字,统计单词数、找某词出现频率、实现简单密码加密(Caesar cipher)
模块 3:列表和字典
# 列表操作
numbers = [3, 1, 4, 1, 5, 9]
numbers.append(2) # 末尾添加
numbers.sort() # 原地排序
sorted(numbers) # 返回排序后新列表
numbers.pop() # 删除并返回最后一个
# 字典操作
word_count = {}
for word in text.split():
word_count[word] = word_count.get(word, 0) + 1
常见考题:实现词频统计、从列表中筛选满足条件的元素
模块 4:文件操作
CSC108H 通常有文件读写作业:
# 读取文件
with open('data.txt', 'r') as f:
lines = f.readlines() # 读所有行到列表
for line in lines:
line = line.strip() # 去掉换行符
# 写入文件
with open('output.txt', 'w') as f:
f.write('Hello\n')
模块 5:函数设计原则(Type Annotations + Docstrings)
UofT CS 课程要求严格的文档规范:
def get_average(grades: list[float]) -> float:
"""Return the average of the grades in the given list.
Preconditions:
- len(grades) > 0
- all(0 <= g <= 100 for g in grades)
>>> get_average([80.0, 90.0, 70.0])
80.0
"""
return sum(grades) / len(grades)
注意:
- Type Annotations 是 UofT 作业评分标准之一
- Docstring 必须包含
Preconditions和>>>示例 - 函数名必须有意义且遵循
snake_case规范
CSC148H1 课程概况
| 项目 | 内容 |
|---|---|
| 课程代码 | CSC148H1 |
| 先修要求 | CSC108H1(必须先修) |
| 核心主题 | 面向对象编程、递归、链表、树、排序算法 |
| 评分方式(通常) | 作业 35% + 期中 25% + 期末 40% |
CSC148H 核心考点
面向对象编程(OOP)
class Node:
"""A node in a linked list."""
item: int
next: 'Node | None'
def __init__(self, item: int, next: 'Node | None' = None) -> None:
self.item = item
self.next = next
class LinkedList:
"""A singly-linked list."""
_first: 'Node | None'
def __init__(self) -> None:
self._first = None
def append(self, item: int) -> None:
"""Add item to the end of this list."""
new_node = Node(item)
if self._first is None:
self._first = new_node
else:
curr = self._first
while curr.next is not None:
curr = curr.next
curr.next = new_node
考试重点:能手写 LinkedList 的 append、remove、__contains__ 方法
递归(Recursion)
递归是 CSC148 最难的部分,也是期末考试必考内容:
def factorial(n: int) -> int:
"""Return n! for n >= 0."""
if n == 0: # Base case
return 1
else: # Recursive case
return n * factorial(n - 1)
def sum_nested(lst: list) -> int:
"""Return the sum of all integers in a nested list."""
total = 0
for item in lst:
if isinstance(item, int):
total += item
else: # item is a list
total += sum_nested(item) # 递归调用
return total
递归三步法:
- 确定 Base Case(最简单情况)
- 缩小问题规模(使问题向 Base Case 靠近)
- 假设递归调用正确,组合结果
树(Trees)
class Tree:
"""A recursive tree data structure."""
root: int
subtrees: list['Tree']
def __init__(self, root: int, subtrees: list['Tree']) -> None:
self.root = root
self.subtrees = subtrees
def __len__(self) -> int:
"""Return the number of items in this tree."""
if self.subtrees == []:
return 1
else:
return 1 + sum(subtree.__len__() for subtree in self.subtrees)
常考操作:树的深度、节点数量、查找最大值、前/中/后序遍历
排序算法
CSC148H 要求理解以下排序算法的原理和实现:
| 算法 | 时间复杂度 | 空间复杂度 | 稳定性 |
|---|---|---|---|
| Selection Sort | O(n²) | O(1) | 不稳定 |
| Insertion Sort | O(n²) | O(1) | 稳定 |
| Merge Sort | O(n log n) | O(n) | 稳定 |
| Quick Sort | O(n log n) 平均 | O(log n) | 不稳定 |
期末考试通常要求:给定一个数组,手动模拟 Merge Sort 或 Quick Sort 的过程(逐步写出每次迭代的状态)。
UofT CS 作业风格和注意事项
PythonTA 代码检查:UofT 使用 python_ta 库对代码风格进行自动检查,很多作业会在提交时自动运行 PythonTA。常见错误:
- 行长度超过 100 字符
- 变量名不符合
snake_case - 缺少类型注释
- 函数过长(超过 20 行)
运行 PythonTA:
import python_ta
python_ta.check_all(config={
'allowed-import-modules': ['doctest', 'typing'],
'max-line-length': 100
})
Markus 提交系统:UofT CS 用 Markus 平台提交作业,注意提交后等待自动测试结果,如有失败的测试案例要立即排查。
期末备考策略
CSC108H 期末重点:
- 字符串操作和切片(高频)
- 列表推导(list comprehension)
- 文件读写
- 递归基础(CSC108 也会涉及简单递归)
CSC148H 期末重点:
- 递归函数(手写 + 追踪执行过程)
- 链表操作
- 树的遍历和操作
- Merge Sort 手动模拟
做题顺序建议:先把所有题目快速浏览一遍,优先完成有把握的题,最后攻关难题。CSC148 期末时间往往很紧张。
常见问题
CSC108H 比较难吗?
如果完全没有编程经验,最开始会有适应期(大约前3周)。但 UofT 提供很多支持资源:Office Hours、Piazza 讨论区、CS 学习中心。坚持完成每周练习题的话,通过率很高。
我可以用 ChatGPT 完成作业吗?
UofT CS 有 AI 使用政策,且作业有 Academic Integrity Declaration。部分课程明确禁止 AI 辅助写代码,有些允许但要注明。建议以学习为目的自己理解代码逻辑,否则期中期末手写代码时会出现问题。
CSC148H 里哪个部分最难?
大多数学生反映递归和树是最难的部分,需要时间建立"递归思维"。建议在 Office Hours 多问具体的递归题,把思路说出来让 TA 指正。
