Compare commits
No commits in common. "main" and "v2" have entirely different histories.
51
u2/ list_ops.py
Normal file
51
u2/ list_ops.py
Normal file
@ -0,0 +1,51 @@
|
||||
def append(list1, list2):
|
||||
pusti = []
|
||||
for gns in list1:
|
||||
pusti.append(gns)
|
||||
for exc in list2:
|
||||
pusti.append(exc)
|
||||
return pusti
|
||||
|
||||
def concat(list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
pusti.extend(gns)
|
||||
return pusti
|
||||
|
||||
def filter(function, list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
if function(gns):
|
||||
pusti.append(gns)
|
||||
return pusti
|
||||
|
||||
def length(list):
|
||||
pocatok = 0
|
||||
for gns in list:
|
||||
pocatok = pocatok + 1
|
||||
return pocatok
|
||||
|
||||
def map(function, list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
function(gns)
|
||||
pusti.append(function(gns))
|
||||
return pusti
|
||||
|
||||
def foldl(function, accumulator, initial):
|
||||
accumulator = initial
|
||||
for gns in list:
|
||||
accumulator = function(accumulator, gns)
|
||||
return accumulator
|
||||
|
||||
def foldr(function, initial, acc):
|
||||
acc = initial
|
||||
for gns in list[::-1]:
|
||||
acc = function(gns, acc)
|
||||
return acc
|
||||
|
||||
def reverse(list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
pusti.append(gns)
|
||||
return pusti[::-1]
|
||||
@ -6,42 +6,46 @@ def append(list1, list2):
|
||||
pusti.append(exc)
|
||||
return pusti
|
||||
|
||||
def concat(lists):
|
||||
def concat(list):
|
||||
pusti = []
|
||||
for gns in lists:
|
||||
for gns in list:
|
||||
pusti.extend(gns)
|
||||
return pusti
|
||||
|
||||
def filter(function, list):
|
||||
def filter(function, list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
if function(gns):
|
||||
pusti.append(gns)
|
||||
return pusti
|
||||
|
||||
def length(list):
|
||||
def length(list):
|
||||
pocatok = 0
|
||||
for gns in list:
|
||||
pocatok = pocatok + 1
|
||||
return pocatok
|
||||
|
||||
def map(function, list):
|
||||
def map(function, list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
function(gns)
|
||||
pusti.append(function(gns))
|
||||
return pusti
|
||||
|
||||
def foldl(function, list, initial):
|
||||
def foldl(function, accumulator, initial):
|
||||
accumulator = initial
|
||||
for gns in list:
|
||||
accumulator = function(accumulator, gns)
|
||||
return accumulator
|
||||
|
||||
def foldr(function, list, initial):
|
||||
def foldr(function, initial, acc):
|
||||
acc = initial
|
||||
for gns in list[::-1]:
|
||||
acc = function(gns, acc)
|
||||
return acc
|
||||
|
||||
def reverse(list):
|
||||
return list[::-1]
|
||||
def reverse(list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
pusti.append(gns)
|
||||
return pusti[::-1]
|
||||
@ -1,47 +0,0 @@
|
||||
def append(list1, list2):
|
||||
pusti = []
|
||||
for gns in list1:
|
||||
pusti.append(gns)
|
||||
for exc in list2:
|
||||
pusti.append(exc)
|
||||
return pusti
|
||||
|
||||
def concat(lists):
|
||||
pusti = []
|
||||
for gns in lists:
|
||||
pusti.extend(gns)
|
||||
return pusti
|
||||
|
||||
def filter(function, list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
if function(gns):
|
||||
pusti.append(gns)
|
||||
return pusti
|
||||
|
||||
def length(list):
|
||||
pocatok = 0
|
||||
for gns in list:
|
||||
pocatok = pocatok + 1
|
||||
return pocatok
|
||||
|
||||
def map(function, list):
|
||||
pusti = []
|
||||
for gns in list:
|
||||
pusti.append(function(gns))
|
||||
return pusti
|
||||
|
||||
def foldl(function, list, initial):
|
||||
accumulator = initial
|
||||
for gns in list:
|
||||
accumulator = function(accumulator, gns)
|
||||
return accumulator
|
||||
|
||||
def foldr(function, list, initial):
|
||||
acc = initial
|
||||
for gns in list[::-1]:
|
||||
acc = function(acc, gns)
|
||||
return acc
|
||||
|
||||
def reverse(list):
|
||||
return list[::-1]
|
||||
@ -1,10 +0,0 @@
|
||||
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
|
||||
@ -1,11 +0,0 @@
|
||||
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
|
||||
@ -1,35 +0,0 @@
|
||||
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]
|
||||
@ -1,13 +0,0 @@
|
||||
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
|
||||
@ -1,13 +0,0 @@
|
||||
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
117
u7/hash_table.py
@ -1,117 +0,0 @@
|
||||
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
|
||||
|
||||
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
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
114
u8/program.py
@ -1,114 +0,0 @@
|
||||
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