Skip to content

Instantly share code, notes, and snippets.

View Sukhrobjon's full-sized avatar
🎯
Focusing

Sukhrobjon Golibboev Sukhrobjon

🎯
Focusing
View GitHub Profile
def evaluate(self, node=None) -> float:
"""
Calculate this tree expression recursively
Args:
node(BinaryTreeNode): starts at the root node
"""
# initialize
if node is None:
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)
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}
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
# 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]
#!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):
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
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
@Sukhrobjon
Sukhrobjon / dict_set.py
Last active May 8, 2019 21:41
This is sample code for medium article for CS-1.3 class at Make School
# 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']
# 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}