mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Set the Python file maximum line length to 88 characters (#2122)
* flake8 --max-line-length=88
* fixup! Format Python code with psf/black push
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
class Heap:
|
||||
"""A generic Heap class, can be used as min or max by passing the key function accordingly.
|
||||
"""
|
||||
A generic Heap class, can be used as min or max by passing the key function
|
||||
accordingly.
|
||||
"""
|
||||
|
||||
def __init__(self, key=None):
|
||||
@@ -9,7 +11,8 @@ class Heap:
|
||||
self.pos_map = {}
|
||||
# Stores current size of heap.
|
||||
self.size = 0
|
||||
# Stores function used to evaluate the score of an item on which basis ordering will be done.
|
||||
# Stores function used to evaluate the score of an item on which basis ordering
|
||||
# will be done.
|
||||
self.key = key or (lambda x: x)
|
||||
|
||||
def _parent(self, i):
|
||||
@@ -41,7 +44,10 @@ class Heap:
|
||||
return self.arr[i][1] < self.arr[j][1]
|
||||
|
||||
def _get_valid_parent(self, i):
|
||||
"""Returns index of valid parent as per desired ordering among given index and both it's children"""
|
||||
"""
|
||||
Returns index of valid parent as per desired ordering among given index and
|
||||
both it's children
|
||||
"""
|
||||
left = self._left(i)
|
||||
right = self._right(i)
|
||||
valid_parent = i
|
||||
@@ -87,8 +93,8 @@ class Heap:
|
||||
self.arr[index] = self.arr[self.size - 1]
|
||||
self.pos_map[self.arr[self.size - 1][0]] = index
|
||||
self.size -= 1
|
||||
# Make sure heap is right in both up and down direction.
|
||||
# Ideally only one of them will make any change- so no performance loss in calling both.
|
||||
# Make sure heap is right in both up and down direction. Ideally only one
|
||||
# of them will make any change- so no performance loss in calling both.
|
||||
if self.size > index:
|
||||
self._heapify_up(index)
|
||||
self._heapify_down(index)
|
||||
@@ -109,7 +115,10 @@ class Heap:
|
||||
return self.arr[0] if self.size else None
|
||||
|
||||
def extract_top(self):
|
||||
"""Returns top item tuple (Calculated value, item) from heap and removes it as well if present"""
|
||||
"""
|
||||
Return top item tuple (Calculated value, item) from heap and removes it as well
|
||||
if present
|
||||
"""
|
||||
top_item_tuple = self.get_top()
|
||||
if top_item_tuple:
|
||||
self.delete_item(top_item_tuple[0])
|
||||
|
||||
@@ -126,7 +126,8 @@ class SkipList(Generic[KT, VT]):
|
||||
"""
|
||||
:param key: Searched key,
|
||||
:return: Tuple with searched node (or None if given key is not present)
|
||||
and list of nodes that refer (if key is present) of should refer to given node.
|
||||
and list of nodes that refer (if key is present) of should refer to
|
||||
given node.
|
||||
"""
|
||||
|
||||
# Nodes with refer or should refer to output node
|
||||
@@ -141,7 +142,8 @@ class SkipList(Generic[KT, VT]):
|
||||
# in skipping searched key.
|
||||
while i < node.level and node.forward[i].key < key:
|
||||
node = node.forward[i]
|
||||
# Each leftmost node (relative to searched node) will potentially have to be updated.
|
||||
# Each leftmost node (relative to searched node) will potentially have to
|
||||
# be updated.
|
||||
update_vector.append(node)
|
||||
|
||||
update_vector.reverse() # Note that we were inserting values in reverse order.
|
||||
|
||||
@@ -49,10 +49,8 @@ def infix_2_postfix(Infix):
|
||||
else:
|
||||
if len(Stack) == 0:
|
||||
Stack.append(x) # If stack is empty, push x to stack
|
||||
else:
|
||||
while (
|
||||
len(Stack) > 0 and priority[x] <= priority[Stack[-1]]
|
||||
): # while priority of x is not greater than priority of element in the stack
|
||||
else: # while priority of x is not > priority of element in the stack
|
||||
while len(Stack) > 0 and priority[x] <= priority[Stack[-1]]:
|
||||
Postfix.append(Stack.pop()) # pop stack & add to Postfix
|
||||
Stack.append(x) # push x to stack
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"""
|
||||
A Trie/Prefix Tree is a kind of search tree used to provide quick lookup
|
||||
of words/patterns in a set of words. A basic Trie however has O(n^2) space complexity
|
||||
making it impractical in practice. It however provides O(max(search_string, length of longest word))
|
||||
lookup time making it an optimal approach when space is not an issue.
|
||||
making it impractical in practice. It however provides O(max(search_string, length of
|
||||
longest word)) lookup time making it an optimal approach when space is not an issue.
|
||||
"""
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user