CT: The Master Encyclopedia (Revision & Exam Handbook)

TIP

Use this document for “Day Before Exam” revision. It focuses on high-density information and trap avoidance.


🚨 Exam Trap Alerts

1. The “Off-By-One” Loop

  • Trap: While (i < Length(L)) vs While (i <= Length(L)).
  • Fix: If accessing L[i] and 1-indexed, loop usually goes to Length(L). If iterating rest(L), loop until L is empty.

2. The “Variable Scope” Illusion

  • Trap: Assuming a variable count inside a procedure retains value between calls.
  • Fix: Local variables RESET every call. Only Global variables or Return values persist state.

3. The “List Copy” Mistake

  • Trap: L1 = L2. modifying L1 logic.
  • Fix: In pseudocode, often pass-by-value implies copy, but be careful with nested structures.

4. The “Early Return”

  • Trap: Putting Return inside a loop without a conditional check.
  • Result: Loop runs exactly ONCE and returns.

⚡ Cheat Tables

📝 List Operations Reference

OperationSyntaxReturnsNotes
Headfirst(L)ElementError if L is empty
Tailrest(L)ListReturns List even if 1 element left ([])
Initinit(L)ListAll except last
Lastlast(L)ElementError if L is empty
AppendL ++ [X]ListAdds to END
Prepend[X] ++ LListAdds to START
Membermember(L, x)BooleanLinear Search O(N)

🔑 Dictionary Operations Reference

OperationSyntaxLogic
Check KeyisKey(D, k)MANDATORY before access!
Get Valuev = D[k]Returns value associated with k
Set ValueD[k] = vOverwrites if exists, Creates if new
Get Keyskeys(D)Returns List of keys (Arbitrary order often)

🕸️ Graph Matrix Logic

ConditionMeaning (in Adjacency Matrix M)
M[i][j] == 1Edge exists from i to j
M[i][j] == 0No edge
M[i][j] == M[j][i]Undirected Graph (Symmetric)
Sum(Row i)Out-Degree of node i
Sum(Col j)In-Degree of node j

🧠 Recursion Tracing Template

When verifying recursive code, use the Stack Trace Table:

Call LevelInput LChecking ConditionActionReturn Value
1[A, B, C]Len==0? Nofirst=AA + Sum([B, C])
2[B, C]Len==0? Nofirst=BB + Sum([C])
3[C]Len==0? Nofirst=CC + Sum([])
4[]Len==0? YESBase Case0
Unwind---A + B + C + 0