Skip to content

Commit 30387a4

Browse files
author
Lyuboslav Karev
committed
Add solution for final task
1 parent 4e2812e commit 30387a4

1 file changed

Lines changed: 40 additions & 78 deletions

File tree

‎workshops/workshop2/workshop2_solutions.ipynb‎

Lines changed: 40 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,9 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": 17,
25+
"execution_count": null,
2626
"metadata": {},
27-
"outputs": [
28-
{
29-
"name": "stdout",
30-
"output_type": "stream",
31-
"text": [
32-
"3\n",
33-
"1\n",
34-
"4\n",
35-
"4\n",
36-
"4\n",
37-
"add.hits=2, add.misses=2\n",
38-
"add.__name__='add', subtract.__name__='subtract'\n"
39-
]
40-
},
41-
{
42-
"data": {
43-
"text/plain": [
44-
"'✅ All OK!'"
45-
]
46-
},
47-
"execution_count": 17,
48-
"metadata": {},
49-
"output_type": "execute_result"
50-
}
51-
],
27+
"outputs": [],
5228
"source": [
5329
"def memoize(func):\n",
5430
"\n",
@@ -147,20 +123,9 @@
147123
},
148124
{
149125
"cell_type": "code",
150-
"execution_count": 75,
126+
"execution_count": null,
151127
"metadata": {},
152-
"outputs": [
153-
{
154-
"data": {
155-
"text/plain": [
156-
"'✅ All OK!'"
157-
]
158-
},
159-
"execution_count": 75,
160-
"metadata": {},
161-
"output_type": "execute_result"
162-
}
163-
],
128+
"outputs": [],
164129
"source": [
165130
"class Query:\n",
166131
" def __init__(self, iterable):\n",
@@ -306,20 +271,9 @@
306271
},
307272
{
308273
"cell_type": "code",
309-
"execution_count": 155,
274+
"execution_count": null,
310275
"metadata": {},
311-
"outputs": [
312-
{
313-
"data": {
314-
"text/plain": [
315-
"'✅ All OK!'"
316-
]
317-
},
318-
"execution_count": 155,
319-
"metadata": {},
320-
"output_type": "execute_result"
321-
}
322-
],
276+
"outputs": [],
323277
"source": [
324278
"def evaluate(expr, env):\n",
325279
" match expr:\n",
@@ -484,28 +438,9 @@
484438
},
485439
{
486440
"cell_type": "code",
487-
"execution_count": 162,
441+
"execution_count": null,
488442
"metadata": {},
489-
"outputs": [
490-
{
491-
"name": "stdout",
492-
"output_type": "stream",
493-
"text": [
494-
"[('tea', 9), ('coffee', 7)]\n"
495-
]
496-
},
497-
{
498-
"ename": "AssertionError",
499-
"evalue": "",
500-
"output_type": "error",
501-
"traceback": [
502-
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
503-
"\u001b[31mAssertionError\u001b[39m Traceback (most recent call last)",
504-
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[162]\u001b[39m\u001b[32m, line 57\u001b[39m\n\u001b[32m 53\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m top_products(orders, \u001b[32m2\u001b[39m) == [(\u001b[33m\"tea\"\u001b[39m, \u001b[32m9\u001b[39m), (\u001b[33m\"coffee\"\u001b[39m, \u001b[32m7\u001b[39m)]\n\u001b[32m 54\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m top_products(orders, \u001b[32m10\u001b[39m) == [(\u001b[33m\"tea\"\u001b[39m, \u001b[32m9\u001b[39m), (\u001b[33m\"coffee\"\u001b[39m, \u001b[32m7\u001b[39m), (\u001b[33m\"cake\"\u001b[39m, \u001b[32m3\u001b[39m)]\n\u001b[32m 55\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m top_products(orders + [Order(\u001b[33m\"ana\"\u001b[39m, \u001b[33m\"beer\"\u001b[39m, \u001b[32m7\u001b[39m, \u001b[32m2.0\u001b[39m, \u001b[32m3\u001b[39m)], \u001b[32m3\u001b[39m) == [(\u001b[33m\"tea\"\u001b[39m, \u001b[32m9\u001b[39m), (\u001b[33m\"beer\"\u001b[39m, \u001b[32m7\u001b[39m), (\u001b[33m\"coffee\"\u001b[39m, \u001b[32m7\u001b[39m)]\n\u001b[32m 56\u001b[39m \n\u001b[32m---> \u001b[39m\u001b[32m57\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m loyal_customers(orders) == [\u001b[33m\"ana\"\u001b[39m, \u001b[33m\"bob\"\u001b[39m]\n\u001b[32m 58\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m loyal_customers(orders[:\u001b[32m2\u001b[39m]) == [\u001b[33m\"ana\"\u001b[39m, \u001b[33m\"bob\"\u001b[39m]\n\u001b[32m 59\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m loyal_customers([]) == []\n\u001b[32m 60\u001b[39m \n",
505-
"\u001b[31mAssertionError\u001b[39m: "
506-
]
507-
}
508-
],
443+
"outputs": [],
509444
"source": [
510445
"from collections import namedtuple\n",
511446
"from functools import reduce\n",
@@ -521,7 +456,6 @@
521456
"\n",
522457
"\n",
523458
"def top_products(orders, n):\n",
524-
"\n",
525459
" products = [(order.product, order.quantity) for order in orders]\n",
526460
" names = {name for name, _ in products}\n",
527461
"\n",
@@ -531,15 +465,44 @@
531465
"\n",
532466
"\n",
533467
"def loyal_customers(orders):\n",
534-
" pass # your code here\n",
468+
" months = {order.month for order in orders}\n",
469+
" customers = {order.customer for order in orders}\n",
470+
" customer_and_month = {(order.customer, order.month) for order in orders} # Премахваме повторенията\n",
471+
" customer_to_order = {customer: orders for customer in customers if (orders:=sum(1 for c, m in customer_and_month if c == customer)) == len(months)}\n",
472+
" sorted_customers_to_order = sorted(customer_to_order.items(), key=lambda x: (-x[1], x[0]))\n",
473+
"\n",
474+
" return [item[0] for item in sorted_customers_to_order]\n",
535475
"\n",
536476
"\n",
537477
"def monthly_growth(orders):\n",
538-
" pass # your code here\n",
478+
" months = {order.month for order in orders}\n",
479+
"\n",
480+
" orders_for_month = {month: sum(order.price * order.quantity for order in orders if order.month == month) for month in months}\n",
481+
"\n",
482+
" def _get_diffs(orders_per_month, current_month):\n",
483+
" if current_month - 1 not in orders_per_month:\n",
484+
" return None\n",
485+
"\n",
486+
" start = orders_per_month[current_month - 1]\n",
487+
" final = orders_per_month[current_month]\n",
488+
"\n",
489+
" return round(((final-start)/abs(start)) * 100, 1)\n",
490+
" \n",
491+
" return sorted([(month, orders_for_month[month], _get_diffs(orders_for_month, month)) for month in months], key=lambda x: x[0])\n",
539492
"\n",
540493
"\n",
541494
"def best_month_per_product(orders):\n",
542-
" pass # your code here\n",
495+
" months = {order.month for order in orders}\n",
496+
" products = {order.product for order in orders}\n",
497+
"\n",
498+
" def _get_months_for_product(product):\n",
499+
" orders_for_month = {month: sum(order.quantity for order in orders if order.product == product and order.month == month) for month in months}\n",
500+
"\n",
501+
"\n",
502+
" sorted_orders_for_month = sorted(orders_for_month.items(), key=lambda x: (-x[1], x[0]))\n",
503+
" return sorted_orders_for_month[0][0]\n",
504+
"\n",
505+
" return {product: _get_months_for_product(product) for product in products}\n",
543506
"\n",
544507
"\n",
545508
"# Tests:\n",
@@ -558,7 +521,6 @@
558521
"assert revenue_by_customer(orders) == {\"ana\": 33.0, \"bob\": 30.0, \"cvetan\": 23.0}\n",
559522
"assert revenue_by_customer([]) == {}\n",
560523
"\n",
561-
"print(top_products(orders, 2))\n",
562524
"assert top_products(orders, 2) == [(\"tea\", 9), (\"coffee\", 7)]\n",
563525
"assert top_products(orders, 10) == [(\"tea\", 9), (\"coffee\", 7), (\"cake\", 3)]\n",
564526
"assert top_products(orders + [Order(\"ana\", \"beer\", 7, 2.0, 3)], 3) == [(\"tea\", 9), (\"beer\", 7), (\"coffee\", 7)]\n",

0 commit comments

Comments
 (0)