Module 0 · Orientation · Lesson 0.2
The problem template
Meet the problem template: fill in a function's body, return an answer, and let the tests check it. First problem: a trade's notional value.
Hook
What is this trade worth?
A desk buys 100 shares of LABX at 101.25 each. Before the order goes out, the risk system asks one question: how much money is this trade worth? In this lesson you write the code that answers it.
Concept
Filling in a function
From now on, each problem arrives as a function to fill in. A function is a named piece of code that receives some values, works out an answer and hands it back. Here is a small one that works out a broker’s fee of 0.005 per share:
def fee(quantity):
return quantity * 0.005
print(fee(1000))Press Run and it prints 5.0. Read it line by line:
def fee(quantity):names the functionfeeand says it receives one value, calledquantity.return quantity * 0.005works out the answer and hands it back.*is Python’s multiplication sign.fee(1000)calls the function withquantityset to 1000, andprintshows the answer it returns.
Treat def and return as a fixed frame for now: you write the line in the middle. Module 4 explains functions properly.
How the tests work. Each problem has test cases: inputs with known answers. Run calls your function on the examples in the problem and shows what it returned. Submit also runs hidden cases, such as a trade of zero shares. If one fails, it shows you the case’s name, so you know what to think about next.
Worked example
One function, many trades
A function is written once and called as often as you like. The same fee works for any order size:
def fee(quantity):
return quantity * 0.005
print(fee(200))
print(fee(10000))
print(fee(0))It prints 1.0, 50.0 and 0.0. Each call sets quantity to a new value, and the line in the middle works out that order’s fee. The tests call your function in exactly this way, once for each case.
Check
Predict the output
Problem
Trade notional
notional(quantity, price) -> float
A trade’s notional is its total value: the number of shares times the price of one share. Buying 100 shares of LABX at 101.25 is a trade worth 10,125.00.
Fill in notional(quantity, price). It receives the number of shares and the price of one share, and it returns the value of the trade.
| quantity | price | returns |
|---|---|---|
| 100 | 101.25 | 10125.0 |
| 250 | 40.0 | 10000.0 |
Constraints: quantity is a whole number from 0 to 1,000,000; price is a positive number.
Hints
Stuck? Open one hint at a time
Hint 1 · Nudge
Everything you need is already in the two names the function receives: quantity and price.
Hint 2 · Approach
The value of the trade is one multiplication. Python writes “times” as *.
Hint 3 · Pseudo-code
return quantity times priceSolution
How to solve it
Run your code at least once to unlock the solution.
Reference solution
def notional(quantity, price):
return quantity * priceThe whole answer is one line. quantity * price multiplies the two numbers the function receives, and return hands the result back to whoever called notional, which here is the test.
Why does notional(3, 0.1) return 0.30000000000000004 instead of 0.3? A computer stores most decimal fractions as close binary approximations, and the tiny error shows through. The tests accept any answer within a billionth of the expected value, so your code still passes. Lesson 1.2 explains the problem and how money code avoids it.
Complexity: one multiplication, however large the numbers. That is constant time, written O(1); lesson 5.1 explains the notation.
How the desk does it: a trading system computes notional for every order before sending it, and rejects an order whose notional breaks a risk limit. The formula is the same one you just wrote.
Recap
Three things to keep
- A problem arrives as a function to fill in; `return` hands your answer back to the tests.
- `*` multiplies numbers: a trade's notional is its quantity times its price.
- Run checks the visible examples; Submit also runs hidden cases and names the one that fails.