Objective
The Strategy pattern separates “which algorithm I use” from “where I use it”: in Python a dictionary that maps a name to a function is enough. Adding a new strategy does not force you to modify the code that uses them.
Instructions
- The first line has the number
nof lines; each one isprice strategy [parameter]with the strategynone(no discount),percent p(percentagep) orfixed d(fixed discount; the price does not go below 0). - Show
Final price: xwith 2 decimals.
3
100 none
100 percent 15
100 fixed 30
Final price: 100.00
Final price: 85.00
Final price: 70.00
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise