Skip to content

Commit

Permalink
Formatted repo with Black.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhthomas committed Aug 26, 2019
1 parent de1072c commit cc5690f
Show file tree
Hide file tree
Showing 48 changed files with 894 additions and 769 deletions.
39 changes: 22 additions & 17 deletions BST_nodes_in_range.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
# Problem: Find the no. of nodes in a BST
# Problem: Find the no. of nodes in a BST
# that lies in a given range


class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def __init__(self, data):
self.data = data
self.left = None
self.right = None


def nodesWithinRange(root, range):
low, high = range
if root is None:
return 0
elif root.data <= high and root.data >= low:
return 1 + nodesWithinRange(root.left, range) + nodesWithinRange(root.right, range)
elif root.data == high or root.data == low:
return 1
elif root.data > high:
return nodesWithinRange(root.left, range)
elif root.data < low:
return nodesWithinRange(root.right, range)

low, high = range
if root is None:
return 0
elif root.data <= high and root.data >= low:
return (
1 + nodesWithinRange(root.left, range) + nodesWithinRange(root.right, range)
)
elif root.data == high or root.data == low:
return 1
elif root.data > high:
return nodesWithinRange(root.left, range)
elif root.data < low:
return nodesWithinRange(root.right, range)


node = Node(10)
node.left = Node(5)
node.left.left = Node(1)
Expand Down
28 changes: 15 additions & 13 deletions binary_search_recursive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
# binary search. If the element is not found
# it returns -1


def binarySearch(lst, key, l, r):
if r>=l:
mid = l + (r-l) // 2
# use print(l, mid, r) to view the process
if lst[mid] == key:
return mid
elif lst[mid] < key:
return binarySearch(lst, key, mid+1, r)
elif lst[mid] > key:
return binarySearch(lst, key, l, mid-1)
else:
return -1

if r >= l:
mid = l + (r - l) // 2
# use print(l, mid, r) to view the process
if lst[mid] == key:
return mid
elif lst[mid] < key:
return binarySearch(lst, key, mid + 1, r)
elif lst[mid] > key:
return binarySearch(lst, key, l, mid - 1)
else:
return -1


arr = [int(i) for i in range(101)]
print(binarySearch(arr, 67, 0, len(arr)-1))
print(binarySearch(arr, 67, 0, len(arr) - 1))
27 changes: 16 additions & 11 deletions check_anagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@
# anagrams of each other

import string

letters = string.ascii_lowercase
CHARACTER_HASH = dict(zip(letters, [0]*len(letters)))
CHARACTER_HASH = dict(zip(letters, [0] * len(letters)))


def mapLettersToHash(text_a):
for char in text_a:
if char in CHARACTER_HASH.keys():
CHARACTER_HASH[char]+=1
for char in text_a:
if char in CHARACTER_HASH.keys():
CHARACTER_HASH[char] += 1


def computeCommonLetters(text_b):
common_letters = 0
for char in text_b:
if CHARACTER_HASH[char] > 0:
common_letters+=1
return common_letters
common_letters = 0
for char in text_b:
if CHARACTER_HASH[char] > 0:
common_letters += 1
return common_letters


def computeUncommonLetters(text_a, text_b, common_letters):
return abs(len(text_a)+len(text_b)-(2*common_letters))
return abs(len(text_a) + len(text_b) - (2 * common_letters))


text_1 = "hello"
text_2 = "billion"

mapLettersToHash(text_1)
common = computeCommonLetters(text_2)
result = computeUncommonLetters(text_1, text_2, common)
print(result)
print(result)
50 changes: 26 additions & 24 deletions check_semiprime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@
# Problem: Find the numbers which are semiprimes,
# within a given range. For e.g. 1 to 100.


def isSemiprime(num):
# start with the smallest prime
prime = 2
# initialize counter to 0
count = 0
# Design of while loop:
# 1. if count exceeds 2, it is not a semiprime, e.g. 30 = 2*3*5
# 2. when the number becomes 1, we have found the second prime
while(count<3 and num!=1):
# if the number is divisible by current prime,
# increment count, else move to new prime
if(not(num % prime)):
num = num / prime
count=count+1
else:
prime = prime + 1
# if count is two, given number is a semiprime
return count == 2

for i in range(1,100):
if(isSemiprime(i)):
print(i, end=' ')

# Result: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49
# 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95
# start with the smallest prime
prime = 2
# initialize counter to 0
count = 0
# Design of while loop:
# 1. if count exceeds 2, it is not a semiprime, e.g. 30 = 2*3*5
# 2. when the number becomes 1, we have found the second prime
while count < 3 and num != 1:
# if the number is divisible by current prime,
# increment count, else move to new prime
if not (num % prime):
num = num / prime
count = count + 1
else:
prime = prime + 1
# if count is two, given number is a semiprime
return count == 2


for i in range(1, 100):
if isSemiprime(i):
print(i, end=" ")

# Result: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49
# 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95
22 changes: 15 additions & 7 deletions dfs_bfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# 1. Depth First Search (DFS)
# 2. Breadth First Search (BFS)


def dfs_1(graph, start):
visited, stack = set(), [start]
while stack:
Expand All @@ -12,6 +13,7 @@ def dfs_1(graph, start):
stack.extend(graph[vertex] - visited)
return visited


def dfs_2(graph, start, visited=None):
if visited is None:
visited = set()
Expand All @@ -20,6 +22,7 @@ def dfs_2(graph, start, visited=None):
dfs_2(graph, next, visited)
return visited


def bfs(graph, start):
visited, queue = set(), [start]
while queue:
Expand All @@ -29,8 +32,10 @@ def bfs(graph, start):
queue.extend(graph[vertex] - visited)
return visited


# bfs(graph, 'A') # {'B', 'C', 'A', 'F', 'D', 'E'}


def dfs_paths(graph, start, goal):
stack = [(start, [start])]
while stack:
Expand All @@ -41,12 +46,15 @@ def dfs_paths(graph, start, goal):
else:
stack.append((next, path + [next]))

graph = {'A': set(['B', 'C']),
'B': set(['A', 'D', 'E']),
'C': set(['A', 'F']),
'D': set(['B']),
'E': set(['B', 'F']),
'F': set(['C', 'E'])}

result = list(dfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]
graph = {
"A": set(["B", "C"]),
"B": set(["A", "D", "E"]),
"C": set(["A", "F"]),
"D": set(["B"]),
"E": set(["B", "F"]),
"F": set(["C", "E"]),
}

result = list(dfs_paths(graph, "A", "F")) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]
print(result)
22 changes: 12 additions & 10 deletions find_m_to_last_llist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

from linked_list_data_structure import LinkedList


def findMToLast(l_list, m):
current = l_list.head
count = 0
current = l_list.head
count = 0

while current is not None and count < m:
count += 1
current = current.getNextNode()

while current is not None and count < m:
count+=1
current = current.getNextNode()
m_behind = l_list.head
while current.next_node is not None:
current = current.getNextNode()
m_behind = m_behind.getNextNode()

m_behind = l_list.head
while current.next_node is not None:
current = current.getNextNode()
m_behind = m_behind.getNextNode()
return m_behind

return m_behind

linked_list = LinkedList()
m_to_last = 3
Expand Down
12 changes: 7 additions & 5 deletions find_pairs_sum_k.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Given an array of numbers, find all the
# pairs of numbers which sum upto `k`


def find_pairs(num_array, k):
pairs_array = []
for num in num_array:
if (k-num) in num_array:
pairs_array.append((num, (k-num)))
return pairs_array
pairs_array = []
for num in num_array:
if (k - num) in num_array:
pairs_array.append((num, (k - num)))
return pairs_array


result = find_pairs([0, 14, 0, 4, 7, 8, 3, 5, 7], 11)
print(result)
14 changes: 7 additions & 7 deletions find_products_pair_k.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
def product_pair(arr, x):
arr_sorted = sorted(arr)
arr_sorted = sorted(arr)

for i in range(0, len(arr_sorted)):
sub_array = arr_sorted[i+1:]
if x // arr_sorted[i] in sub_array:
return True
for i in range(0, len(arr_sorted)):
sub_array = arr_sorted[i + 1 :]
if x // arr_sorted[i] in sub_array:
return True

return False
return False


arr = [10, 20, 9, 40]
arr = [10, 20, 9, 40]
x = 400

res = product_pair(arr, x)
Expand Down
32 changes: 20 additions & 12 deletions find_second_largest_in_binary_tree.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Given a binary tree, find the second largest
# node in it


class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def __init__(self, data):
self.data = data
self.left = None
self.right = None


def find_largest(root):
current = root
Expand All @@ -14,21 +16,27 @@ def find_largest(root):
return current.right.data
current = current.right


def find_second_largest(root):
if root is None or (root.left is None and root.right is None):
raise ValueError("Tree must atleast have 2 nodes")

current = root

while current is not None:
if(current.left is not None and current.right is None):
if current.left is not None and current.right is None:
return find_largest(current.left)

if(current.right is not None and current.right.left is None and current.right.right is None):

if (
current.right is not None
and current.right.left is None
and current.right.right is None
):
return current.data

current = current.right



node = Node(10)
node.left = Node(5)
node.left.left = Node(1)
Expand All @@ -37,4 +45,4 @@ def find_second_largest(root):
node.right.right = Node(100)

result = find_second_largest(node)
print(result) # prints 50
print(result) # prints 50
Loading

0 comments on commit cc5690f

Please sign in to comment.