All subjects · Computer Science · Unit 4
WCP04 Advanced Practical Programming and Problem-solving
Edexcel IAL Computer Science, Unit 4 (A2 unit, usually Year 13). 28 lessons so far, in the suggested order.
Lessons in WCP04
- Operators and control flow constructs
/ gives a float, // rounds down, % gives the remainder; & | ^ ~ << >> work on bits; match/case with case _ as the default; for for a known count, while for an u - Data types, variables, constants and type conversion
int, float, str, bool; type() checks; int() truncates; int('3.7') fails; only empty values are False; round() halves to even; constants in UPPER_CASE. - One- and two-dimensional data structures
list (mutable, ordered), tuple (immutable), dict (key to value), set (unique, fast membership), str (immutable). grid[r][c]; build 2D lists with a comprehension - Computational thinking, IDEs and debugging tools
Decompose, abstract, spot patterns, write exact steps. Trace tables predict output. Read tracebacks from the last line up; use breakpoints, stepping and watches - Subprograms, modules, libraries and text files
Functions return values, procedures do not; prefer parameters and return values to globals. import modules; math, random, copy, time from the PLS. Files: 'r', ' - Floating-point data and the decimal module
Floats are binary approximations; Decimal('0.1') is exact. quantize to round (default half-even, ROUND_HALF_UP on request); getcontext().prec sets significant f - Regular expressions: finding and validating text
Raw strings; \d \w \s, [sets], * + ? {m,n}, | and groups; search finds the first match, fullmatch validates, findall lists, sub replaces, split splits; Match.gr - Numerical data with NumPy arrays
np.array with a type code; shape, reshape; a[r, c], a[:, c], a[a > x]; arithmetic and comparisons work element by element; sum/mean/std/var with axis=0 (columns - Data analysis with pandas
read_csv into a DataFrame; shape, head, tail, info, describe; Series mean, median, mode, std, value_counts; clean with dropna and duplicated; sort_values; iloc - Implementing a circular queue in a fixed-length list
Fixed-length list plus front, rear and size; move a pointer with (p + 1) % capacity; empty when size == 0, full when size == capacity; check before enqueue and - Implementing a linked list with classes
Node(data, next); list holds head; traverse by following next until None; insert by linking new.next first, then the previous node's next; remove by making the - Hash tables and hash algorithms
hash(key) gives the slot index directly; good hashes are deterministic, fast and even; sum of codes makes anagrams collide, a polynomial hash does not; prime ta - Handling collisions: chaining, linear probing and rehashing
Chaining: each slot holds a list. Linear probing: step to (i + 1) % size; find stops at an empty slot; delete with a tombstone. Rehashing: when the load factor - Representing graphs: adjacency matrix and adjacency list
Undirected, directed, weighted; matrix[i][j] = weight (n2 cells, instant edge test, good for dense graphs); adjacency list as a dict of dicts (memory per edge, - Implementing a binary search tree
Node(data, left, right); smaller values go left, larger go right; insert by walking to an empty reference; find follows the same path; first value is the root a - Recursive tree traversals: pre-order, in-order and post-order
Pre-order: node, left, right. In-order: left, node, right (sorted for a BST). Post-order: left, right, node. Base case: an empty subtree does nothing. - Designing programs: decomposition, modularity and scope
Decompose into focused subprograms; pick fitting types, structures and constructs; keep input, processing and output separate; minimise side effects and globals - Clean code: consistency, readability and controlling behaviour
PEP 8 consistency (4 spaces, snake_case, CapWords, UPPER_CASE, fixed line length); meaningful names, named constants and purposeful comments; guard clauses to r - Testing, debugging, reviewing and refining
Syntax errors stop the program running; run-time errors raise exceptions (NameError, AttributeError, TypeError...); logic errors give wrong results. Test normal - Object-oriented programming: classes, objects and encapsulation
A class is a blueprint; __init__ sets attributes; self is the current object; methods are functions in the class; class attributes are shared. Encapsulation: _p - Inheritance, polymorphism, aggregation, composition and UML
Subclass(Parent) inherits; super().__init__ reuses the parent constructor; overriding replaces a method; polymorphism: the same call runs each object's own meth - Functional programming: pure functions, higher-order functions and comprehensions
Pure functions: same inputs, same output, no side effects; return new values instead of mutating; functions are first-class; higher-order functions take or retu - Lazy evaluation and generators
yield makes a generator; next() runs to the next yield and pauses; StopIteration when finished; generator expressions use round brackets; values are computed on - Implementing a recursive merge sort
Base case: 0 or 1 items. Split at mid = len // 2, merge sort each half, merge by taking the smaller front item; extend with leftovers. O(n log n) always, O(n) e - Implementing a recursive quick sort
Choose a pivot, partition into smaller, equal and larger, recurse on the parts and join. Average O(n log n); worst O(n2) with a bad pivot (such as the first it - Implementing Dijkstra's shortest path algorithm
Distances start at infinity (start 0); repeatedly settle the closest unvisited vertex; relax each neighbour: update if current + weight is smaller and record pr - Implementing run-length encoding
Count each run; on a change store (value, count) and restart; store the final run; decode by repeating; lossless; great for long runs, worse than the original w - The Unit 4 on-screen exam: how it works and how to tackle it
3 hours, 80 marks, 5 compulsory questions, Python 3 in an IDE, no AI or internet, PLS provided; save to the named file and never overwrite originals; tasks rang