考前速记
Quick Reference
| Task | Code |
|---|---|
| Declare 1D array (size 10) | Arr = [0] * 10 |
| Declare 2D array (3 × 4) | Arr = [[0] * 4 for i in range(3)] |
| Traverse 1D | for i in range(len(Arr)): |
| Traverse 2D | for r in range(R): for c in range(C): |
| Read input to array | Arr[i] = int(input(...)) |
| Sum all elements | Total = Total + Arr[i] |
| Find max | if Arr[i] > Max: Max = Arr[i] |
| Search | if Arr[i] == SearchValue: |
| Global declaration | global Arr before assignment |
Common Patterns
Fill-and-process template
def main():
global Arr
Arr = [0] * 10
for i in range(10):
Arr[i] = int(input())
# process Arr here
Find average
Total = 0
for i in range(len(Arr)):
Total = Total + Arr[i]
Average = Total / len(Arr)
Red Flags — Check These During Exam
| Red Flag | What to Check |
|---|---|
global used? | If declaring array inside a function, yes |
range(1, N+1) | Off-by-one — should be range(N) |
Arr = [] then Arr[i] = x | Must use append() or pre-allocate |
[0] * N for 2D | Shallow copy bug — use comprehension |
input() without int() | String stored instead of number |
| Loop bounds wrong way round | Rows first, columns second in 2D |
if Arr[i] = x | Should be == not = |
| Array name spelling | Must match exactly what question gives |
Mark Scheme Keywords
备注
M1 = method / structure (loop, function, file open)
A1 = accuracy (correct variable, correct indexing)
B1 = bonus / alternative (type conversion, edge case)
If You Get Stuck
- Write the loop first:
for i in range(N): - Decide what happens inside: input, calculation, comparison
- Check array bounds:
0toN-1 - Check
globalif inside a function - Test with a small example mentally