better site... hopefully?

2020
Dawid J. Kubis 4 years ago
parent b0dd677cd5
commit c4a220e0c6

@ -0,0 +1,16 @@
# kricici_lukas.py
```python
# ptame se uzivatel jestli lukas krici
krici = input('Krici Lukas? : ')
# promenna `krici` ma typ str
# zjistujeme jestli uzivatel napsal ano
# tady je rozdil mezi 'ano' a 'Ano' (velka pismena nemame osetrena)
if krici == 'ano':
print('Lukasi, mohl by ses trochu ztisit?')
else:
print('zrovna nemluvi?')
# btw tohle vymyslel Lada
```

@ -0,0 +1,22 @@
# sude_nebo_liche.py
```python
# zjistujeme cislo od uzivatele
x = input('zadej cislo : ') # x je typ str
# prevadime x na typ int pomoci funkce `int`
# tohle vyhodi chybu pokud uzivatel je dement
# a napsal neco ve smyslu 'ahoj'; to totiz nelze prevest na int
x = int(x)
# koukame se jestli zbytek z deleni dvemi je 0
# -> jesli je `x` delitelne dvemi
if x % 2 == 0:
print('je sude')
else:
# tohle je sice ekvivalent `elif x % 2 == 1`
# ale takhle je to prehlednejsi
# mohli bychom taky napsat dalsi if
# - fungovalo by to stejne ale tohle je
# mnohem prehlednejsi
print('je liche')
```

@ -0,0 +1,39 @@
# umocnovani.py
```python
# takze tohle je implementace
# skriptu ktery umocnuje cisla
# bez pouziti operatoru `**`
# je to uprimne trochu gay a
# ve skutecnosti byste tohle nikdy
# nepouzili, ale hezky to ukazuje praci
# s loopama
# ziskame zaklad
a = int(input("zadejte zaklad : "))
# ziskame exponent
e = int(input("zadejte exponent : "))
i = 0 # tohle je iteracni promenna
# budeme totiz loopovat tolikrat, kolik mame exponent
# coz je trochu problem protoze exponent musi
# byt cele cislo
# no nic
prod = 1 # do tyhle promenny to budeme vsechno nasobit
# pozor; nemuze byt 0 protoze by nula je nulovy prvek
# grupy celych cisel s nasobenim; to je jedno
# proste by nam pohltila cely nasobeni a vysledek
# by byl nula
while i < e: # loopujeme tolikrat kolik je exponent
prod *= a # nasobime cislo samym sebou
# ekvivalent
# prod = prod * a
i += 1 # pricitame iterator aby to fungovalo
# vypisujeme vysledek
print(f"vysledek : {prod}")
```

@ -0,0 +1,25 @@
# fizzbuzz.py
```python
# tahame hodnotu od uzivatele
lim = int(input('zadej cislo : '))
# prazny string
output = ''
# vsechna cisla do lim
for i in range(lim):
# reset promenny output
output = ''
# pokud delitelne tremi
if i % 3 == 0:
output += 'fizz'
# pokud delitelne peti
if i % 5 == 0:
output += 'buzz'
# zjistujeme jestli nam to
# stoji za to vypsat
if output != '':
print(output)
```

@ -0,0 +1,15 @@
# bigdick_sito.py
```python
# Tak tady bude nejaka cool implementace erastotenova sita
# timhle se radsi zatim netrapte
from functools import reduce
def gen_primes(lim): # kratsi nazev -> optimalizace :D
return [i for i in reduce((lambda a, b : [(i[0] if i[0]==i[1] else 0) for i in zip(a,b)]),[[(0 if ((x % i == 0) and (x != i)) else x) for x in range(2, lim)] for i in range(2, lim)]) if i != 0] # hehe :D
# lol tohle je doslova nenormalni
# prosim nepiste takovyhle kod, nikdo tomu nebude rozumet
# ale je to celkem flex
print(gen_primes(int(input("zadej cislo : "))))
# je to dost pomalejsi nez to predtim ale je to rozhodne vic cool
```

@ -0,0 +1,53 @@
# erastotenovo_sito.py
```python
# yeehaw
# budeme hledat prvocisla
# fakt super
# zacnu od udelani funkce ktera bere
# horni hranici intervalu a vrati mi seznam
# vsech prvocisel do tyto hranice
# generate_primes(10) ma vyhodit [2, 3, 5, 7]
def generate_primes(lim): # lim je horni hranice intervalu
nums = list(range(2, lim)) # seznam vsech cisel od 2 do lim
primes = [] # prazny seznam
x = nums[0] # ulozime si prvni cislo
# ktery je prvocislo
while x < int(lim**0.5): # staci ze hledam do odmocniny z lim
# teoreticky bych mohl napsat jenom
# while nums[0] < lim
# ale je to pomalejsi
# jo a int() mi z toho udela cele cislo
# coz odmocnina neni
x = nums[0] # aktualizace x, ma smysl az po prvnim cyklu
# prvocislo
temp = [] # temporary seznam do kteryho budeme kopirovat
primes.append(x) # ukladame prvocislo do seznamu prvocisel
for i in nums: # prochazime nums a snazime se rasove cistit
# cili nechat jenom cisla ktery nejsou delitelna nove pridanym
# prvocislem
if i % x:
temp.append(i) # temp nam slouzi jako budouci nums
nums = temp # pozirame temp a aktualizujeme nums
primes += nums # jinak by mi chybely cisla protoze jsem nahore
# napsal :
# while x< int(lim**0.5)
# kdybych to tam nenapsal tak tohle muzu vynechat
return primes # vracim cisla
print(generate_primes(int(input("zadej cislo : "))))
# tohle je mozna trochu prasacky
# mohl jsem to rozepsat jako :
# i = int(input("zadej cislo : "))
# print(generate_primes(i))
# ale nechtelo se mi lol
```

@ -0,0 +1,9 @@
# nejbigdick.py
```python
# tady te mam docente
def gp(n):
return [n[0]] + gp([i for i in n[1:] if i%n[0] != 0]) if n else [] # mnohem rychlejsi
print(gp(range(2, int(input('zadej cislo : ')))))
# touche
```

@ -0,0 +1,13 @@
# vic_bigdick_sito.py
```python
# Sire Kubisi, tady jsem vas dobehl
# Tohle single line sito je lepsi, kratsi a rychlejsi
# (Meril jsem to pomoci timeit)
from functools import reduce
def primes(n):
return reduce(lambda l, x: [y for y in l if y==x or y%x!=0], range(2, n), range(2, n))
print(primes(int(input("Zadej cislo: "))))
```

@ -8,20 +8,30 @@ Učí profesor Kubis a docent Veškrna.
+ [if](1/if)
+ [interpretace](1/interpretace)
+ [komentare](1/komentare)
+ [kricici_lukas](1/kricici_lukas)
+ [operace](1/operace)
+ [promenne](1/promenne)
+ [python_interpreter](1/python_interpreter)
+ [sude_nebo_liche](1/sude_nebo_liche)
+ [zakladni_funkce](1/zakladni_funkce)
## 2
+ [umocnovani](2/umocnovani)
+ [while](2/while)
## 3
+ [fizzbuzz](3/fizzbuzz)
+ [for](3/for)
+ [funkce](3/funkce)
+ [range](3/range)
+ [seznam](3/seznam)
## 4
+ [bigdick_sito](4/bigdick_sito)
+ [erastotenovo_sito](4/erastotenovo_sito)
+ [nejbigdick](4/nejbigdick)
+ [vic_bigdick_sito](4/vic_bigdick_sito)
## [5](5)
## [6](6)
@ -37,3 +47,7 @@ Učí profesor Kubis a docent Veškrna.
## [9](9)
##
+ [code_gen](code_gen)
+ [site_gen](site_gen)

@ -0,0 +1,23 @@
# code_gen.py
```python
#!/usr/bin/python3
from site_gen import get_ext
import os
def mkformat(name, lines):
result = []
result.append(f'# {name}\n')
result.append('```python\n')
result += lines
result.append('```')
return result
if __name__=='__main__':
files = [(i + '.py', i + '.md') for i in get_ext('.', '.py')]
for py, md in files:
with open(py) as p:
with open(md, 'w') as m:
m.writelines(mkformat(os.path.split(p.name)[1], p.readlines()))
```

@ -0,0 +1,20 @@
#!/usr/bin/python3
from site_gen import get_ext
import os
def mkformat(name, lines):
result = []
result.append(f'# {name}\n')
result.append('```python\n')
result += lines
result.append('```')
return result
if __name__=='__main__':
files = [(i + '.py', i + '.md') for i in get_ext('.', '.py')]
for py, md in files:
with open(py) as p:
with open(md, 'w') as m:
m.writelines(mkformat(os.path.split(p.name)[1], p.readlines()))

@ -0,0 +1,59 @@
# site_gen.py
```python
#!/usr/bin/python3
import os
from functools import reduce
def get_ext(path, ext):
results = []
for i in os.listdir(path):
if i[0] == '.':
continue
if os.path.isdir(i):
results = results + get_ext(os.path.join(path, i), ext)
else:
name, extension = os.path.splitext(i)
if extension == ext:
results.append(os.path.join(path, name))
return results
def mkformat(files):
result = []
files = [list(os.path.split(i)) for i in files]
files = path_dict(files)
for key in files:
if 'README' in files[key]:
result.append(f'## [{key}]({key})')
else:
result.append(f'## {key}')
for name in [i for i in sorted(files[key]) if i != 'README']:
result.append(f'+ [{name}]({os.path.join(key, name)})')
result.append('')
return result
def path_dict(files):
di = dict()
for x, y in files:
if not di.get(x):
di[x] = []
di[x].append(y)
return di
if __name__=='__main__':
prefix = []
if os.path.exists('PREFIX.md'):
with open('PREFIX.md') as p:
prefix = p.readlines()
files = sorted([i[2:] for i in get_ext('.', '.md') if i != './README' and i != './PREFIX'])
with open('README.md', 'w') as f:
to_write = mkformat(files)
to_write = [i + '\n' for i in to_write]
to_write = prefix + to_write
f.writelines(to_write)
```

@ -3,16 +3,16 @@
import os
from functools import reduce
def get_mds(path):
def get_ext(path, ext):
results = []
for i in os.listdir(path):
if i[0] == '.':
continue
if os.path.isdir(i):
results = results + get_mds(os.path.join(path, i))
results = results + get_ext(os.path.join(path, i), ext)
else:
name, extension = os.path.splitext(i)
if extension == ".md":
if extension == ext:
results.append(os.path.join(path, name))
return results
@ -47,7 +47,7 @@ if __name__=='__main__':
with open('PREFIX.md') as p:
prefix = p.readlines()
files = sorted([i[2:] for i in get_mds('.') if i != './README' and i != './PREFIX'])
files = sorted([i[2:] for i in get_ext('.', '.md') if i != './README' and i != './PREFIX'])
with open('README.md', 'w') as f:
to_write = mkformat(files)

Loading…
Cancel
Save