Skip to content

Commit

Permalink
naming convention fixes in many files (keon#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
goswami-rahul authored Apr 22, 2018
1 parent 83023ff commit efb2c22
Show file tree
Hide file tree
Showing 43 changed files with 192 additions and 190 deletions.
4 changes: 2 additions & 2 deletions backtrack/array_sum_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def backtrack(constructed_sofar):
S = 7


def check_sum(N, *nums):
if sum(x for x in nums) == N:
def check_sum(n, *nums):
if sum(x for x in nums) == n:
return (True, nums)
else:
return (False, nums)
Expand Down
8 changes: 4 additions & 4 deletions calculator/math_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ def main():
simple user-interface
"""
print("\t\tCalculator\n\n")
userInput = input("expression or exit: ")
while userInput != "exit":
user_input = input("expression or exit: ")
while user_input != "exit":
try:
print("The result is {0}".format(evaluate(userInput)))
print("The result is {0}".format(evaluate(user_input)))
except Exception:
print("invalid syntax!")
userInput = input("expression or exit: ")
user_input = input("expression or exit: ")
print("program end")


Expand Down
12 changes: 6 additions & 6 deletions dfs/count_islands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ def num_islands(grid):
for i, row in enumerate(grid):
for j, col in enumerate(grid[i]):
if col == '1':
DFS(grid, i, j)
dfs(grid, i, j)
count += 1
return count


def DFS(grid, i, j):
def dfs(grid, i, j):
if (i < 0 or i >= len(grid)) or (j < 0 or len(grid[0])):
return
if grid[i][j] != '1':
return
grid[i][j] = '0'
DFS(grid, i+1, j)
DFS(grid, i-1, j)
DFS(grid, i, j+1)
DFS(grid, i, j-1)
dfs(grid, i+1, j)
dfs(grid, i-1, j)
dfs(grid, i, j+1)
dfs(grid, i, j-1)
18 changes: 9 additions & 9 deletions dfs/pacific_atlantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ def pacific_atlantic(matrix):
atlantic = [[False for _ in range (n)] for _ in range(m)]
pacific = [[False for _ in range (n)] for _ in range(m)]
for i in range(n):
DFS(pacific, matrix, float("-inf"), i, 0)
DFS(atlantic, matrix, float("-inf"), i, m-1)
dfs(pacific, matrix, float("-inf"), i, 0)
dfs(atlantic, matrix, float("-inf"), i, m-1)
for i in range(m):
DFS(pacific, matrix, float("-inf"), 0, i)
DFS(atlantic, matrix, float("-inf"), n-1, i)
dfs(pacific, matrix, float("-inf"), 0, i)
dfs(atlantic, matrix, float("-inf"), n-1, i)
for i in range(n):
for j in range(m):
if pacific[i][j] and atlantic[i][j]:
res.append([i, j])
return res

def DFS(grid, matrix, height, i, j):
def dfs(grid, matrix, height, i, j):
if i < 0 or i >= len(matrix) or j < 0 or j >= len(matrix[0]):
return
if grid[i][j] or matrix[i][j] < height:
return
grid[i][j] = True
DFS(grid, matrix, matrix[i][j], i-1, j)
DFS(grid, matrix, matrix[i][j], i+1, j)
DFS(grid, matrix, matrix[i][j], i, j-1)
DFS(grid, matrix, matrix[i][j], i, j+1)
dfs(grid, matrix, matrix[i][j], i-1, j)
dfs(grid, matrix, matrix[i][j], i+1, j)
dfs(grid, matrix, matrix[i][j], i, j-1)
dfs(grid, matrix, matrix[i][j], i, j+1)
12 changes: 6 additions & 6 deletions dfs/walls_and_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ def walls_and_gates(rooms):
for i in range(len(rooms)):
for j in range(len(rooms[0])):
if rooms[i][j] == 0:
DFS(rooms, i, j, 0)
dfs(rooms, i, j, 0)


def DFS(rooms, i, j, depth):
def dfs(rooms, i, j, depth):
if (i < 0 or i >= len(rooms)) or (j < 0 or j >= len(rooms[0])):
return # out of bounds
if rooms[i][j] < depth:
return # crossed
rooms[i][j] = depth
DFS(rooms, i+1, j, depth+1)
DFS(rooms, i-1, j, depth+1)
DFS(rooms, i, j+1, depth+1)
DFS(rooms, i, j-1, depth+1)
dfs(rooms, i+1, j, depth+1)
dfs(rooms, i-1, j, depth+1)
dfs(rooms, i, j+1, depth+1)
dfs(rooms, i, j-1, depth+1)
6 changes: 3 additions & 3 deletions dp/coin_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
So the output should be 5.
"""

def count(S, n):
def count(s, n):
# We need n+1 rows as the table is consturcted in bottom up
# manner using the base case 0 value case (n = 0)
m = len(S)
m = len(s)
table = [[0 for x in range(m)] for x in range(n+1)]

# Fill the enteries for 0 value case (n = 0)
Expand All @@ -26,7 +26,7 @@ def count(S, n):
for i in range(1, n+1):
for j in range(m):
# Count of solutions including S[j]
x = table[i - S[j]][j] if i-S[j] >= 0 else 0
x = table[i - s[j]][j] if i-s[j] >= 0 else 0

# Count of solutions excluding S[j]
y = table[i][j-1] if j >= 1 else 0
Expand Down
20 changes: 10 additions & 10 deletions dp/egg_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@

# Function to get minimum number of trials needed in worst
# case with n eggs and k floors
def eggDrop(n, k):
def egg_drop(n, k):
# A 2D table where entery eggFloor[i][j] will represent minimum
# number of trials needed for i eggs and j floors.
eggFloor = [[0 for x in range(k+1)] for x in range(n+1)]
egg_floor = [[0 for x in range(k+1)] for x in range(n+1)]

# We need one trial for one floor and0 trials for 0 floors
for i in range(1, n+1):
eggFloor[i][1] = 1
eggFloor[i][0] = 0
egg_floor[i][1] = 1
egg_floor[i][0] = 0

# We always need j trials for one egg and j floors.
for j in range(1, k+1):
eggFloor[1][j] = j
egg_floor[1][j] = j

# Fill rest of the entries in table using optimal substructure
# property
for i in range(2, n+1):
for j in range(2, k+1):
eggFloor[i][j] = INT_MAX
egg_floor[i][j] = INT_MAX
for x in range(1, j+1):
res = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x])
if res < eggFloor[i][j]:
eggFloor[i][j] = res
res = 1 + max(egg_floor[i-1][x-1], egg_floor[i][j-x])
if res < egg_floor[i][j]:
egg_floor[i][j] = res

# eggFloor[n][k] holds the result
return eggFloor[n][k]
return egg_floor[n][k]
10 changes: 5 additions & 5 deletions dp/job_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, start, finish, profit):
# returns -1 if all jobs before index conflict with it.
# The array jobs[] is sorted in increasing order of finish
# time.
def binarySearch(job, start_index):
def binary_search(job, start_index):

# Initialize 'lo' and 'hi' for Binary Search
lo = 0
Expand Down Expand Up @@ -51,12 +51,12 @@ def schedule(job):
for i in range(1, n):

# Find profit including the current job
inclProf = job[i].profit
l = binarySearch(job, i)
incl_prof = job[i].profit
l = binary_search(job, i)
if (l != -1):
inclProf += table[l];
incl_prof += table[l];

# Store maximum of including and excluding
table[i] = max(inclProf, table[i - 1])
table[i] = max(incl_prof, table[i - 1])

return table[n-1]
10 changes: 5 additions & 5 deletions dp/matrix_chain_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
INF = float("inf")

def matrix_chain_order(array):
N=len(array)
matrix = [[0 for x in range(N)] for x in range(N)]
sol = [[0 for x in range(N)] for x in range(N)]
for chain_length in range(2,N):
for a in range(1,N-chain_length+1):
n=len(array)
matrix = [[0 for x in range(n)] for x in range(n)]
sol = [[0 for x in range(n)] for x in range(n)]
for chain_length in range(2,n):
for a in range(1,n-chain_length+1):
b = a+chain_length-1

matrix[a][b] = INF
Expand Down
14 changes: 7 additions & 7 deletions dp/min_cost_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@

def min_cost(cost):

N = len(cost)
n = len(cost)
# dist[i] stores minimum cost from 0 --> i.
dist = [INF] * N
dist = [INF] * n

dist[0] = 0 # cost from 0 --> 0 is zero.

for i in range(N):
for j in range(i+1,N):
for i in range(n):
for j in range(i+1,n):
dist[j] = min(dist[j], dist[i] + cost[i][j])

return dist[N-1]
return dist[n-1]

if __name__ == '__main__':

cost = [ [ 0, 15, 80, 90], # cost[i][j] is the cost of
[-1, 0, 40, 50], # going from i --> j
[-1, -1, 0, 70],
[-1, -1, -1, 0] ] # cost[i][j] = -1 for i > j
N = len(cost)
total_len = len(cost)

mcost = min_cost(cost)
assert mcost == 65

print("The Minimum cost to reach station %d is %d" % (N, mcost))
print("The Minimum cost to reach station %d is %d" % (total_len, mcost))
2 changes: 1 addition & 1 deletion dp/regex_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import unittest

class Solution(object):
def isMatch(self, s, p):
def is_match(self, s, p):
m, n = len(s) + 1, len(p) + 1
matches = [[False] * n for _ in range(m)]

Expand Down
4 changes: 2 additions & 2 deletions dp/rod_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Returns the best obtainable price for a rod of length n and
# price[] as prices of different pieces
def cutRod(price):
def cut_rod(price):
n = len(price)
val = [0]*(n+1)

Expand All @@ -19,6 +19,6 @@ def cutRod(price):

# Driver program to test above functions
arr = [1, 5, 8, 9, 10, 17, 17, 20]
print("Maximum Obtainable Value is " + str(cutRod(arr)))
print("Maximum Obtainable Value is " + str(cut_rod(arr)))

# This code is contributed by Bhavya Jain
8 changes: 4 additions & 4 deletions graph/minimum_spanning_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, n):
for i in range(n):
self.parent[i] = i # Make all nodes his own parent, creating n sets.

def mergeSet(self, a, b):
def merge_set(self, a, b):
# Args:
# a, b (int): Indexes of nodes whose sets will be merged.

Expand All @@ -39,12 +39,12 @@ def mergeSet(self, a, b):
self.parent[b] = a # Merge set(b) and set(a)
self.size[a] += self.size[b] # Add size of old set(b) to set(a)

def findSet(self, a):
def find_set(self, a):
if self.parent[a] != a:
# Very important, memoize result of the
# recursion in the list to optimize next
# calls and make this operation practically constant, O(1)
self.parent[a] = self.findSet(self.parent[a])
self.parent[a] = self.find_set(self.parent[a])

# node <a> it's the set root, so we can return that index
return self.parent[a]
Expand Down Expand Up @@ -76,7 +76,7 @@ def kruskal(n, edges, ds):
set_u = ds.findSet(edge.u) # Set of the node <u>
set_v = ds.findSet(edge.v) # Set of the node <v>
if set_u != set_v:
ds.mergeSet(set_u, set_v)
ds.merge_set(set_u, set_v)
mst.append(edge)
if len(mst) == n-1:
# If we have selected n-1 edges, all the other
Expand Down
4 changes: 2 additions & 2 deletions heap/binary_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def min_child(self,i):
@abstractmethod
def remove_min(self,i):
pass
class Binary_Heap(AbstractHeap):
class BinaryHeap(AbstractHeap):
def __init__(self):
self.currentSize = 0
self.heap = [(0)]
Expand Down Expand Up @@ -120,7 +120,7 @@ class TestSuite(unittest.TestCase):
Test suite for the Binary_Heap data structures
"""
def setUp(self):
self.min_heap = Binary_Heap()
self.min_heap = BinaryHeap()
self.min_heap.insert(4)
self.min_heap.insert(50)
self.min_heap.insert(7)
Expand Down
2 changes: 1 addition & 1 deletion heap/merge_sorted_k_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, x):
self.next = None


def mergeKLists(lists):
def merge_k_lists(lists):
dummy = node = ListNode(0)
h = [(n.val, n) for n in lists if n]
heapify(h)
Expand Down
12 changes: 6 additions & 6 deletions heap/skyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@

import heapq

def get_skyline(LRH):
def get_skyline(lrh):
"""
Wortst Time Complexity: O(NlogN)
:type buildings: List[List[int]]
:rtype: List[List[int]]
"""
skyline, live = [], []
i, n = 0, len(LRH)
i, n = 0, len(lrh)
while i < n or live:
if not live or i < n and LRH[i][0] <= -live[0][1]:
x = LRH[i][0]
while i < n and LRH[i][0] == x:
heapq.heappush(live, (-LRH[i][2], -LRH[i][1]))
if not live or i < n and lrh[i][0] <= -live[0][1]:
x = lrh[i][0]
while i < n and lrh[i][0] == x:
heapq.heappush(live, (-lrh[i][2], -lrh[i][1]))
i += 1
else:
x = -live[0][1]
Expand Down
5 changes: 3 additions & 2 deletions linkedlist/is_palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def is_palindrome(head):
fast = fast.next.next
slow = slow.next
second = slow.next
slow.next = None # Don't forget here! But forget still works!
slow.next = None # Don't forget here! But forget still works!
# reverse the second part
node = None
while second:
Expand All @@ -24,6 +24,7 @@ def is_palindrome(head):
head = head.next
return True


def is_palindrome_stack(head):
if not head or not head.next:
return True
Expand Down Expand Up @@ -80,7 +81,7 @@ def is_palindrome_dict(head):
else:
step = 0
for i in range(0, len(v)):
if v[i] + v[len(v)-1-step] != checksum:
if v[i] + v[len(v) - 1 - step] != checksum:
return False
step += 1
if middle > 1:
Expand Down
Loading

0 comments on commit efb2c22

Please sign in to comment.