160 lines
3.1 KiB
Python
160 lines
3.1 KiB
Python
|
|
|
|
#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]
|
|
|
|
|
|
|