Files
2023-10-09 11:15:06 +02:00

56 lines
1.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# amount to change\n",
"amount = 12.35\n",
"\n",
"# available coins in the cash register (do not take into account the number of coins available)\n",
"available_coins = [5,0.5,0.1,2,1,0.2,0.05,0.02,0.01]\n",
"\n",
"# Greedy : function to make change with unsorted coins\n",
"def make_change_unsorted(amount, coins):\n",
" i = 0\n",
" change = []\n",
" while amount > 0 and len(coins) > i:\n",
" # print(str(round(amount//coins[i])) + \" Coins of \" + str(coins[i]) + \"€\")\n",
" for j in range(round(amount//coins[i])):\n",
" change.append(coins[i])\n",
" amount = round(amount%coins[i],2)\n",
" i = i+1\n",
" \n",
" return change\n",
"\n",
"# Greedy : function to make change with sorted coins\n",
"def make_change_sorted(amount, coins):\n",
" coins.sort(reverse=True)\n",
" i = 0\n",
" change = []\n",
"\n",
" while amount > 0 and len(coins) > i:\n",
" # print(str(round(amount//coins[i])) + \" Coins of \" + str(coins[i]) + \"€\")\n",
" for j in range(round(amount//coins[i])):\n",
" change.append(coins[i])\n",
" amount = round(amount%coins[i], 2)\n",
" i = i+1\n",
" if amount > 0:\n",
" print(f\"Cannot make exact change for {amount:.2f}€\")\n",
" return change\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}