An algorithm can be described in words or pseudocode before anyone writes a program. A Python implementation adds the language's types, arithmetic and execution rules. The same high-level procedure can have several implementations, with different speed and numerical behaviour.
A small contract before the steps
Suppose a fictional ledger contains cashflows of USD 30, −10 and 5. Positive amounts are receipts; negative amounts are payments. We want the undiscounted net cashflow, USD 25.
| Part of the contract | What it means here |
|---|---|
| Input | A finite list of signed, whole-dollar USD amounts; no missing values |
| Output | One integer: their net total in USD |
| Empty input | Return zero |
| Side effects | Leave the input list unchanged |
| Financial scope | No currency conversion, discounting or interest accrual |
The procedure is:
- Set a running total to zero.
- Visit each cashflow in the supplied order and add it to the total.
- After the last cashflow, return the total.
Why the procedure is correct
It holds initially because no cashflows have been processed and the total is zero. Adding the next cashflow preserves it. At the end, all cashflows have been processed, so the invariant gives the promised total. Separately, termination follows because each iteration consumes one item from a finite list that the procedure does not extend.
These are two different checks: the invariant explains the answer; the progress argument explains why we reach it. A numerical solver also needs a stopping policy, such as a tolerance plus an iteration limit and an explicit failure result. “Repeat until accurate” leaves essential behaviour unspecified.
One Python implementation
The loop is kept explicit so that its state matches the diagram. This block was executed with Python 3.14.4.
def net_cashflow(cashflows_usd: list[int]) -> int:
"""Sum a finite list of whole-dollar USD cashflows without changing it."""
total_usd = 0
for cashflow_usd in cashflows_usd:
total_usd += cashflow_usd
return total_usd
cashflows_usd = [30, -10, 5]
print(net_cashflow(cashflows_usd))
print(net_cashflow([]))
print(cashflows_usd)
# 25
# 0
# [30, -10, 5]sum(cashflows_usd) also returns 25 here. A shorter call does not imply less computational work, and its internal handling of other numeric types need not match this explicit loop. The official documentation describes sum and its version-specific floating-point changes.Correct, efficient and financially appropriate
Correctness also depends on the question. This algorithm correctly nets same-currency amounts, but it cannot price dated payments: that needs dates and discount factors. Nor does “algorithm” imply a trading strategy or artificial intelligence. A ledger total, a sorting procedure and a trading decision rule are all possible algorithms; each solves a different specified problem.
Sources
- NIST Dictionary of Algorithms and Data Structures: algorithm — the computable-procedure definition.
- Python 3.14 tutorial: for statements — visiting sequence items in order and the hazards of modifying a collection during iteration.
- Python 3.14 built-in functions: sum — summation behaviour and numeric implementation changes. The documentation served Python 3.14.10 when checked; the example above ran on Python 3.14.4.