Fixed all noted camelcase in function/variable/file names and made minor spelling edits by herinckc · Pull Request #340 · keon/algorithms · GitHub
Skip to content
2 changes: 1 addition & 1 deletion algorithms/arrays/max_ones_index.py
2 changes: 1 addition & 1 deletion algorithms/calculator/math_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ def main():


if __name__ == "__main__":
main()
main()
2 changes: 1 addition & 1 deletion algorithms/dfs/sudoku_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ def test_sudoku_solver(self):


if __name__ == "__main__":
unittest.main()
unittest.main()
2 changes: 1 addition & 1 deletion algorithms/dp/fib.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ def fib_iter(n):
return sum

# => 354224848179261915075
# print(fib_iter(100))
# print(fib_iter(100))
4 changes: 2 additions & 2 deletions algorithms/maths/base_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import string

def int2base(n, base):
def int_to_base(n, base):
"""
:type n: int
:type base: int
Expand All @@ -31,7 +31,7 @@ def int2base(n, base):
return res[::-1]


def base2int(s, base):
def base_to_int(s, base):
"""
Note : You can use int() built-in function instread of this.
:type s: str
Expand Down
4 changes: 2 additions & 2 deletions algorithms/search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .binary_search import *
from .first_occurance import *
from .last_occurance import *
from .first_occurrence import *
from .last_occurrence import *
from .linear_search import *
from .search_insert import *
from .two_sum import *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Approach- Binary Search
# T(n)- O(log n)
#
def first_occurance(array, query):
def first_occurrence(array, query):
lo, hi = 0, len(array) - 1
while lo <= hi:
mid = (lo + hi) // 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Approach- Binary Search
# T(n)- O(log n)
#
def last_occurance(array, query):
def last_occurrence(array, query):
lo, hi = 0, len(array) - 1
while lo <= hi:
mid = (hi + lo) // 2
Expand Down
14 changes: 7 additions & 7 deletions algorithms/stack/longest_abs_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ def length_longest_path(input):
:type input: str
:rtype: int
"""
currlen, maxlen = 0, 0 # running length and max length
curr_len, max_len = 0, 0 # running length and max length
stack = [] # keep track of the name length
for s in input.split('\n'):
print("---------")
print("<path>:", s)
depth = s.count('\t') # the depth of current dir or file
print("depth: ", depth)
print("stack: ", stack)
print("curlen: ", currlen)
print("curlen: ", curr_len)
while len(stack) > depth: # go back to the correct depth
currlen -= stack.pop()
curr_len -= stack.pop()
stack.append(len(s.strip('\t'))+1) # 1 is the length of '/'
currlen += stack[-1] # increase current length
curr_len += stack[-1] # increase current length
print("stack: ", stack)
print("curlen: ", currlen)
print("curlen: ", curr_len)
if '.' in s: # update maxlen only when it is a file
maxlen = max(maxlen, currlen-1) # -1 is to minus one '/'
return maxlen
max_len = max(max_len, curr_len-1) # -1 is to minus one '/'
return max_len

st= "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdirectory1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
st2 = "a\n\tb1\n\t\tf1.txt\n\taaaaa\n\t\tf2.txt"
Expand Down
6 changes: 3 additions & 3 deletions algorithms/strings/make_sentence.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
count = 0


def make_sentence(str_piece, dictionarys):
def make_sentence(str_piece, dictionaries):
global count
if len(str_piece) == 0:
return True
for i in range(0, len(str_piece)):
prefix, suffix = str_piece[0:i], str_piece[i:]
if prefix in dictionarys:
if suffix in dictionarys or make_sentence(suffix, dictionarys):
if prefix in dictionaries:
if suffix in dictionaries or make_sentence(suffix, dictionaries):
count += 1
return True
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ def __init__(self, val = 0):
self.left = None
self.right = None

def bintree2list(root):
def bin_tree_to_list(root):
"""
type root: root class
"""
if not root:
return root
root = bintree2list_util(root)
root = bin_tree_to_list_util(root)
while root.left:
root = root.left
return root

def bintree2list_util(root):
def bin_tree_to_list_util(root):
if not root:
return root
if root.left:
left = bintree2list_util(root.left)
left = bin_tree_to_list_util(root.left)
while left.right:
left = left.right
left.right = root
root.left = left
if root.right:
right = bintree2list_util(root.right)
right = bin_tree_to_list_util(root.right)
while right.left:
right = right.left
right.left = root
Expand All @@ -45,5 +45,5 @@ def print_tree(root):
tree.left.right = Node(30)
tree.right.left = Node(36)

head = bintree2list(tree)
head = bin_tree_to_list(tree)
print_tree(head)
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ def __init__(self, x):
self.right = None


def array2bst(nums):
def array_to_bst(nums):
if not nums:
return None
mid = len(nums)//2
node = TreeNode(nums[mid])
node.left = array2bst(nums[:mid])
node.right = array2bst(nums[mid+1:])
node.left = array_to_bst(nums[:mid])
node.right = array_to_bst(nums[mid+1:])
return node
14 changes: 7 additions & 7 deletions algorithms/tree/longest_consecutive.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ def longest_consecutive(root):
"""
if not root:
return 0
maxlen = 0
dfs(root, 0, root.val, maxlen)
return maxlen
max_len = 0
dfs(root, 0, root.val, max_len)
return max_len


def dfs(root, cur, target, maxlen):
def dfs(root, cur, target, max_len):
if not root:
return
if root.val == target:
cur += 1
else:
cur = 1
maxlen = max(cur, maxlen)
dfs(root.left, cur, root.val+1, maxlen)
dfs(root.right, cur, root.val+1, maxlen)
max_len = max(cur, max_len)
dfs(root.left, cur, root.val+1, max_len)
dfs(root.right, cur, root.val+1, max_len)
2 changes: 1 addition & 1 deletion algorithms/tree/red_black_tree/red_black_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def delete(self, node):
node_min.left = node.left
node_min.left.parent = node_min
node_min.color = node.color
# when node is black ,then need to fix it with 4 cases
# when node is black, then need to fix it with 4 cases
if node_color == 0:
self.delete_fixup(temp_node)

Expand Down
6 changes: 3 additions & 3 deletions algorithms/tree/segment_tree/segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def __init__(self,arr,function):
self.fn = function
self.maketree(0,0,len(arr)-1)

def maketree(self,i,l,r):
def make_tree(self,i,l,r):
if l==r:
self.segment[i] = self.arr[l]
elif l<r:
self.maketree(2*i+1,l,int((l+r)/2))
self.maketree(2*i+2,int((l+r)/2)+1,r)
self.make_tree(2*i+1,l,int((l+r)/2))
self.make_tree(2*i+2,int((l+r)/2)+1,r)
self.segment[i] = self.fn(self.segment[2*i+1],self.segment[2*i+2])

def __query(self,i,L,R,l,r):
Expand Down
14 changes: 7 additions & 7 deletions tests/test_maths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from algorithms.maths import (
int2base, base2int,
int_to_base, base_to_int,
extended_gcd,
factorial, factorial_recur,
gcd, lcm,
Expand All @@ -26,14 +26,14 @@ class TestBaseConversion(unittest.TestCase):
"""

def test_int2base(self):
self.assertEqual("101", int2base(5, 2))
self.assertEqual("0", int2base(0, 2))
self.assertEqual("FF", int2base(255, 16))
self.assertEqual("101", int_to_base(5, 2))
self.assertEqual("0", int_to_base(0, 2))
self.assertEqual("FF", int_to_base(255, 16))

def test_base2int(self):
self.assertEqual(5, base2int("101", 2))
self.assertEqual(0, base2int("0", 2))
self.assertEqual(255, base2int("FF", 16))
self.assertEqual(5, base_to_int("101", 2))
self.assertEqual(0, base_to_int("0", 2))
self.assertEqual(255, base_to_int("FF", 16))


class TestExtendedGcd(unittest.TestCase):
Expand Down
30 changes: 15 additions & 15 deletions tests/test_search.py