|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Money calculation\n", |
| 8 | + "\n", |
| 9 | + "**Difficulty level**: 1 - beginner\n", |
| 10 | + "\n", |
| 11 | + "Imagine you need to create a calculation that, for a given amount of money, it will return the number of each \n", |
| 12 | + "bill and coin you need to reach that amount. The priority order for the bills is from the highest bill to the smallest coin.\n", |
| 13 | + "\n", |
| 14 | + "Assumptions:\n", |
| 15 | + "\n", |
| 16 | + "- Available bills: `100$`, `50$`, `20$`, `10$` and `5$`\n", |
| 17 | + "- Available coins: `1$`, `0.5$`, `0.1$`, `0.05$` and `0.01$`\n", |
| 18 | + "- The largest bill/coin has priority compared to the smallest one.\n", |
| 19 | + "- The return of the function should be a dictionary, where the keys are represented by the nominal value of the \n", |
| 20 | + " bill/coin and the values are represented bty the number of bills/coins needed.\n", |
| 21 | + "- Float is not a precise data format, use `decimal.Decimal` instead.\n", |
| 22 | + " \n", |
| 23 | + "Tips:\n", |
| 24 | + "\n", |
| 25 | + "- Some functions that you will need for this challenge: \n", |
| 26 | + " - `for` loop.\n", |
| 27 | + " - the dictionary `keys` method.\n", |
| 28 | + " - string slicing.\n", |
| 29 | + " - casting from string to decimal using `decimal.Decimal` method.\n", |
| 30 | + " - for a division with no rest use `//`.\n", |
| 31 | + " - cast a decimal or float number to integer using `int` method.\n", |
| 32 | + "\n", |
| 33 | + "Example:\n", |
| 34 | + "\n", |
| 35 | + "```python\n", |
| 36 | + ">>> amount = 87.16\n", |
| 37 | + ">>> get_total_bills_and_coins(amount)\n", |
| 38 | + "{\n", |
| 39 | + " \"100$\": 0, \n", |
| 40 | + " \"50$\": 1,\n", |
| 41 | + " \"20$\": 1,\n", |
| 42 | + " \"10$\": 1,\n", |
| 43 | + " \"5$\": 1,\n", |
| 44 | + " \"1$\": 2,\n", |
| 45 | + " \"0.5$\": 0,\n", |
| 46 | + " \"0.1$\": 1,\n", |
| 47 | + " \"0.05$\": 1,\n", |
| 48 | + " \"0.01$\": 1\n", |
| 49 | + "}\n", |
| 50 | + "```" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "from decimal import Decimal\n", |
| 60 | + "\n", |
| 61 | + "\n", |
| 62 | + "def get_total_bills_and_coins(amount):\n", |
| 63 | + " n_bills = {\n", |
| 64 | + " \"100$\": 0, \n", |
| 65 | + " \"50$\": 0,\n", |
| 66 | + " \"20$\": 0,\n", |
| 67 | + " \"10$\": 0,\n", |
| 68 | + " \"5$\": 0,\n", |
| 69 | + " \"1$\": 0,\n", |
| 70 | + " \"0.5$\": 0,\n", |
| 71 | + " \"0.1$\": 0,\n", |
| 72 | + " \"0.05$\": 0,\n", |
| 73 | + " \"0.01$\": 0\n", |
| 74 | + " }\n", |
| 75 | + " \n", |
| 76 | + " for k in n_bills.keys():\n", |
| 77 | + " # YOUR CODE HERE\n", |
| 78 | + " \n", |
| 79 | + " return n_bills\n", |
| 80 | + " \n", |
| 81 | + " \n", |
| 82 | + "amount = Decimal('87.16')\n", |
| 83 | + "n_bills = get_total_bills_and_coins(amount)\n", |
| 84 | + "\n", |
| 85 | + "expected = {\n", |
| 86 | + " \"100$\": 0, \n", |
| 87 | + " \"50$\": 1,\n", |
| 88 | + " \"20$\": 1,\n", |
| 89 | + " \"10$\": 1,\n", |
| 90 | + " \"5$\": 1,\n", |
| 91 | + " \"1$\": 2,\n", |
| 92 | + " \"0.5$\": 0,\n", |
| 93 | + " \"0.1$\": 1,\n", |
| 94 | + " \"0.05$\": 1,\n", |
| 95 | + " \"0.01$\": 1\n", |
| 96 | + "}\n", |
| 97 | + "\n", |
| 98 | + "assert n_bills == expected" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "markdown", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "## References\n", |
| 106 | + "\n", |
| 107 | + "- https://realpython.com/python-for-loop/\n", |
| 108 | + "- https://realpython.com/python-dicts/\n", |
| 109 | + "- https://realpython.com/python-strings/#string-slicing\n", |
| 110 | + "- https://realpython.com/python-rounding/#the-decimal-class\n", |
| 111 | + "- https://realpython.com/python-operators-expressions/#arithmetic-operators\n", |
| 112 | + "- https://www.peterbe.com/plog/interesting-casting-in-python" |
| 113 | + ] |
| 114 | + } |
| 115 | + ], |
| 116 | + "metadata": { |
| 117 | + "kernelspec": { |
| 118 | + "display_name": "Python 3", |
| 119 | + "language": "python", |
| 120 | + "name": "python3" |
| 121 | + }, |
| 122 | + "language_info": { |
| 123 | + "codemirror_mode": { |
| 124 | + "name": "ipython", |
| 125 | + "version": 3 |
| 126 | + }, |
| 127 | + "file_extension": ".py", |
| 128 | + "mimetype": "text/x-python", |
| 129 | + "name": "python", |
| 130 | + "nbconvert_exporter": "python", |
| 131 | + "pygments_lexer": "ipython3", |
| 132 | + "version": "3.8.5" |
| 133 | + } |
| 134 | + }, |
| 135 | + "nbformat": 4, |
| 136 | + "nbformat_minor": 4 |
| 137 | +} |
0 commit comments