On a retail website, the following discounts apply:
- If the user is an employee of the store, he gets a 30% discount
- If the user is an affiliate of the store, he gets a 10% discount
- If the user has been a customer for over 2 years, he gets a 5% discount.
- For every $100 on the bill, there would be a $ 5 discount (e.g. for $990, you get $ 45 as a discount).
- The percentage based discounts do not apply on groceries.
- A user can get only one of the percentage based discounts on a bill.
This is a simple application that demonstrates how you can code this in Python using Functional Programming.
Simply load up the environment (for development purposes):
guix environment --load=guix.scm
Better yet, you could run a proper container:
guix environment -C --network --load=guix.scm
(assuming you are in a guix container; otherwise use venv!)
To run tests:
python -m unittest discover -v
Running pylint:
pylint *py tests app
Running mypy(type-checker):
mypy .
To spin up the server:
env FLASK_DEBUG=1 FLASK_APP="main.py" flask run --port=8080
IMPORTANT NOTE: I do not recommend using pip tools, use Guix instead
- Prepare your system. You need to make you have python > 3.8, and the ability to install modules.
- Create and enter your virtualenv:
virtualenv --python python3 venv
. venv/bin/activate
- Install the required packages
# The --ignore-installed flag forces packages to # get installed in the venv even if they existed # in the global env pip install -r requirements.txt --ignore-installed
Make sure that the dependencies in the `requirements.txt` file match those in guix. To freeze dependencies:
# Consistent way to ensure you don't capture globally
# installed packages
pip freeze --path venv/lib/python3.8/site-packages > requirements.txt
Port this to:
- [ ] Haskell
- [ ] Guile
- [ ] Rust
as some form as kata since it’s a simple enough exercise.