Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3e7cdd0dc | ||
|
|
fb98ea4d71 | ||
|
|
0ee69b3bc8 | ||
|
|
042ad72a34 | ||
|
|
03f9440798 | ||
|
|
28d3a8a421 | ||
|
|
cf4210f117 | ||
|
|
c4e9adc88b | ||
|
|
e4099b8ab5 | ||
|
|
795c9c36c5 | ||
|
|
116620dc06 | ||
|
|
8ca348e0fd | ||
|
|
6e798c47e4 | ||
|
|
2ba988445a | ||
|
|
2407d7cbe1 | ||
|
|
e5ad06fc22 | ||
|
|
b8252195f1 | ||
|
|
7f81fa7032 | ||
|
|
b42417b424 | ||
|
|
8107a35409 | ||
|
|
5b720afa92 | ||
|
|
63a4107fd4 | ||
|
|
006641d515 | ||
|
|
7d30399dbd | ||
|
|
58c2991113 | ||
|
|
f83828a180 | ||
|
|
9c73711444 | ||
|
|
90c104bead | ||
|
|
bdd1eb5208 |
@ -6,46 +6,42 @@ def append(list1, list2):
|
|||||||
pusti.append(exc)
|
pusti.append(exc)
|
||||||
return pusti
|
return pusti
|
||||||
|
|
||||||
def concat(list):
|
def concat(lists):
|
||||||
pusti = []
|
pusti = []
|
||||||
for gns in list:
|
for gns in lists:
|
||||||
pusti.extend(gns)
|
pusti.extend(gns)
|
||||||
return pusti
|
return pusti
|
||||||
|
|
||||||
def filter(function, list):
|
def filter(function, list):
|
||||||
pusti = []
|
pusti = []
|
||||||
for gns in list:
|
for gns in list:
|
||||||
if function(gns):
|
if function(gns):
|
||||||
pusti.append(gns)
|
pusti.append(gns)
|
||||||
return pusti
|
return pusti
|
||||||
|
|
||||||
def length(list):
|
def length(list):
|
||||||
pocatok = 0
|
pocatok = 0
|
||||||
for gns in list:
|
for gns in list:
|
||||||
pocatok = pocatok + 1
|
pocatok = pocatok + 1
|
||||||
return pocatok
|
return pocatok
|
||||||
|
|
||||||
def map(function, list):
|
def map(function, list):
|
||||||
pusti = []
|
pusti = []
|
||||||
for gns in list:
|
for gns in list:
|
||||||
function(gns)
|
|
||||||
pusti.append(function(gns))
|
pusti.append(function(gns))
|
||||||
return pusti
|
return pusti
|
||||||
|
|
||||||
def foldl(function, accumulator, initial):
|
def foldl(function, list, initial):
|
||||||
accumulator = initial
|
accumulator = initial
|
||||||
for gns in list:
|
for gns in list:
|
||||||
accumulator = function(accumulator, gns)
|
accumulator = function(accumulator, gns)
|
||||||
return accumulator
|
return accumulator
|
||||||
|
|
||||||
def foldr(function, initial, acc):
|
def foldr(function, list, initial):
|
||||||
acc = initial
|
acc = initial
|
||||||
for gns in list[::-1]:
|
for gns in list[::-1]:
|
||||||
acc = function(gns, acc)
|
acc = function(gns, acc)
|
||||||
return acc
|
return acc
|
||||||
|
|
||||||
def reverse(list):
|
def reverse(list):
|
||||||
pusti = []
|
return list[::-1]
|
||||||
for gns in list:
|
|
||||||
pusti.append(gns)
|
|
||||||
return pusti[::-1]
|
|
||||||
@ -6,46 +6,42 @@ def append(list1, list2):
|
|||||||
pusti.append(exc)
|
pusti.append(exc)
|
||||||
return pusti
|
return pusti
|
||||||
|
|
||||||
def concat(list):
|
def concat(lists):
|
||||||
pusti = []
|
pusti = []
|
||||||
for gns in list:
|
for gns in lists:
|
||||||
pusti.extend(gns)
|
pusti.extend(gns)
|
||||||
return pusti
|
return pusti
|
||||||
|
|
||||||
def filter(function, list):
|
def filter(function, list):
|
||||||
pusti = []
|
pusti = []
|
||||||
for gns in list:
|
for gns in list:
|
||||||
if function(gns):
|
if function(gns):
|
||||||
pusti.append(gns)
|
pusti.append(gns)
|
||||||
return pusti
|
return pusti
|
||||||
|
|
||||||
def length(list):
|
def length(list):
|
||||||
pocatok = 0
|
pocatok = 0
|
||||||
for gns in list:
|
for gns in list:
|
||||||
pocatok = pocatok + 1
|
pocatok = pocatok + 1
|
||||||
return pocatok
|
return pocatok
|
||||||
|
|
||||||
def map(function, list):
|
def map(function, list):
|
||||||
pusti = []
|
pusti = []
|
||||||
for gns in list:
|
for gns in list:
|
||||||
function(gns)
|
|
||||||
pusti.append(function(gns))
|
pusti.append(function(gns))
|
||||||
return pusti
|
return pusti
|
||||||
|
|
||||||
def foldl(function, accumulator, initial):
|
def foldl(function, list, initial):
|
||||||
accumulator = initial
|
accumulator = initial
|
||||||
for gns in list:
|
for gns in list:
|
||||||
accumulator = function(accumulator, gns)
|
accumulator = function(accumulator, gns)
|
||||||
return accumulator
|
return accumulator
|
||||||
|
|
||||||
def foldr(function, initial, acc):
|
def foldr(function, list, initial):
|
||||||
acc = initial
|
acc = initial
|
||||||
for gns in list[::-1]:
|
for gns in list[::-1]:
|
||||||
acc = function(gns, acc)
|
acc = function(acc, gns)
|
||||||
return acc
|
return acc
|
||||||
|
|
||||||
def reverse(list):
|
def reverse(list):
|
||||||
pusti = []
|
return list[::-1]
|
||||||
for gns in list:
|
|
||||||
pusti.append(gns)
|
|
||||||
return pusti[::-1]
|
|
||||||
|
|||||||
10
u3/flatten_array.py
Normal file
10
u3/flatten_array.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
def flatten(iterable):
|
||||||
|
pusti = []
|
||||||
|
for gns in iterable:
|
||||||
|
if gns is None:
|
||||||
|
continue
|
||||||
|
elif isinstance(gns, (list, tuple)):
|
||||||
|
pusti.extend(flatten(gns))
|
||||||
|
else:
|
||||||
|
pusti.append(gns)
|
||||||
|
return pusti
|
||||||
11
u4/word_count.py
Normal file
11
u4/word_count.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
def count_words(phrase):
|
||||||
|
slovnik1 = {}
|
||||||
|
zbirac = phrase.lower()
|
||||||
|
for simbolii in r",._:;!?@#$%^&*()-+=/\|[]{}<>~`""":
|
||||||
|
zbirac = zbirac.replace(simbolii, " ")
|
||||||
|
slova1 = zbirac.split()
|
||||||
|
for gns in slova1:
|
||||||
|
cisteslovo = gns.strip("'")
|
||||||
|
if cisteslovo:
|
||||||
|
slovnik1[cisteslovo] = slovnik1.get(cisteslovo, 0) + 1
|
||||||
|
return slovnik1
|
||||||
35
u5/kindergarten_garden.py
Normal file
35
u5/kindergarten_garden.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
class NoSuchStudentException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Garden:
|
||||||
|
def __init__(self, diagram, students=None):
|
||||||
|
self.diagram = diagram
|
||||||
|
if students is None:
|
||||||
|
self.students = [
|
||||||
|
"Alena", "Boris", "Cyril", "Daniel", "Eva", "František",
|
||||||
|
"Gitka", "Helena", "Ivana", "Jozef", "Karol", "Ladislav"
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
self.students = sorted(students)
|
||||||
|
|
||||||
|
self.plant_names = {
|
||||||
|
"G": "Tráva",
|
||||||
|
"C": "Ďatelina",
|
||||||
|
"R": "Reďkovky",
|
||||||
|
"V": "Fialky"
|
||||||
|
}
|
||||||
|
|
||||||
|
def plants(self, student):
|
||||||
|
if student not in self.students:
|
||||||
|
raise NoSuchStudentException("Student not found")
|
||||||
|
|
||||||
|
index = self.students.index(student)
|
||||||
|
start = index * 2
|
||||||
|
|
||||||
|
rows = self.diagram.splitlines()
|
||||||
|
row1 = rows[0]
|
||||||
|
row2 = rows[1]
|
||||||
|
|
||||||
|
letters = row1[start:start+2] + row2[start:start+2]
|
||||||
|
|
||||||
|
return [self.plant_names[char] for char in letters]
|
||||||
13
u6/matching-brackets.py
Normal file
13
u6/matching-brackets.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
def is_paired(input_string):
|
||||||
|
spisok = []
|
||||||
|
slovnik = {')': '(', ']': '[', '}': '{'}
|
||||||
|
|
||||||
|
for gnx in input_string:
|
||||||
|
if gnx in "([{":
|
||||||
|
spisok.append(gnx)
|
||||||
|
elif gnx in ")]}":
|
||||||
|
if not spisok or spisok[-1] != slovnik[gnx]:
|
||||||
|
return False
|
||||||
|
spisok.pop()
|
||||||
|
|
||||||
|
return len(spisok) == 0
|
||||||
13
u6/matching_brackets.py
Normal file
13
u6/matching_brackets.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
def is_paired(input_string):
|
||||||
|
spisok = []
|
||||||
|
slovnik = {')': '(', ']': '[', '}': '{'}
|
||||||
|
|
||||||
|
for gnx in input_string:
|
||||||
|
if gnx in "([{":
|
||||||
|
spisok.append(gnx)
|
||||||
|
elif gnx in ")]}":
|
||||||
|
if not spisok or spisok[-1] != slovnik[gnx]:
|
||||||
|
return False
|
||||||
|
spisok.pop()
|
||||||
|
|
||||||
|
return len(spisok) == 0
|
||||||
117
u7/hash_table.py
Normal file
117
u7/hash_table.py
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
class HashTable:
|
||||||
|
def __init__(self, capacity=10):
|
||||||
|
if not isinstance(capacity, int) or isinstance(capacity, bool):
|
||||||
|
raise TypeError("Capacity must be an integer.")
|
||||||
|
if capacity <= 0:
|
||||||
|
raise ValueError("Capacity must be positive.")
|
||||||
|
|
||||||
|
self.capacity = capacity
|
||||||
|
self.empty = object()
|
||||||
|
self.deleted = object()
|
||||||
|
self.slots = [self.empty] * capacity
|
||||||
|
self.count = 0
|
||||||
|
|
||||||
|
def hash(self, key):
|
||||||
|
if not isinstance(key, str):
|
||||||
|
raise TypeError("Key must be a string.")
|
||||||
|
|
||||||
|
result = 0
|
||||||
|
for char in key:
|
||||||
|
result = result * 31 + ord(char)
|
||||||
|
return result % self.capacity
|
||||||
|
|
||||||
|
def probe(self, key):
|
||||||
|
start_idx = self.hash(key)
|
||||||
|
idx = start_idx
|
||||||
|
first_deleted = None
|
||||||
|
|
||||||
|
for _ in range(self.capacity):
|
||||||
|
slot = self.slots[idx]
|
||||||
|
if slot is self.empty:
|
||||||
|
return first_deleted if first_deleted is not None else idx
|
||||||
|
elif slot is self.deleted:
|
||||||
|
if first_deleted is None:
|
||||||
|
first_deleted = idx
|
||||||
|
elif slot[0] == key:
|
||||||
|
return idx
|
||||||
|
|
||||||
|
idx = (idx + 1) % self.capacity
|
||||||
|
|
||||||
|
if first_deleted is not None:
|
||||||
|
return first_deleted
|
||||||
|
raise RuntimeError("HashTable probe failed.")
|
||||||
|
|
||||||
|
def load_factor(self):
|
||||||
|
return float(self.count) / self.capacity
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return self.count
|
||||||
|
|
||||||
|
def __contains__(self, key):
|
||||||
|
if not isinstance(key, str):
|
||||||
|
raise TypeError("Key must be a string.")
|
||||||
|
try:
|
||||||
|
idx = self.probe(key)
|
||||||
|
slot = self.slots[idx]
|
||||||
|
return slot is not self.empty and slot is not self.deleted and slot[0] == key
|
||||||
|
except RuntimeError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
if not isinstance(key, str):
|
||||||
|
raise TypeError("Key must be a string.")
|
||||||
|
idx = self.probe(key)
|
||||||
|
slot = self.slots[idx]
|
||||||
|
if slot is not self.empty and slot is not self.deleted and slot[0] == key:
|
||||||
|
return slot[1]
|
||||||
|
raise KeyError(key)
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
if not isinstance(key, str):
|
||||||
|
raise TypeError("Key must be a string.")
|
||||||
|
|
||||||
|
is_new = not self.__contains__(key)
|
||||||
|
if is_new:
|
||||||
|
if (self.count + 1) / self.capacity > 0.7:
|
||||||
|
self._resize()
|
||||||
|
|
||||||
|
idx = self.probe(key)
|
||||||
|
slot = self.slots[idx]
|
||||||
|
|
||||||
|
if slot is self.empty or slot is self.deleted:
|
||||||
|
self.slots[idx] = (key, value)
|
||||||
|
self.count += 1
|
||||||
|
elif slot[0] == key:
|
||||||
|
self.slots[idx] = (key, value)
|
||||||
|
|
||||||
|
def __delitem__(self, key):
|
||||||
|
if not isinstance(key, str):
|
||||||
|
raise TypeError("Key must be a string.")
|
||||||
|
idx = self.probe(key)
|
||||||
|
slot = self.slots[idx]
|
||||||
|
if slot is not self.empty and slot is not self.deleted and slot[0] == key:
|
||||||
|
self.slots[idx] = self.deleted
|
||||||
|
self.count -= 1
|
||||||
|
else:
|
||||||
|
raise KeyError(key)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for slot in self.slots:
|
||||||
|
if slot is not self.empty and slot is not self.deleted:
|
||||||
|
yield slot[0]
|
||||||
|
|
||||||
|
def _resize(self):
|
||||||
|
old_slots = self.slots
|
||||||
|
self.capacity *= 2
|
||||||
|
self.slots = [self.empty] * self.capacity
|
||||||
|
self.count = 0
|
||||||
|
|
||||||
|
for slot in old_slots:
|
||||||
|
if slot is not self.empty and slot is not self.deleted:
|
||||||
|
key, value = slot
|
||||||
|
idx = self.probe(key)
|
||||||
|
self.slots[idx] = (key, value)
|
||||||
|
self.count += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
17
u7/is_paired.py
Normal file
17
u7/is_paired.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
def is_paired(input_string):
|
||||||
|
duzki = {
|
||||||
|
')': '(',
|
||||||
|
']': '[',
|
||||||
|
'}': '{'
|
||||||
|
}
|
||||||
|
|
||||||
|
zbirac = []
|
||||||
|
|
||||||
|
for simbol in input_string:
|
||||||
|
if simbol in duzki.values():
|
||||||
|
zbirac.append(simbol)
|
||||||
|
elif simbol in duzki:
|
||||||
|
if not zbirac or zbirac.pop() != duzki[simbol]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return len(zbirac) == 0
|
||||||
114
u8/program.py
Normal file
114
u8/program.py
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
|
||||||
|
def __init__(self, text):
|
||||||
|
self.text = text
|
||||||
|
self.right = None
|
||||||
|
self.left = None
|
||||||
|
self.otazka = text.endswith("?")
|
||||||
|
|
||||||
|
|
||||||
|
def para_stromov(lines, index):
|
||||||
|
if index >= len(lines):
|
||||||
|
raise ValueError("Chybna databaza")
|
||||||
|
|
||||||
|
text = lines[index]
|
||||||
|
if not text:
|
||||||
|
raise ValueError("Chybna databaza")
|
||||||
|
|
||||||
|
node = Node(text)
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
if node.otazka:
|
||||||
|
node.left, index = para_stromov(lines, index)
|
||||||
|
node.right, index = para_stromov(lines, index)
|
||||||
|
|
||||||
|
return node, index
|
||||||
|
|
||||||
|
|
||||||
|
def pocet_nodov(node):
|
||||||
|
if not node:
|
||||||
|
return 0, 0
|
||||||
|
|
||||||
|
if node.otazka:
|
||||||
|
a_left, n_left = pocet_nodov(node.left)
|
||||||
|
a_right, n_right = pocet_nodov(node.right)
|
||||||
|
return 1 + a_left + a_right, n_left + n_right
|
||||||
|
else:
|
||||||
|
return 0, 1
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
danii = sys.stdin.read()
|
||||||
|
if not danii:
|
||||||
|
print("Expert zo zoo to vie.")
|
||||||
|
print("Chybna databaza")
|
||||||
|
return
|
||||||
|
|
||||||
|
lines = danii.splitlines()
|
||||||
|
|
||||||
|
print("Expert zo zoo to vie.")
|
||||||
|
|
||||||
|
if not lines:
|
||||||
|
print("Chybna databaza")
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
prvi = lines.index("")
|
||||||
|
except ValueError:
|
||||||
|
print("Chybna databaza")
|
||||||
|
return
|
||||||
|
|
||||||
|
stromlines = lines[:prvi]
|
||||||
|
vibir = lines[prvi + 1 :]
|
||||||
|
|
||||||
|
if not stromlines:
|
||||||
|
print("Chybna databaza")
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
root, druhy = para_stromov(stromlines, 0)
|
||||||
|
except Exception:
|
||||||
|
print("Chybna databaza")
|
||||||
|
return
|
||||||
|
|
||||||
|
if druhy != len(stromlines):
|
||||||
|
print("Chybna databaza")
|
||||||
|
return
|
||||||
|
|
||||||
|
n_pocet, a_pocet = pocet_nodov(root)
|
||||||
|
|
||||||
|
print(f"Pocet otazok: {n_pocet}")
|
||||||
|
print(f"Pocet odpovedi: {a_pocet}")
|
||||||
|
print("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.")
|
||||||
|
|
||||||
|
potocni = root
|
||||||
|
viber_indx = 0
|
||||||
|
|
||||||
|
while potocni.otazka:
|
||||||
|
print(potocni.text)
|
||||||
|
|
||||||
|
if viber_indx >= len(vibir):
|
||||||
|
print("Koniec vstupu")
|
||||||
|
return
|
||||||
|
|
||||||
|
choice = vibir[viber_indx]
|
||||||
|
viber_indx += 1
|
||||||
|
|
||||||
|
if choice != "a" and choice != "n":
|
||||||
|
print("Nerozumiem")
|
||||||
|
return
|
||||||
|
|
||||||
|
if choice == "a":
|
||||||
|
potocni = potocni.left
|
||||||
|
else:
|
||||||
|
potocni = potocni.right
|
||||||
|
|
||||||
|
print(potocni.text)
|
||||||
|
print("Koniec")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in New Issue
Block a user