{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d8986a28",
   "metadata": {},
   "source": [
    "# Homeworks\n",
    "\n",
    "These exercises are optional, but to learn a programming language, it is better to practise!\n",
    "\n",
    "You can use anything and everything to solve these exercises, **BUT** you must understand what you are doing and why! Don't copy and paste code from AI or Stack Overflow without understanding its purpose, it may contain bugs!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a25fc8b5",
   "metadata": {},
   "source": [
    "## Exercice 1\n",
    "\n",
    "### Question 1.1\n",
    "\n",
    "Split the following string to make a list :\n",
    "`fruits = \"banana,apple,pear,orange,lemon,watermelon,apple,lemon,orange\"`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "149b5355",
   "metadata": {},
   "source": [
    "### Question 1.2\n",
    "\n",
    "Add the content of this new `list` to the previous `list`:\n",
    "\n",
    "``` python\n",
    "new_fruits_list = ['dragon fruit', 'kiwi', 'watermelon', 'lemon']\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7de7082f",
   "metadata": {},
   "source": [
    "### Question 1.3\n",
    "\n",
    "We want to know the quantity of each fruit in the list.  \n",
    "We are going to store this information in a dictionary (`my_fruit_dict`), with the fruit’s name as the key and its quantity as the value."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6e18d70",
   "metadata": {},
   "source": [
    "### Question 1.4\n",
    "\n",
    "We want to sell all the fruits.  \n",
    "Calculate the total price for each fruit, store it in a dictionary called `sold_fruit`.  \n",
    "The dictionary below (`fruit_price`) shows the price of each fruit.\n",
    "\n",
    "```python\n",
    "fruit_price = {\"pear\" : 0.65, \"kiwi\" : 1.0, \"orange\" : 0.80, \"lemon\" : 0.80, \"watermelon\" : 3, \"dragon fruit\" : 5.5}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8709db1f",
   "metadata": {},
   "source": [
    "### Question 1.5\n",
    "\n",
    "If the sum of the `sold_fruit` is below 3, we will increase its price by 20%. If it is above 5, we will reduce its price by 20%.  \n",
    "Change the value of `fruit_price`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "191134a5",
   "metadata": {},
   "source": [
    "### Question 1.6\n",
    "\n",
    "Create a function called `fruits_expectation` that indicates how much money you could expect to make if you sold all your fruits.  \n",
    "Use loops to do this, don't use the `sum` function.  \n",
    "Your function must take two dictionaries as arguments, one containing your current fruits and the other containing the expected price of each fruit."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcab5c42",
   "metadata": {},
   "source": [
    "## Exercise 2\n",
    "\n",
    "Let’s do a few exercises on nested lists.\n",
    "\n",
    "<!-- The following nested list contains information on some animals. In order, there are the name, the age, the sex and the animal type. -->\n",
    "\n",
    "### Question 2.1\n",
    "\n",
    "`complex_list = [['cat', 4, 'female', 'Minette'], ['cat', 5, 'male', 'Moustache'], ['dog', 2, 'female', 'Regina']]`\n",
    "\n",
    "What do the following items contain?  \n",
    "What type of object is it? (`string`, `int`, `list`...)\n",
    "\n",
    "- `complex_list[0]`  \n",
    "- `complex_list[0][3]`  \n",
    "- `complex_list[-1]`  \n",
    "- `complex_list[-1][1]`  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a040582",
   "metadata": {},
   "source": [
    "### Question 2.2\n",
    "\n",
    "What command should you type to access the following items?  \n",
    "\n",
    "- 'Regina'  \n",
    "- 'male'  \n",
    "- 'female'  \n",
    "- 4   \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a93f0f5",
   "metadata": {},
   "source": [
    "### Question 2.3\n",
    "\n",
    "As you will have realised, this list contains a list of animals, with each sub-list containing the following in this order:\n",
    "- type\n",
    "- age\n",
    "- sex\n",
    "- name\n",
    "\n",
    "Use a loop to display information about each animal."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca5a60ad",
   "metadata": {},
   "source": [
    "## Exercise 3\n",
    "\n",
    "Do you remember the exercise on generating email addresses?  \n",
    "(please see the brainstorming time in `functions` section).\n",
    "\n",
    "We have written a function that generates an email address from a first name, a last name, a company name and an extension, \n",
    "and a function that cleans up a string (removing spaces at the start and end of words, replacing other spaces with hyphens, and converting to lower case)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8b621c80",
   "metadata": {},
   "outputs": [],
   "source": [
    "def clean_str(my_str):\n",
    "    cleaned_str = ''\n",
    "    # remove all spaces at the beginning \n",
    "    # or end of string\n",
    "    cleaned_str = my_str.strip()\n",
    "\n",
    "    # replace all other spaces with hyphens\n",
    "    cleaned_str = cleaned_str.replace(\" \", \"-\")\n",
    "\n",
    "    # write the string in lowercase\n",
    "    cleaned_str = cleaned_str.lower()\n",
    "\n",
    "    # don't forget to return the result\n",
    "    return cleaned_str\n",
    "\n",
    "def generate_email(first_name, last_name, company='bilille', extension='fr'):\n",
    "    email = clean_str(first_name) + \".\" + clean_str(last_name) + \"@\" + clean_str(company) + \".\" + clean_str(extension)\n",
    "    return email"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95015670",
   "metadata": {},
   "source": [
    "There was one thing left for us to do: replace accented letters and other special characters with their non-special versions.\n",
    "\n",
    "<details>\n",
    "  <summary>Click here to see a tip</summary>\n",
    "  \n",
    "  You can use a dictionary containing the special characters and their non-special versions\n",
    "</details>\n",
    "\n",
    "Once you’ve done that, check your results by generating the addresses of the following people, using a list and a loop.\n",
    "\n",
    "First name | Last name        | company                 | extension | specials characters\n",
    "-----------|------------------|-------------------------|-----------|--------------------\n",
    "Andréa     | Çorbière         | Univ Montfaucon         | fr        | é, ç\n",
    "Björn      | Ölafsson         | Nordlys Biotechnologies | no        | ö\n",
    "María      | Álvarez          | IMBV                    | es        | í, á\n",
    "François   | Düpont           | Helvetia Genomics       | ch        | ç, ü\n",
    "Chloë      | De Châteaubriand | Maple Leaf              | ca        | ë, é\n",
    "Jürgen     | Schröder         | UMF                     | de        | ü, ö\n",
    "Iñigo      | Montoya          | IT Navarra              | es        | í, ñ\n",
    "Théo       | Bêlmont          | Univ Namur              | be        | é, ê\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8255fc1b",
   "metadata": {},
   "source": [
    "## Exercise 4 :fire:\n",
    "\n",
    "This exercice is quite challenging. Take it slow, check back the functions, loops and conditions sections, and if you're stuck check the tips given below for each exercice. \n",
    "\n",
    "We're doing an experiment on eye color, try to make it to the end using the least amount of tips. Good luck!\n",
    "\n",
    "### Question 4.1\n",
    "Make a function that returns an eye color from the following list at random: brown, blue, hazel, amber, gray, green.\n",
    "We consider each color has the same chances of being picked.\n",
    "\n",
    "Use the following function that returns a random integer between a and b (included), don't worry too much about what's inside, we'll see it in the 3rd day's package section.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "480647e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_random_number(a:int,b:int)->int:\n",
    "    \"\"\"\n",
    "    Generates a random integer in range [a,b] (both included)\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "    a : lower bound\n",
    "    b : upper bound\n",
    "    \n",
    "    Returns\n",
    "    -------\n",
    "    int\n",
    "        A random integer between a and b.\n",
    "        \n",
    "    Example\n",
    "    -------\n",
    "    >> random_number=get_random_number(0,10)\n",
    "    >> print(random_number)\n",
    "    6\n",
    "\n",
    "    \"\"\"\n",
    "    import random\n",
    "    return random.randint(a,b)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf4b9a33",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Click here to see a tip</summary>\n",
    "  \n",
    "  You can start with this function draft that takes no arguments and returns a random number between 0 and 5 and modify it to return an eye color. \n",
    "  \n",
    "  And if you're wondering: yes, a function doesn't need to have arguments!\n",
    "\n",
    "  ```python3\n",
    "  def get_eye_color():\n",
    "    random_number=get_random_number(0,5)\n",
    "    ...\n",
    "    return ... \n",
    "  ```\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acc58f6f",
   "metadata": {},
   "source": [
    "\n",
    "### Question 4.2\n",
    "Make a loop to run your function 100 times and count how many times you got each color.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc2e07df",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Click here to see a tip</summary>\n",
    "  You can use a loop with range(100) to execute 100 times your function.\n",
    "  </details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97995e30",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Still stuck? Check this next tip</summary>\n",
    "  You can use a dictionary to store the counts.\n",
    "  </details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "193f71cc",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Still stuck? Check this next tip</summary>\n",
    "  Start with a dictionary of each color and their counts (zero to start). Make a loop and add to your counts at each iteration.\n",
    "  </details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98722980",
   "metadata": {},
   "source": [
    "### Question 4.3\n",
    "Actually, eye color is not random. Tweak your function from Question 4.1 to return:\n",
    "- brown : 75% of the times\n",
    "- blue : 10% of the times\n",
    "- hazel : 5% of the times\n",
    "- amber : 5% of the times\n",
    "- gray : 3% of the times\n",
    "- green : 2% of the times"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dd514e8",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Click here to see a tip</summary>\n",
    "  It you had only 2 colors (brown 80% chance, blue 20% chance) you could pick a random number between 1 and 10,  1 to 8 would be brown, 9 or 10 would be blue. Try to write a function that does this, then adapt it to the full list of colors.  \n",
    "  </details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1c901f9",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Still stuck? Click here to see another tip</summary>\n",
    "  Try to use use random.randint(0,99) with conditionals.\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d57d5e21",
   "metadata": {},
   "source": [
    "Check that the proportions look good by using the loop you wrote in Question 4.2 and replacing the function with the one you just created."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e95357e",
   "metadata": {},
   "source": [
    "### Question 4.4\n",
    "For our experiment we'll need at least 100 blue eyed people. We want to know how many people total we need to pick to get at least 100 blue eyed people.\n",
    "\n",
    "Maths say we'll need around 1000 people since around 10% should be blue eyed. Let's check that !\n",
    "\n",
    "Pick eye colors until you have 100 eyed people and check how many eye colors you picked to get those 100 blue ones.\n",
    "\n",
    "<details>\n",
    "  <summary>Click here to see a tip</summary>\n",
    "  \n",
    "  We need to loop until we get 100 people, but we don't know in advance how many loops we'll need. A loop where you don't know in advance the number of repetitions, does that ring a bell ?\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1f53733",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Still stuck? Click here to see another tip</summary>\n",
    "  \n",
    "  You can write a while loop to pick a new person until you have 100 blue eyed people.\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "317350a8",
   "metadata": {},
   "source": [
    "### Question 4.5\n",
    "\n",
    "In the previous question we picked people until we had 100 blue eyed ones. Our result was close to 1000, but we did it only once. Write a loop that repeats the process 100 000 times to check what the mean and maximum number of total picks was. \n",
    "\n",
    "**Warning** : Executing 100 000 loops can take time, start with a smaller number of repetitions to check your code.\n",
    "\n",
    "<details>\n",
    "  <summary>Click here to see a tip</summary>\n",
    "  \n",
    "  Check the sum() and max() functions.\n",
    "  \n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0177ab82",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Still stuck ? Click here to see another tip</summary>\n",
    "  \n",
    "  In the previous question we just printed the total count of picks we needed. Here we'll need to save it for each iteration so that when we are done iterating we can compute the mean and maximum of all the values.\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a20b2bb",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary>Still stuck? Click here to see another tip</summary>\n",
    "  \n",
    "  You can write a for loop with range(100000) to execute the code of the previous question 100 000 times. Start with that, then think about how to save the number of picks you needed to get those 100 blue eyed people. \n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fd1dc55",
   "metadata": {},
   "source": [
    "### Question 4.6\n",
    "\n",
    "In the future we'll want to do more experiments needing different eye colors. Take your code from Question 4.5 and turn it into a function that takes eye_color, n_needed and the number of iterations tested and returns the maximum number of picks needed. \n",
    "\n",
    "ex : good_function_name(eye_color=\"gray\",n_needed=50,n_iterations=100000) returns the maximum number of picks to get 50 gray eyed people by doing 100 000 repetitions.\n",
    "\n",
    "**Warning**: Executing 100 000 loops takes time, start with a smaller number to test your code."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "2025_formation_python (3.14.4)",
   "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.14.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
