This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evaluate(self, node=None) -> float: | |
""" | |
Calculate this tree expression recursively | |
Args: | |
node(BinaryTreeNode): starts at the root node | |
""" | |
# initialize | |
if node is None: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insert(self, expression: str): | |
""" | |
Insert the postfix expression into the tree using stack | |
""" | |
postfix_exp = self.infix_to_postfix(expression) | |
# if max size is 0, then it is infinite | |
stack = deque() | |
char = postfix_exp[0] | |
# create a node for the first element of the expression | |
node = BinaryTreeNode(char) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def infix_to_postfix(self, infix_input: list) -> list: | |
""" | |
Converts infix expression to postfix. | |
Args: | |
infix_input(list): infix expression user entered | |
""" | |
# precedence order and associativity helps to determine which | |
# expression is needs to be calculated first | |
precedence_order = {'+': 0, '-': 0, '*': 1, '/': 1, '^': 2} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_fastest_route(self, from_vertex, to_vertex): | |
"""Finds for the shortest path from vertex a to b using | |
Dijkstra's algorithm: | |
https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm | |
Args: | |
from_vertex (str) : Starting point on the graph | |
to_vertex (str) : The final distanation | |
Returns: | |
shortest path (tuple): List of vertices in the path and len | |
Empty list if path does not exist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Longest Substring: https://leetcode.com/problems/longest-substring-without-repeating-characters/ | |
def optimized_longest_substring(word): | |
seen = {} | |
# index is current index, starter is left pointer | |
# to keep track of the start of the sub string | |
index = starter = longest = 0 | |
while index < len(word): | |
curr_char = word[index] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python | |
from linkedlist import LinkedList | |
# Implement LinkedStack below, then change the assignment at the bottom | |
# to use this Stack implementation to verify it passes all tests | |
class LinkedStack(object): | |
def __init__(self, iterable=None): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_index(text, pattern): | |
""" | |
Return the starting index of the first occurrence of pattern in text, | |
or None if not found. | |
""" | |
# if pattern is empty | |
if pattern == "": | |
return 0 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_index(text, pattern): | |
""" | |
Return the starting index of the first occurrence of pattern in text, | |
or None if not found. | |
""" | |
window = len(pattern) | |
if len(pattern) == 0: | |
return 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# declaring the dictionary with keys and values | |
dict_nums = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5} | |
print(dict_nums) # {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5} | |
# extracting the keys and putting in a list | |
list_keys = list(dict_nums.keys()) | |
print(list_keys) # ['one', 'two', 'three', 'four', 'five'] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# list of number | |
number_arr = [1, 2, 3, 3, 4, 2] | |
print(number_arr) # [1, 2, 3, 3, 4, 2] | |
# we can wrap the same list of numbers in a set | |
number_set = set([1, 2, 3, 3, 4, 2]) | |
# if we print it returns only distinct numbers | |
print(number_set) # {1, 2, 3, 4} |
NewerOlder