Algorithms with lists
A list is an ordered data structure. An algorithm specifies finite steps for processing its entries. Build, trace, test and explain the implementation
- Positions and values State whether positions start at 0 or 1. This lesson starts at 1. L[i] is the entry stored at position i.
- Assignment Evaluate the right side using current values, then store its result on the left. Updating one entry does not change every entry
- Count, sum or filter Add 1 to count, add x to sum values, or append x to an output list. A condition selects which entries contribute
- Preserve required data Lists retain order and repeats. Filtering alone does not sort or remove duplicates
- Initialise and handle empty input Initialise an accumulator before the loop. Empty input gives zero iterations. A maximum requires a non-empty list or a separate empty-input response
- Tests and justification Use empty, one-entry, repeated, negative and equality-boundary cases. State expected outputs first. Explain what the algorithm has correctly accumulated after each iteration
Worked example
Implement a sum of entries at least 3. State the initial value, condition and update, then trace the output
- \(\displaystyle \text{total}\leftarrow0\)Initialise once before the loop
- \(\displaystyle x\ge3\)Visit each entry in order. Test this condition before updating the total
- \(\displaystyle \text{total}\leftarrow\text{total}+x\)Apply this update only to passing entries. The running totals after each visit are 3, 3, 6 and 14.
- \(\displaystyle \text{output}=3+3+8=14\)The repeated 3 contributes twice; −1 fails the condition
Common mistakes
Difficulty levels
Questions about this topic
Is pseudocode a particular programming language?+
No. It communicates an algorithm precisely without a language-specific syntax. State your conventions, loop boundaries, conditions and output
Does this builder run arbitrary code?+
No. It executes only its bounded list-processing blocks. You can choose a condition, update and initialisation policy and inspect the complete trace
Must a list contain different values?+
No. Repeated values are valid entries. Removing duplicates is a separate operation and can change a count or sum
Does passing every question prove my own implementation?+
No. The questions check particular reasoning. Your own design also needs tests against its specification and an explanation of why each step preserves the intended result
Can I use a calculator?+
No. This topic is practised without a calculator
Which year level and chapter is this?+
Year 10 Mathematics, chapter 3: Algebra and indices