diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..76c704a 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -99,7 +99,8 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "debits = list(filter(lambda x: x[1] == 'debit', transactions))\n", + "debits" ] }, { @@ -129,7 +130,8 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "sort_descending = sorted(debits, key=lambda x: x[1], reverse=True)\n", + "sort_descending" ] }, { @@ -174,7 +176,9 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "interest_rate = 0.05\n", + "balances_interest = list(map(lambda b: (b * interest_rate) + b, balances))\n", + "balances_interest" ] }, { @@ -214,7 +218,11 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n" + "balances_interest = list(map(\n", + " lambda acc: acc['balance'] * (1 + acc['interest_rate']),\n", + " accounts\n", + "))\n", + "balances_interest\n" ] }, { @@ -250,7 +258,13 @@ "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "interest_rate = 0.05\n", + "balances_interest = list(map(lambda b: (b * interest_rate) + b, balances))\n", + "\n", + "negative_balances = list(filter(lambda x: x < 0, balances_interest))\n", + "total_negative = reduce(lambda acc, x: acc + x, negative_balances, 0)\n", + "\n", + "total_negative" ] }, { @@ -284,7 +298,13 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "def calculate_balance(account):\n", + " total_withdrawals = sum(account['withdrawals'])\n", + " remaining_balance = account['balance'] - total_withdrawals\n", + " return remaining_balance\n", + "\n", + "remaining_balances = list(map(calculate_balance, accounts))\n", + "remaining_balances\n" ] } ],