{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MAF gyakorlat 1\n", "\n", "## Tárgy elei információk\n", " - Követelmények\n", " - Hogy fog zajlani a gyakorlat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Egyszerű aritmetika" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # Összeadás" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*3 # Szorzás" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.6666666666666667" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5/3 #Osztás" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5//3 # Maradékos osztás hányadosa" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5%3 # Maradékos osztas maradéka" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1024" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**10 # Hatványozás" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. `range()`\n", "\n", "A `range()` egy függveny, mellyel létre tudunk hozni egy olyan egyszeri \"felsorolható\" objektumot, ami egy számtani sorozat elemeit adja vissza.\n", "\n", "Első példa: `range(10)`" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(10)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(5, 10)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(5,10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Gyakran elegendő az előbb emlitett objektum, de neha magára a listára van szüksegünk, akkor azzá tudjuk konvertálni `list(range(10))` -módon" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Shift+Tab segitsegevel elő tudjuk hívni a Doc stringet. Kiderül, hogy a `range()` függveny többfélekepp is parameterezhető.\n", "\n", "Példa: lista egy számtani sorozatból, melynek differenciája -2, 10-zel kezdodik és a sorozat nem lépi át az 1-et:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 8, 6, 4, 2]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(10,1,-2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1 Írd ki a számokat 70-től 79-ig." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2 Írd ki a páros számokat 50-től 65-ig." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### \\*1.3 Írd ki a számokat 19-től 10-ig." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Ciklus és elágazás\n", "\n", "A `for` ciklus segitségével végig tudunk iteralni egy \"felsorolható\" objektum elemein. (pl: range, lista, szotar kulcsai) Indentálással jelezzük, hogy mit tartozik a ciklushoz." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(10):\n", " print(i)\n", "print('Én már nem tartozok a ciklushoz')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `while` ciklus segítségével ismételten végrehajthatunk parancsokat, amíg egy megadott feltétel teljesül. Itt is indentálással jelöljük, hogy mely parancsok tartoznak a ciklushoz." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x=2\n", "while x<10:\n", " x=x+1\n", " print(x)\n", "print('Én már nem tartozok a ciklushoz')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`if` segítségével esetszétválasztást definiálhatunk egy feltétel alapján. Indentálással jelezzük, hogy melyik parancsok tartoznak az elágazáshoz." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nagy\n" ] } ], "source": [ "x=5\n", "if x<4:\n", " print('kicsi')\n", "else:\n", " print('nagy')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.1 Sorold fel az $(x,y)$ egész számpárokat ahol $0\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0miranyito_szamok\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Nekeresd'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'Nekeresd'" ] } ], "source": [ "iranyito_szamok['Nekeresd']" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "iranyito_szamok.get('Nekeresd')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Nekeresd' in iranyito_szamok" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Szótár bővítése, `iranyito_szamok['Martely']=6636`" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Hodmezovasarhely': 6800, 'Szegvar': 6635, 'Szeged': 6700, 'Martely': 6636}" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iranyito_szamok['Martely']=6636\n", "iranyito_szamok" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Kulcsok elkerese" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['Hodmezovasarhely', 'Szegvar', 'Szeged', 'Martely'])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iranyito_szamok.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Szótárban szereplő értékek" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values([6800, 6635, 6700, 6636])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iranyito_szamok.values()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Szótarbeli (kulcs,érték) párok" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('Hodmezovasarhely', 6800), ('Szegvar', 6635), ('Szeged', 6700), ('Martely', 6636)])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iranyito_szamok.items()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.1 Keressük meg az előző szótárban azt a települést, amelyiknek a legkisebb az iranyitószáma (Hint: használjuk for ciklus ciklusvaltozójának `iranyito_szamok.keys()` vagy `iranyito_szamok.items()`" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "#TODO" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Függvények\n", "\n", "Egy egyszerű egyváltozós függvény így néz ki:\n", "\n", "`def fgv_nev(valtozo_nev):\n", " parancsok\n", " return visszateresi_ertek`\n", " \n", " Például: \n", " \n", "`def negyzet(x):\n", " return x*x`\n", "\n", "A következő feladatokban függvény vázlatokat láthattok. Fejezzétek be őket.\n", "Ha helyesen dolgoztok, a feladatok alján lévő ellenörző \"assert\" állítások nem fognak hibát jelezni. (Természetesen, ez visszafelé nem feltétlenül igaz.) Nyugodtan bővíthetitek a tesztek listáját. \n", "\n", "### 6.1 Írj egy függvényt, amely beolvas egy N számot, majd pedig eldönti, hogy a szám prím-e." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def is_prime(N):\n", " # TODO\n", "\n", "assert(is_prime(2)==True)\n", "assert(is_prime(3)==True)\n", "assert(is_prime(4)==False)\n", "assert(is_prime(5)==True)\n", "assert(is_prime(6)==False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.2 Írj egy függvényt, amely beolvas egy N számot, majd pedig visszatér egy listával, aminek elemei az első N darab prímszám, 2-vel kezdve." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_n_primes(N):\n", " # TODO\n", "\n", "assert(get_n_primes(3) == [2, 3, 5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.3 Írj egy függvényt, ami egy `int` paramétert vár (N), majd pedig visszatér az N. Fibonacci számmal.\n", "\n", "A Fibonacci sorozat így kezdődik: 1, 1, 2, 3, 5, 8, Tehát a második szám 1 és a harmadik 2." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_nth_fibonacci(N):\n", " # TODO\n", "\n", "assert(get_nth_fibonacci(2) == 1)\n", "assert(get_nth_fibonacci(3) == 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.4 Írj egy függvényt, ami egy `int` paramétert vár (N), majd pedig visszatér az első N darab Fibonacci számot tartalmazó listával.\n", "\n", "Próbáld meg felhasználni az előző feladatban megírt függvényed!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_n_fibonacci(N):\n", " # TODO\n", " \n", "assert(get_n_fibonacci(4) == [1, 1, 2, 3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### \\*6.5 Írj egy függvényt, ami elfogad két egész számot, A-t és B-t, és vissztér B-A darab Fibbonacci számmal, az A.-tól a B-1.-ig. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_range_fibonacci(A, B):\n", " # TODO\n", "\n", "assert(get_range_fibonacci(2, 5) == [1, 2, 3])\n", "assert(get_range_fibonacci(5, 5) == [])\n", "assert(get_range_fibonacci(1, 5) == [1, 1, 2, 3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. Extra feladatok\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7.1 Írj egy függvényt, amely elfogad egy $q$ egész számot, ha az adott szám egy prím, akkor visszatér egy szótárral, ami minden $i\\neq 0$-re megadja az $\\mathbb{F}_q$-beli inverzt, különben egy üres listával terjen vissza." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_inverses(q):\n", " #TODO\n", " \n", "assert(get_inverse(3)=={1:1,2:2})\n", "assert(get_inverse(4)=={})\n", "assert(get_inverse(5)=={1:1,2:3,3:2,4:4})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7.2 Írj egy függvényt, amely elfogad egy egész számot, ha az adott szám egy prím, akkor visszatér egy szótárral ami minden $i$-re megad egy listát, ami az $\\mathbb{F}_q$-beli négyzetgyököket tartalmazza, különben egy üres listával terjen vissza." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_sqrt(q):\n", " #TODO\n", " \n", "assert(get_sqrt(3)=={0:[0],1:[1,2],2:[]})\n", "assert(get_sqrt(4)=={})\n", "assert(get_sqrt(5)=={0:[0],1:[1,4],2:[],3:[],4:[2,3]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7.3 Írj egy függvenyt, amely elfogad 4 számot $q,a,b,c$, ellenőrizvén, hogy $q$ egy prím, visszatér egy listaval, ahol első érték megadja, hogy $a x^2+ b x +c \\equiv 0 (\\mod q)$ megoldható-e, míg a második elem egy lista a megoldásokkal. (Ha nincs megoldás, akkor egy üres listával tér vissza)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def solve(q):\n", " #todo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }