|
22 | 22 | }, |
23 | 23 | { |
24 | 24 | "cell_type": "code", |
25 | | - "execution_count": 17, |
| 25 | + "execution_count": null, |
26 | 26 | "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": [], |
52 | 28 | "source": [ |
53 | 29 | "def memoize(func):\n", |
54 | 30 | "\n", |
|
147 | 123 | }, |
148 | 124 | { |
149 | 125 | "cell_type": "code", |
150 | | - "execution_count": 75, |
| 126 | + "execution_count": null, |
151 | 127 | "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": [], |
164 | 129 | "source": [ |
165 | 130 | "class Query:\n", |
166 | 131 | " def __init__(self, iterable):\n", |
|
306 | 271 | }, |
307 | 272 | { |
308 | 273 | "cell_type": "code", |
309 | | - "execution_count": 155, |
| 274 | + "execution_count": null, |
310 | 275 | "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": [], |
323 | 277 | "source": [ |
324 | 278 | "def evaluate(expr, env):\n", |
325 | 279 | " match expr:\n", |
|
484 | 438 | }, |
485 | 439 | { |
486 | 440 | "cell_type": "code", |
487 | | - "execution_count": 162, |
| 441 | + "execution_count": null, |
488 | 442 | "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": [], |
509 | 444 | "source": [ |
510 | 445 | "from collections import namedtuple\n", |
511 | 446 | "from functools import reduce\n", |
|
521 | 456 | "\n", |
522 | 457 | "\n", |
523 | 458 | "def top_products(orders, n):\n", |
524 | | - "\n", |
525 | 459 | " products = [(order.product, order.quantity) for order in orders]\n", |
526 | 460 | " names = {name for name, _ in products}\n", |
527 | 461 | "\n", |
|
531 | 465 | "\n", |
532 | 466 | "\n", |
533 | 467 | "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", |
535 | 475 | "\n", |
536 | 476 | "\n", |
537 | 477 | "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", |
539 | 492 | "\n", |
540 | 493 | "\n", |
541 | 494 | "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", |
543 | 506 | "\n", |
544 | 507 | "\n", |
545 | 508 | "# Tests:\n", |
|
558 | 521 | "assert revenue_by_customer(orders) == {\"ana\": 33.0, \"bob\": 30.0, \"cvetan\": 23.0}\n", |
559 | 522 | "assert revenue_by_customer([]) == {}\n", |
560 | 523 | "\n", |
561 | | - "print(top_products(orders, 2))\n", |
562 | 524 | "assert top_products(orders, 2) == [(\"tea\", 9), (\"coffee\", 7)]\n", |
563 | 525 | "assert top_products(orders, 10) == [(\"tea\", 9), (\"coffee\", 7), (\"cake\", 3)]\n", |
564 | 526 | "assert top_products(orders + [Order(\"ana\", \"beer\", 7, 2.0, 3)], 3) == [(\"tea\", 9), (\"beer\", 7), (\"coffee\", 7)]\n", |
|
0 commit comments