{ "cells": [ { "cell_type": "markdown", "id": "permanent-destruction", "metadata": {}, "source": [ "# Lambda, Classes and Objects (10/3 - 2021)" ] }, { "cell_type": "markdown", "id": "focal-point", "metadata": {}, "source": [ "## Exercise\n", "\n", "Given a list ``L`` of intergers, create a new list where all integers are incremented by one. E.g. for ``L = [3, 4, 5]`` you should create the list ``[4, 5, 6]``." ] }, { "cell_type": "code", "execution_count": 38, "id": "marine-probe", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 5, 6]\n", "[4, 5, 6]\n", "[4, 5, 6]\n", "[4, 5, 6]\n", "[4, 5, 6]\n", "[4, 5, 6]\n" ] } ], "source": [ "L = [3, 4, 5]\n", "\n", "def inc(x):\n", " return x + 1\n", "\n", "# print(inc(42))\n", "# print((lambda x: x + 1)(42))\n", "\n", "L_ = []\n", "for x in L:\n", " L_.append(x + 1)\n", "print(L_)\n", "\n", "L_ = []\n", "for x in L:\n", " L_.append(inc(x))\n", "print(L_)\n", "\n", "L_ = [x + 1 for x in L]\n", "print(L_)\n", "\n", "L_ = [inc(x) for x in L]\n", "print(L_)\n", "\n", "L_ = list(map(inc, L))\n", "print(L_)\n", "\n", "#inc_lambda = lambda x: x + 1\n", "#print(inc_lambda(42))\n", "#print(inc(42))\n", "#print((lambda x: x + 1)(42))\n", "L_ = list(map(lambda x: x + 1, L))\n", "print(L_)" ] }, { "cell_type": "markdown", "id": "labeled-remains", "metadata": {}, "source": [ "## Exercise\n", "\n", "Sort a list of strings by the number of distinct characters (strings with the same number of distinct symbols can appear in any order)." ] }, { "cell_type": "code", "execution_count": 39, "id": "adjacent-alberta", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7 hocus pocus\n", "5 bibbidi bobbidi boo\n", "5 alakazam\n", "12 presto chango\n", "5 abracadabra\n", "9 joshikazam\n", "7 sim sala bim\n", "['abracadabra', 'alakazam', 'bibbidi bobbidi boo', 'hocus pocus', 'sim sala bim', 'joshikazam', 'presto chango']\n", "['abracadabra', 'alakazam', 'bibbidi bobbidi boo', 'hocus pocus', 'sim sala bim', 'joshikazam', 'presto chango']\n", "['bibbidi bobbidi boo', 'alakazam', 'abracadabra', 'hocus pocus', 'sim sala bim', 'joshikazam', 'presto chango']\n", "['bibbidi bobbidi boo', 'alakazam', 'abracadabra', 'hocus pocus', 'sim sala bim', 'joshikazam', 'presto chango']\n" ] } ], "source": [ "def distinct(text):\n", " return len(set(text))\n", "\n", "texts = ['hocus pocus', 'bibbidi bobbidi boo', 'alakazam', 'presto chango', 'abracadabra', 'joshikazam', 'sim sala bim']\n", "\n", "for text in texts: \n", " print(distinct(text), text)\n", " \n", "#print('String sorted:', sorted(texts))\n", "L = [(distinct(text), text) for text in texts]\n", "#print(L)\n", "L_sorted = sorted(L)\n", "#print(L_sorted)\n", "L_ = []\n", "for d, text in L_sorted:\n", " L_.append(text)\n", "print(L_)\n", "L_ = [text for (d, text) in sorted(L)]\n", "print(L_)\n", "L_ = sorted(texts, key=distinct)\n", "print(L_)\n", "#print(sorted(texts, key=len))\n", "print(sorted(texts, key=lambda text: len(set(text))))" ] }, { "cell_type": "markdown", "id": "collectible-sigma", "metadata": {}, "source": [ "## Exercise\n", "\n", "Sort a list of strings by number of vovals." ] }, { "cell_type": "code", "execution_count": 51, "id": "molecular-depression", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['hocus pocus', 'alakazam', 'presto chango', 'joshikazam', 'sim sala bim', 'abracadabra', 'bibbidi bobbidi boo']\n" ] } ], "source": [ "vowals = 'aeiou'\n", "\n", "texts = ['hocus pocus', 'bibbidi bobbidi boo', 'alakazam', 'presto chango', 'abracadabra', 'joshikazam', 'sim sala bim']\n", "\n", "#print('o' in vowals)\n", "#print('x' in vowals)\n", "#print([c in vowals for c in texts[0]])\n", "#print(sum([c in vowals for c in texts[0]]))\n", "\n", "print(sorted(texts, key=lambda text: sum([c in vowals for c in text])))" ] }, { "cell_type": "markdown", "id": "talented-worcester", "metadata": {}, "source": [ "## Exercise\n", "\n", "Create a class ``Beverage`` to store information about a beverage. Create a ``__init__`` method to be able to create a beverage object using:\n", "\n", "
\n",
    "CocaCola = Beverage(\n",
    "  name = \"Coca Cola\",\n",
    "  volume = 0.33,\n",
    "  container = \"Can\",\n",
    "  alcohol_percent = 0\n",
    ")\n",
    "
\n", "or\n", "
\n",
    "HarboeBottle = Beverage('Harboe Pilsner', 0.33, 'Glass', 4.4)\n",
    "
\n", "\n", "Create methods ``__str__`` to be able to print a ``Beverage`` object, and ``__le__`` to be able to compare objects, e.g. just order them by name. Create a method ``total_alcohol`` return the total abount of alcohol in a beverage unit." ] }, { "cell_type": "code", "execution_count": 89, "id": "imported-preparation", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Coca Cola (Can, 33.0 cl 0%)\n", "Harboe Pilsner (Glass, 33.0 cl 4.4%)\n", "Carlsberg (Can, 33.0 cl 4.6%)\n", "Harboe Pilsner (Can, 33.0 cl 4.6%)\n", "Tuborg fine festival (Can, 50.0 cl 7.5%)\n" ] } ], "source": [ "class Beverage:\n", " def __init__(self, name, volume, container, alcohol_percent):\n", " #def __init__(self, *, name, volume, container, alcohol_percent):\n", " self.name = name\n", " self.volume = volume\n", " self.container = container\n", " self.alcohol_percent=alcohol_percent\n", " \n", " def __str__(self):\n", " return self.name + ' (' + self.container + ', ' + str(self.volume * 100) + ' cl ' + str(self.alcohol_percent) + '%)'\n", " \n", " def total_alcohol(self):\n", " return self.volume * self.alcohol_percent / 100\n", " \n", " def __lt__(self, other):\n", " return self.name < other.name\n", " \n", "CocaCola = Beverage(\n", " name=\"Coca Cola\",\n", " volume = 0.33,\n", " container = \"Can\",\n", " alcohol_percent = 0\n", ")\n", "\n", "Carlsberg = Beverage(\n", " name=\"Carlsberg\",\n", " volume = 0.33,\n", " container = \"Can\",\n", " alcohol_percent = 4.6\n", ")\n", "\n", "FF = Beverage(\"Tuborg fine festival\", 0.5, \"Can\", 7.5)\n", "HarboeCan = Beverage('Harboe Pilsner', 0.33, 'Can', 4.6)\n", "HarboeBottle = Beverage('Harboe Pilsner', 0.33, 'Glass', 4.4)\n", "\n", "#print(CocaCola)\n", "#print(CocaCola.total_alcohol())\n", "\n", "beverages = [CocaCola, Carlsberg, FF, HarboeCan, HarboeBottle]\n", "\n", "#for beverage in sorted(beverages):\n", "#for beverage in sorted(beverages, key=lambda beverage: beverage.name):\n", "for beverage in sorted(beverages, key=lambda beverage: beverage.total_alcohol()):\n", " print(beverage)\n" ] } ], "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.9.1" } }, "nbformat": 4, "nbformat_minor": 5 }