2020
Dawid J. Kubis 5 years ago
parent babb2b0083
commit 2703a2c89f

@ -0,0 +1,9 @@
# priklad.py
```python
class Clovek:
def __init__(self, age, weight):
self.age = age
self.weight = weight
```

@ -0,0 +1,71 @@
# fraction.py
```python
#!/usr/bin/python
from math import gcd # importuju greatest common divisor
# aby se mi snaz kratily zlomky
# definujeme novou tridu
class Fraction:
# definujeme co se ma dit pri vytvoreni objektu
def __init__(self, up, down):
self.up = up # self.up != up
self.down = down
self.base() # bude se hodit pri zbytku implementaci
# zarucuje ze nebudeme muset explicitne volat
# metodu `base` nikde jinde
# kraceni zlomku
# vsimneme si ze narozdil od funkci, u metod
# je v pohode i jiny typ vraceni z funkce nez
# jenom pomoci `return`
# - metoda muze menit objekt kterymu nalezi
def base(self):
divisor = gcd(self.up, self.down) # ziskavame nejvedsi spolecny delitel
if divisor > 1: # zjistujeme jestli muzeme kratit
self.up = self.up // divisor # znena sebe sama
self.down = self.down // divisor # a zase
self.base() # rekurze, slo by to udelat i snadneji
# scitani zlomku
def add(self, other):
return Fraction(self.up * other.down + other.up * self.down, self.down * other.down)
# ^- tohle mi zaruci ze se ten zlomek automaticky zkrati
# jelikoz tvorim novy objekt a v initu mam `self.base()`
# taky vracim jiny objekt a nemenim ty, ktere jsem dostal
# nasobeni zlomku
def multiply(self, other):
return Fraction(self.up * other.up, self.down * other.down)
# odcitani zlomku
def subtract(self, other):
return Fraction(self.up * other.down - other.up * self.down, self.down * other.down)
# deleni
# prosim tady nepouzivat pythonovsky deleni, je velice nepresny
def divide(self, other):
return Fraction(self.up * other.down, self.down * other.up)
# aby se mi to hezky printlo
def show(self):
return f"{self.up}/{self.down}"
# testy
for x in range(1, 4):
for y in range(1, 4):
a = Fraction(x, y)
b = Fraction(y, x)
print(f"\na: {a.show()}\nb: {b.show()}\n")
print("+", a.add(b).show())
print("*", a.multiply(b).show())
print("-", a.subtract(b).show())
print("/", a.divide(b).show())
# ez
```

@ -56,8 +56,12 @@ Učí profesor Kubis a docent Veškrna.
+ [rekurze](11/rekurze) + [rekurze](11/rekurze)
## 12 ## 12
+ [priklad](12/priklad)
+ [tridy_a_objekty](12/tridy_a_objekty) + [tridy_a_objekty](12/tridy_a_objekty)
## 13
+ [fraction](13/fraction)
## ##
+ [code_gen](code_gen) + [code_gen](code_gen)
+ [site_gen](site_gen) + [site_gen](site_gen)

Loading…
Cancel
Save