From 4f4e722f236facbb45141a93ef80dcd3f592f1e4 Mon Sep 17 00:00:00 2001 From: Martin Mares <mj@ucw.cz> Date: Fri, 1 Nov 2019 18:06:23 +0100 Subject: [PATCH] =?UTF-8?q?05:=20Uk=C3=A1zkov=C3=A9=20programy=20z=20cvi?= =?UTF-8?q?=C4=8Den=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05-funkce/fibonacci.py | 9 +++++++++ 05-funkce/kvadrov.py | 22 ++++++++++++++++++++++ 05-funkce/min3.py | 10 ++++++++++ 05-funkce/prunik.py | 13 +++++++++++++ 05-funkce/suda.py | 15 +++++++++++++++ 5 files changed, 69 insertions(+) create mode 100755 05-funkce/fibonacci.py create mode 100755 05-funkce/kvadrov.py create mode 100755 05-funkce/min3.py create mode 100755 05-funkce/prunik.py create mode 100755 05-funkce/suda.py diff --git a/05-funkce/fibonacci.py b/05-funkce/fibonacci.py new file mode 100755 index 0000000..a3da97e --- /dev/null +++ b/05-funkce/fibonacci.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 + +def fibonacci(n): + if n <= 1: + return n + a, b = 0, 1 + for i in range(2, n+1): + a, b = b, a+b + return b diff --git a/05-funkce/kvadrov.py b/05-funkce/kvadrov.py new file mode 100755 index 0000000..33e2d7d --- /dev/null +++ b/05-funkce/kvadrov.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +from math import sqrt + +# Vrátí seznam kořenů rovnice ax**2 + bc + c = 0 +def kvadraticka_rovnice(a, b, c): + if a == 0: + print("Lineární rovnice neřešíme") + return [] + d = b**2 - 4*a*c # Diskriminant + if d < 0: + return [] + elif d == 0: + return [-b/(2*a)] + else: + q = sqrt(d) + return [(-b - q) / (2*a), (-b + q) / (2*a)] + +# Příklady: +print(kvadraticka_rovnice(4, 0, -64)) # 2 kořeny +print(kvadraticka_rovnice(4, 0, 64)) # žádný kořen +print(kvadraticka_rovnice(1, -2, 1)) # 1 kořen diff --git a/05-funkce/min3.py b/05-funkce/min3.py new file mode 100755 index 0000000..0de8f4e --- /dev/null +++ b/05-funkce/min3.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 + +def min2(x, y): + if x < y: + return x + else: + return y + +def min3(x, y, z): + return min2(min2(x, y), z) diff --git a/05-funkce/prunik.py b/05-funkce/prunik.py new file mode 100755 index 0000000..12bf17b --- /dev/null +++ b/05-funkce/prunik.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +# Spočítá průnik dvou setříděných seznamů +def prunik_seznamu(a, b): + vystup = [] + j = 0 + for x in a: + while j < len(b) and b[j] < x: + j += 1 + if j < len(b) and b[j] == x: + vystup.append(x) + j += 1 + return vystup diff --git a/05-funkce/suda.py b/05-funkce/suda.py new file mode 100755 index 0000000..3556733 --- /dev/null +++ b/05-funkce/suda.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +def kolik_sudych(seznam): + c = 0 + for x in seznam: + if x % 2 == 0: + c += 1 + return c + +def vyber_suda(seznam): + vystup = [] + for x in seznam: + if x % 2 == 0: + vystup.append(x) + return vystup -- GitLab