asdo
This commit is contained in:
parent
756f05dac2
commit
9d1cbff011
0
najkratsia_cesta.py
Normal file
0
najkratsia_cesta.py
Normal file
160
test/test.py
Normal file
160
test/test.py
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#hash tabulka(1)
|
||||||
|
|
||||||
|
def hash_insert(table, key):
|
||||||
|
n = len(table)
|
||||||
|
start = key % n
|
||||||
|
|
||||||
|
for step in range(n):
|
||||||
|
index = (start + step) % n
|
||||||
|
if table[index] == key:
|
||||||
|
return index
|
||||||
|
|
||||||
|
if table[index] is None:
|
||||||
|
table[index] = key
|
||||||
|
return index
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def hash_find(table, key):
|
||||||
|
n = len(table)
|
||||||
|
start = key % n
|
||||||
|
|
||||||
|
for step in range(n):
|
||||||
|
index = (start + step) % n
|
||||||
|
if table[index] == key:
|
||||||
|
return index
|
||||||
|
if table[index] is None:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
return -1
|
||||||
|
#RLE(5)
|
||||||
|
|
||||||
|
|
||||||
|
def rle_encode(text):
|
||||||
|
if not text:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
result = []
|
||||||
|
current_char = text[0]
|
||||||
|
counter = 1
|
||||||
|
|
||||||
|
for char in text [1:]:
|
||||||
|
if char == current_char:
|
||||||
|
count = count + 1
|
||||||
|
else :
|
||||||
|
result.append((current_char, count))
|
||||||
|
current_char = char
|
||||||
|
count = 1
|
||||||
|
result.append((current_char, count))
|
||||||
|
return result
|
||||||
|
|
||||||
|
def rle_decode(data):
|
||||||
|
result = ""
|
||||||
|
for char, count in data:
|
||||||
|
result += char * count
|
||||||
|
return result
|
||||||
|
|
||||||
|
#binarny strom
|
||||||
|
|
||||||
|
|
||||||
|
def bst_contains(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return False
|
||||||
|
if value == tree.value:
|
||||||
|
return True
|
||||||
|
if value < tree.value:
|
||||||
|
return bst_contains(tree.left, value)
|
||||||
|
else:
|
||||||
|
return bst_contains(tree.right, value)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def bst_insert(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return BSTNode(value)
|
||||||
|
if value < tree.value:
|
||||||
|
tree.left = bst_insert(tree.left, value)
|
||||||
|
elif value > tree.value:
|
||||||
|
tree.right = bst_insert(tree.right, value)
|
||||||
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def huffman_deocde(bits, codes):
|
||||||
|
if not bits:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
checked_Codes = {}
|
||||||
|
for char, code in codes.items():
|
||||||
|
checked_Codes[code] = char
|
||||||
|
|
||||||
|
result = ""
|
||||||
|
current = ""
|
||||||
|
for bit in bits:
|
||||||
|
current = current + bit
|
||||||
|
if current in checked_Codes:
|
||||||
|
result = result + checked_Codes[current]
|
||||||
|
current = ""
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def huffman_decode_tree(bits, tree):
|
||||||
|
result = ""
|
||||||
|
current = tree
|
||||||
|
for bit in bits:
|
||||||
|
if bit == "0":
|
||||||
|
current = current.left
|
||||||
|
else:
|
||||||
|
current = current.right
|
||||||
|
|
||||||
|
|
||||||
|
if current is None:
|
||||||
|
raise ValueError
|
||||||
|
if current.left is None and current.right is None:
|
||||||
|
result = result + current.symbol
|
||||||
|
current = tree
|
||||||
|
|
||||||
|
if current is not tree:
|
||||||
|
raise ValueError
|
||||||
|
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def min_coins(coins, amount):
|
||||||
|
inf = amount + 1
|
||||||
|
dp = [inf] * (amount + 1)
|
||||||
|
dp[0] = 0
|
||||||
|
for s in range(1, amount + 1):
|
||||||
|
for coin in coins:
|
||||||
|
if coin <= s and dp[s - coin] + 1 < dp[s]:
|
||||||
|
dp[s] = dp[s - coin] + 1
|
||||||
|
if dp[amount] == inf:
|
||||||
|
return -1
|
||||||
|
return dp[amount]
|
||||||
|
|
||||||
|
|
||||||
|
def max_non_adjacent(numbers):
|
||||||
|
if not numbers:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
n = len(numbers)
|
||||||
|
if n == 1:
|
||||||
|
return numbers[0]
|
||||||
|
|
||||||
|
dp = [0] * n
|
||||||
|
dp[0] = numbers[0]
|
||||||
|
dp[1] = max(numbers[0], numbers[1])
|
||||||
|
for i in range(2, n):
|
||||||
|
dp[i] = max(dp[i-1], dp[i - 2] + numbers[i])
|
||||||
|
return dp[n - 1]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
52
test/test2.py
Normal file
52
test/test2.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
def hash_insert(table, key):
|
||||||
|
n = len(table)
|
||||||
|
for i in range(n):
|
||||||
|
index = (key + i) % n
|
||||||
|
if table[index] == key:
|
||||||
|
return index
|
||||||
|
if table[index] is None:
|
||||||
|
table[index] = key
|
||||||
|
return index
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def hash_find(table, key):
|
||||||
|
n = len(table)
|
||||||
|
for i in range(n):
|
||||||
|
index = (key + i) % n
|
||||||
|
if table[index] == key:
|
||||||
|
return index
|
||||||
|
if table[index] is None:
|
||||||
|
return -1
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
def bst_contains(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return False
|
||||||
|
if value == tree.value:
|
||||||
|
return True
|
||||||
|
if value < tree.value:
|
||||||
|
return bst_contains(tree.left, value)
|
||||||
|
return bst_contains(tree.right, value)
|
||||||
|
|
||||||
|
|
||||||
|
def bst_insert(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return BSTNode(value)
|
||||||
|
if value < tree.value:
|
||||||
|
tree.left = bst_insert(tree.left, value)
|
||||||
|
elif value > tree.value:
|
||||||
|
tree.right = bst_insert(tree.right, value)
|
||||||
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
def build_huffman(freq):
|
||||||
|
trees = []
|
||||||
|
for ch in freq:
|
||||||
|
trees.append((freq[ch], ch, HuffmanNode(ch, None, None)))
|
||||||
|
while len(trees) > 1:
|
||||||
|
trees.sort()
|
||||||
|
w1, c1, left = trees.pop(0)
|
||||||
|
w2, c2, right = trees.pop(0)
|
||||||
|
trees.append((w1 + w2, min(c1, c2), HuffmanNode(None, left, right)))
|
||||||
|
return trees[0][2]
|
||||||
110
test3.py
Normal file
110
test3.py
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
|
||||||
|
class BSTNode:
|
||||||
|
def __init__(self, value, left=None, right=None):
|
||||||
|
self.value = value
|
||||||
|
self.left = left
|
||||||
|
self.right = right
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def hash_insert(table, key):
|
||||||
|
n = len(table)
|
||||||
|
for i in range(n):
|
||||||
|
index = (key + 1) % n
|
||||||
|
if table[index] == key:
|
||||||
|
return index
|
||||||
|
if table[index] is None:
|
||||||
|
table[index] = key
|
||||||
|
return index
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def hash_find(table, key):
|
||||||
|
n = len(table)
|
||||||
|
for i in range(n):
|
||||||
|
index = (key + 1) % n
|
||||||
|
if table[index] == key:
|
||||||
|
return key
|
||||||
|
if table[index] is None:
|
||||||
|
return -1
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def bst_contains(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return False
|
||||||
|
if value == tree.value:
|
||||||
|
return True
|
||||||
|
if value < tree.value:
|
||||||
|
return bst_contains(tree.left, value)
|
||||||
|
return bst_contains(tree.right, value)
|
||||||
|
|
||||||
|
|
||||||
|
def bst_insert(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return BSTNode(value)
|
||||||
|
if value < tree.value:
|
||||||
|
tree.left = bst_insert(tree.left, value)
|
||||||
|
elif value > tree.value:
|
||||||
|
tree.right = bst_insert(tree.right, value)
|
||||||
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
def bst_contains(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return False
|
||||||
|
if value == tree.value:
|
||||||
|
return True
|
||||||
|
if value < tree.value:
|
||||||
|
return bst_contains(tree.left, value)
|
||||||
|
return bst_contains(tree.right, value)
|
||||||
|
|
||||||
|
|
||||||
|
def bst_insert(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return BSTNode(value)
|
||||||
|
if value < tree.value:
|
||||||
|
tree.left = bst_insert(tree.left, value)
|
||||||
|
elif value > tree.value:
|
||||||
|
tree.right = bst_insert(tree.right, value)
|
||||||
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def huffman_decode(bits, codes):
|
||||||
|
res = ""
|
||||||
|
cur = ""
|
||||||
|
for b in bits:
|
||||||
|
cur = cur + b
|
||||||
|
for ch in codes:
|
||||||
|
if codes[ch] == cur:
|
||||||
|
res = res + ch
|
||||||
|
cur = ""
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def huffman_decode_tree(bits, tree):
|
||||||
|
res = ""
|
||||||
|
node = tree
|
||||||
|
for b in bits:
|
||||||
|
if b == "0":
|
||||||
|
node = node.left
|
||||||
|
else:
|
||||||
|
node = node.right
|
||||||
|
if node.symbol is not None:
|
||||||
|
res = res + node.symbol
|
||||||
|
node = tree
|
||||||
|
if node is not tree:
|
||||||
|
raise ValueError()
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def rle_encode(data):
|
||||||
|
res = ""
|
||||||
|
for ch, count in data:
|
||||||
|
res += ch * count
|
||||||
|
return res
|
||||||
105
test4.py
Normal file
105
test4.py
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
class BSTNode:
|
||||||
|
def __init__(self, value, left=None, right=None):
|
||||||
|
self.value = value
|
||||||
|
self.left = left
|
||||||
|
self.right = right
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def hash_insert(table, key):
|
||||||
|
n = len(table)
|
||||||
|
for i in range(n):
|
||||||
|
index = (key + i) % n
|
||||||
|
if table[index] == key:
|
||||||
|
return index
|
||||||
|
if table[index] is None:
|
||||||
|
table[index] = key
|
||||||
|
return index
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
def hash_find(table, key):
|
||||||
|
n = len(table)
|
||||||
|
for i in range(n):
|
||||||
|
index = (key + i) % n
|
||||||
|
if table[index] == key:
|
||||||
|
return index
|
||||||
|
if table[index] is None:
|
||||||
|
return -1
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def bst_contains(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return False
|
||||||
|
if value == tree.value:
|
||||||
|
return True
|
||||||
|
if value < tree.value:
|
||||||
|
return bst_contains(tree.left, value)
|
||||||
|
return bst_contains(tree.right, value)
|
||||||
|
|
||||||
|
|
||||||
|
def bst_insert(tree, value):
|
||||||
|
if tree is None:
|
||||||
|
return BSTNode(value)
|
||||||
|
if value < tree.value:
|
||||||
|
tree.left = bst_insert(tree.left, value)
|
||||||
|
elif value > tree.value:
|
||||||
|
tree.right = bst_insert(tree.right, value)
|
||||||
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def huffman_decode(bits, codes):
|
||||||
|
res = ""
|
||||||
|
cur = ""
|
||||||
|
for b in bits:
|
||||||
|
cur = cur + b
|
||||||
|
for ch in codes:
|
||||||
|
if codes[ch] == cur:
|
||||||
|
res = res + ch
|
||||||
|
cur = ""
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def huffman_decode_tree(bits, tree):
|
||||||
|
res = ""
|
||||||
|
node = tree
|
||||||
|
for b in bits:
|
||||||
|
if b == "0":
|
||||||
|
node = node.left
|
||||||
|
else:
|
||||||
|
node = node.right
|
||||||
|
if node.symbol is not None:
|
||||||
|
res = res + node.symbol
|
||||||
|
node = tree
|
||||||
|
if node is not tree:
|
||||||
|
raise ValueError()
|
||||||
|
return res
|
||||||
|
|
||||||
|
def rle_encode(text):
|
||||||
|
res = []
|
||||||
|
prev = ""
|
||||||
|
count = 0
|
||||||
|
for char in text:
|
||||||
|
if char == prev:
|
||||||
|
count += 1
|
||||||
|
elif count > 0:
|
||||||
|
res.append((prev, count))
|
||||||
|
prev = char
|
||||||
|
count = 1
|
||||||
|
if count > 0:
|
||||||
|
res.append((prev, count))
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def rle_encode(data):
|
||||||
|
res = ""
|
||||||
|
for char, count in data:
|
||||||
|
res += char * count
|
||||||
|
return res
|
||||||
53
u10test/test.py
Normal file
53
u10test/test.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
def shortest_path(number_of_nodes, connections):
|
||||||
|
if number_of_nodes == 1:
|
||||||
|
return [0]
|
||||||
|
|
||||||
|
# zoznam susedov
|
||||||
|
susedy = [[] for _ in range(number_of_nodes)]
|
||||||
|
for node_a, node_b, distance in connections:
|
||||||
|
susedy[node_a].append((node_b, distance))
|
||||||
|
|
||||||
|
susedy[node_b].append((node_a, distance)) # obojsmerne (+ node)
|
||||||
|
|
||||||
|
|
||||||
|
# init
|
||||||
|
distancii = [float("inf")] * number_of_nodes
|
||||||
|
paths = [[] for _ in range(number_of_nodes)]
|
||||||
|
|
||||||
|
boli = [False] * number_of_nodes
|
||||||
|
|
||||||
|
distancii[0] = 0
|
||||||
|
paths[0] = [0]
|
||||||
|
|
||||||
|
|
||||||
|
for _ in range(number_of_nodes):
|
||||||
|
teraz = -1
|
||||||
|
|
||||||
|
# najblizsi node
|
||||||
|
for node in range(number_of_nodes):
|
||||||
|
if not boli[node] and distancii[node] != float("inf"):
|
||||||
|
if teraz == -1:
|
||||||
|
teraz = node
|
||||||
|
|
||||||
|
elif (distancii[node], paths[node]) < (distancii[teraz], paths[teraz]):
|
||||||
|
teraz = node
|
||||||
|
|
||||||
|
|
||||||
|
if teraz == -1:
|
||||||
|
break
|
||||||
|
|
||||||
|
boli[teraz] = True
|
||||||
|
|
||||||
|
# check susedov
|
||||||
|
for sused, weight in susedy[teraz]:
|
||||||
|
if not boli[sused]:
|
||||||
|
|
||||||
|
new_dist = round(distancii[teraz] + weight, 9)
|
||||||
|
new_path = paths[teraz] + [sused]
|
||||||
|
|
||||||
|
if (new_dist, new_path) < (distancii[sused], paths[sused]):
|
||||||
|
distancii[sused] = new_dist # update dist + path
|
||||||
|
paths[sused] = new_path
|
||||||
|
|
||||||
|
|
||||||
|
return paths[number_of_nodes - 1]
|
||||||
13
u11/knapsack.py
Normal file
13
u11/knapsack.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
def maximum_value_with_items(maximum_weight, items):
|
||||||
|
dv = []
|
||||||
|
for _ in range(maximum_weight):
|
||||||
|
dv.append((0, []))
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
vaha = item["veight"]
|
||||||
|
hodnota = ["value"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return dv[maximum_weight]
|
||||||
Loading…
Reference in New Issue
Block a user