{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cell-0",
   "metadata": {},
   "source": [
    "# Recap of lessons 1-3 -\n",
    "## 15 tasks\n",
    "\n",
    "\n",
    "**Topics:**\n",
    "- Lesson 1: types, input, conditions\n",
    "- Lesson 2: loops, lists, slicing\n",
    "- Lesson 3: tuple/set/dict, functions, *args/**kwargs, lambda\n",
    "\n",
    "**How to solve:** replace `pass` with your code, press Shift+Enter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sec-1",
   "metadata": {},
   "source": [
    "---\n",
    "# Section 1. Lesson 1: types and conditions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t1-md",
   "metadata": {},
   "source": [
    "## Task 1. Temperature conversion\n",
    "\n",
    "Given a value in Celsius. Convert it to Fahrenheit using the formula `F = C * 9/5 + 32` and print the result with two decimal places.\n",
    "\n",
    "Hint: `f\"{value:.2f}\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t1",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = 25.0\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t2-md",
   "metadata": {},
   "source": [
    "## Task 2. Discount calculator\n",
    "\n",
    "Given a product price and a category (`\"vip\"`, `\"regular\"`, `\"new\"`). Print the total:\n",
    "- VIP: 20% discount\n",
    "- regular: 10% discount\n",
    "- new: no discount\n",
    "- otherwise: \"unknown category\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "t2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "800.0\n",
      "900.0\n",
      "1000\n",
      "unknown category\n"
     ]
    }
   ],
   "source": [
    "price = 1000\n",
    "\n",
    "categories=['vip','regular','new','unknown']\n",
    "\n",
    "\n",
    "for i in categories:\n",
    "    if i == 'vip':\n",
    "        total = price*0.8\n",
    "    elif i == 'regular':\n",
    "        total = price*0.9\n",
    "    elif i == 'new':\n",
    "        total = price*1\n",
    "    else:\n",
    "        total = 'unknown category'\n",
    "    print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t3-md",
   "metadata": {},
   "source": [
    "## Task 3. Letter grade by score\n",
    "\n",
    "Given a score from 0 to 100. Print the grade:\n",
    "- 90-100: \"A\"\n",
    "- 75-89:  \"B\"\n",
    "- 60-74:  \"C\"\n",
    "- 0-59:   \"F\"\n",
    "- otherwise:  \"invalid score\"\n",
    "\n",
    "Test on: 95, 75, 60, 40, 150."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "t3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "F\n",
      "F\n",
      "Invalid score\n",
      "F\n"
     ]
    }
   ],
   "source": [
    "def grade(score):\n",
    "    if 90 <= score <100:\n",
    "        return 'A'\n",
    "    elif 75 <= score <90:\n",
    "        return 'B'\n",
    "    elif 60 <= score <75:\n",
    "        return 'C'\n",
    "    elif 0 <= score <60:\n",
    "        return 'F'\n",
    "    else:\n",
    "        return 'Invalid score'\n",
    "    \n",
    "scores= [90,20,32,220,10]\n",
    "\n",
    "for i in scores:\n",
    "    print(grade(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sec-2",
   "metadata": {},
   "source": [
    "---\n",
    "# Section 2. Lesson 2: loops and lists"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t4-md",
   "metadata": {},
   "source": [
    "## Task 4. Sum of numbers from 1 to n\n",
    "\n",
    "Given `n`. Compute the sum of numbers from 1 to n inclusive via a loop (without `sum()`).\n",
    "\n",
    "Check: for n=100 the answer is 5050."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "t4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "n=100\n",
    "\n",
    "\n",
    "total=0\n",
    "\n",
    "for i in range(1,n+1):\n",
    "    total += i\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t5-md",
   "metadata": {},
   "source": [
    "## Task 5. Reverse a list via slicing\n",
    "\n",
    "Given a list. Get its reverse in one line via the slice `[::-1]`.\n",
    "\n",
    "```python\n",
    "items = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
    "# Expected: ['e', 'd', 'c', 'b', 'a']\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "t5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['e', 'd', 'c', 'b', 'a']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "items = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
    "\n",
    "\n",
    "items[::-1]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t6-md",
   "metadata": {},
   "source": [
    "## Task 6. Remove duplicates preserving order\n",
    "\n",
    "Given a list with repeats. Get a list of unique values in the order of first appearance.\n",
    "\n",
    "```python\n",
    "items = [1, 3, 1, 2, 3, 4, 2, 5]\n",
    "# Expected: [1, 3, 2, 4, 5]\n",
    "```\n",
    "\n",
    "Hint: use a loop and a helper `set`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "t6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 2, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "items = [1, 3, 1, 2, 3, 4, 2, 5]\n",
    "\n",
    "s=set()\n",
    "results=[]\n",
    "\n",
    "\n",
    "for i in items:\n",
    "    if i not in s:\n",
    "        results.append(i)\n",
    "        s.add(i)\n",
    "\n",
    "print(results)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sec-3",
   "metadata": {},
   "source": [
    "---\n",
    "# Section 3. Lesson 3: tuple, set, dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t7-md",
   "metadata": {},
   "source": [
    "## Task 7. Unique words\n",
    "\n",
    "Given a string. Count the number of unique words (case-insensitive).\n",
    "\n",
    "```python\n",
    "text = \"Python is great Python is fun and Python is fast\"\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "t7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "text = \"Python is great Python is fun and Python is fast\"\n",
    "\n",
    "words = text.lower().split()\n",
    "\n",
    "unique_words = set(words)\n",
    "\n",
    "print(len(unique_words))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t8-md",
   "metadata": {},
   "source": [
    "## Task 8. Letter frequency\n",
    "\n",
    "Given a word. Build a dict `{letter: count}`. Ignore case.\n",
    "\n",
    "```python\n",
    "word = \"programming\"\n",
    "# Expected: {'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t8",
   "metadata": {},
   "outputs": [],
   "source": [
    "word = \"programming\"\n",
    "\n",
    "# Your code here\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t9-md",
   "metadata": {},
   "source": [
    "## Task 9. Intersecting interests\n",
    "\n",
    "Given two users' interests. Find:\n",
    "- common (`&`)\n",
    "- only the first one's (`-`)\n",
    "- all together (`|`)\n",
    "\n",
    "```python\n",
    "anya  = {\"python\", \"sql\", \"git\", \"docker\"}\n",
    "boris = {\"python\", \"java\", \"git\", \"linux\"}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t9",
   "metadata": {},
   "outputs": [],
   "source": [
    "anya  = {\"python\", \"sql\", \"git\", \"docker\"}\n",
    "boris = {\"python\", \"java\", \"git\", \"linux\"}\n",
    "\n",
    "# Your code here\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sec-4",
   "metadata": {},
   "source": [
    "---\n",
    "# Section 4. Lesson 3: functions, *args, **kwargs, lambda"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t10-md",
   "metadata": {},
   "source": [
    "## Task 10. Average score via *args\n",
    "\n",
    "Write a function `avg(*scores)` that returns the mean. If there are no arguments - return `0`.\n",
    "\n",
    "```python\n",
    "avg(85, 72, 91)   # 82.666...\n",
    "avg()             # 0\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t10",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t11-md",
   "metadata": {},
   "source": [
    "## Task 11. CSV row via **kwargs\n",
    "\n",
    "Write a function `to_csv_row(**fields)` that returns a string of the form `key1=val1,key2=val2,...`.\n",
    "\n",
    "```python\n",
    "to_csv_row(name=\"Anna\", age=30, city=\"Tashkent\")\n",
    "# 'name=Anna,age=30,city=Tashkent'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t12-md",
   "metadata": {},
   "source": [
    "## Task 12. Group words by length\n",
    "\n",
    "Write a function `group_by_len(words)` that returns `{length: [words of that length]}`.\n",
    "\n",
    "```python\n",
    "words = [\"sun\", \"moon\", \"sky\", \"star\", \"cloud\", \"rain\", \"snow\"]\n",
    "# {3: ['sun', 'sky'], 4: ['moon', 'star', 'rain', 'snow'], 5: ['cloud']}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t12",
   "metadata": {},
   "outputs": [],
   "source": [
    "words = [\"sun\", \"moon\", \"sky\", \"star\", \"cloud\", \"rain\", \"snow\"]\n",
    "\n",
    "# Your code here\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sec-5",
   "metadata": {},
   "source": [
    "---\n",
    "# Section 5. Final tasks"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t13-md",
   "metadata": {},
   "source": [
    "## Task 13. Top-N students\n",
    "\n",
    "Given a dict `{name: score}`. Write `top_n(scores, n=3)` that returns a list of `(name, score)` pairs in descending order of scores.\n",
    "\n",
    "```python\n",
    "scores = {\"Anna\": 85, \"Boris\": 72, \"Vika\": 91, \"Gleb\": 68, \"Dima\": 88}\n",
    "top_n(scores, 3)\n",
    "# [('Vika', 91), ('Dima', 88), ('Anna', 85)]\n",
    "```\n",
    "\n",
    "Hint: `sorted(...)` + `lambda` + slice `[:n]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t13",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = {\"Anna\": 85, \"Boris\": 72, \"Vika\": 91, \"Gleb\": 68, \"Dima\": 88}\n",
    "\n",
    "# Your code here\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t14-md",
   "metadata": {},
   "source": [
    "## Task 14. CSV row parser\n",
    "\n",
    "Given a list of strings in the format `\"name,age,city\"`. Build a list of dicts. Age is `int`, the rest are strings.\n",
    "\n",
    "```python\n",
    "rows = [\n",
    "    \"Anna,30,Tashkent\",\n",
    "    \"Boris,25,Samarkand\",\n",
    "]\n",
    "# [{'name': 'Anna', 'age': 30, 'city': 'Tashkent'}, ...]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t14",
   "metadata": {},
   "outputs": [],
   "source": [
    "rows = [\n",
    "    \"Anna,30,Tashkent\",\n",
    "    \"Boris,25,Samarkand\",\n",
    "    \"Vika,28,Bukhara\",\n",
    "]\n",
    "\n",
    "# Your code here\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "t15-md",
   "metadata": {},
   "source": [
    "## Task 15. Score analyzer (final)\n",
    "\n",
    "Write a function `analyze(students, passing=60)`. Takes a list of dicts `{\"name\": str, \"score\": int}`, returns statistics:\n",
    "\n",
    "- `total` - total students\n",
    "- `passed` - how many passed (`score >= passing`)\n",
    "- `failed` - how many failed\n",
    "- `avg_passed` - average score among those who passed (or 0)\n",
    "- `top` - the name of the best (or `None` if the list is empty)\n",
    "\n",
    "All topics are at play here: loop, condition, dict, list, function, default, lambda, max."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "t15",
   "metadata": {},
   "outputs": [],
   "source": [
    "students = [\n",
    "    {\"name\": \"Anna\",  \"score\": 85},\n",
    "    {\"name\": \"Boris\", \"score\": 42},\n",
    "    {\"name\": \"Vika\",  \"score\": 91},\n",
    "    {\"name\": \"Gleb\",  \"score\": 58},\n",
    "    {\"name\": \"Dima\",  \"score\": 73},\n",
    "]\n",
    "\n",
    "# Your code here\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-final",
   "metadata": {},
   "source": [
    "---\n",
    "## What's next\n",
    "\n",
    "If all 15 tasks are solved, you're ready for **lesson 4** (OOP)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
