Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Jun 4, 2022
1 parent 3da897a commit 0a9bf8a
Show file tree
Hide file tree
Showing 37 changed files with 1,028 additions and 735 deletions.
15 changes: 7 additions & 8 deletions array-is-subset/array-is-subset.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
array1 = [1,2,3]
array2 = [7,8,9,1,2,3]
array1 = [1, 2, 3]
array2 = [7, 8, 9, 1, 2, 3]

# Todo: we have to determine if array 1 is a subset of array2

# Solution 1: Brute force


def is_subset(array1, array2):
# iterate through both, continue if we have matches

Expand All @@ -16,21 +17,19 @@ def is_subset(array1, array2):

# If they don't match, continue to next in array 2
if array1[a1] != array2[a2]:
a2+=1
a2 += 1

# If they do match, take one step in both
elif array1[a1] == array2[a2]:

# if we are at the last index
if a1 == len(array1) -1:
if a1 == len(array1) - 1:
return True

a2+=1
a1+=1
a2 += 1
a1 += 1

return False


print(is_subset(array1, array2))


36 changes: 18 additions & 18 deletions bfs/bfs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env python

# Driver code
# Driver code

class Graph:

class Graph:
def __init__(self):

# We will store the graph in a dict
self.graph = dict()
self.graph = dict()

def add_edge(self, node1, node2):
if node1 not in self.graph:
Expand All @@ -20,30 +20,30 @@ def bfs(self, source):

# Add the source to the queue first
queue = [source]
visited = {node:False for node in self.graph}
visited = {node: False for node in self.graph}
visited[source] = True

# While we still have nodes in the queue
while queue:

source = queue.pop(0)
print (source, end = " ")
print(source, end=" ")

# We could keep a list, but hash (dict) is faster for lookup
for node in self.graph[source]:
if visited[node] == False:
queue.append(node)

for node in self.graph[source]:
if visited[node] == False:
queue.append(node)
visited[node] = True


# Create a graph
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)

g.bfs(2)
4 changes: 2 additions & 2 deletions bisect/bisect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

def left_bisect(sorted_list, element):
"""Find an element in a list (from left side)
"""
Expand All @@ -15,7 +14,8 @@ def left_bisect(sorted_list, element):
elif middle >= element:
idxRight = idxMiddle
return idxLeft



def right_bisect(sorted_list, element):
"""
Find an element in a list (from the right)
Expand Down
11 changes: 6 additions & 5 deletions bubble-sort/bubble-sort.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

numbers = [1,2,3,4,5,8,3,2,3,1,1,1]
numbers = [1, 2, 3, 4, 5, 8, 3, 2, 3, 1, 1, 1]


def bubblesort(numbers):
Expand All @@ -24,11 +24,12 @@ def bubblesort(numbers):
numbers[indexA] = numbers[indexB]
numbers[indexB] = holder
switched = True

indexA+=1
indexB+=1

print('Sorted numbers are %s' % numbers)
indexA += 1
indexB += 1

print("Sorted numbers are %s" % numbers)
return numbers


bubblesort(numbers)
45 changes: 23 additions & 22 deletions compare-linked-list/compare.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Compare two strings represented as linked lists
# Given two linked lists, represented as linked lists (every character is a node in linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are same, 1 if first linked list is lexicographically greater, and -1 if second string is lexicographically greater.

Expand All @@ -17,13 +16,14 @@
# Output: 0


class Node:
# Constructor to create a new node
def __init__(self, char):
class Node:

# Constructor to create a new node
def __init__(self, char):
self.c = char
self.next = None


def compare(str1, str2):

# Case 1: both strings are the same, return 0
Expand All @@ -36,7 +36,7 @@ def compare(str1, str2):
str2 = str2.next

# When we get here, if both are still defined
if (str1 and str2):
if str1 and str2:
if str1.c > str2.c:
return 1
return -1
Expand All @@ -50,20 +50,21 @@ def compare(str1, str2):

return 0

# Driver program

list1 = Node('g')
list1.next = Node('e')
list1.next.next = Node('e')
list1.next.next.next = Node('k')
list1.next.next.next.next = Node('s')
list1.next.next.next.next.next = Node('b')

list2 = Node('g')
list2.next = Node('e')
list2.next.next = Node('e')
list2.next.next.next = Node('k')
list2.next.next.next.next = Node('s')
list2.next.next.next.next.next = Node('a')


# Driver program

list1 = Node("g")
list1.next = Node("e")
list1.next.next = Node("e")
list1.next.next.next = Node("k")
list1.next.next.next.next = Node("s")
list1.next.next.next.next.next = Node("b")

list2 = Node("g")
list2.next = Node("e")
list2.next.next = Node("e")
list2.next.next.next = Node("k")
list2.next.next.next.next = Node("s")
list2.next.next.next.next.next = Node("a")

print(compare(list1, list2))
48 changes: 25 additions & 23 deletions count-triples/count.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Given an array of distinct integers and a sum value. Find count of triplets
# Given an array of distinct integers and a sum value. Find count of triplets
# with sum smaller than given sum value. Expected Time Complexity is O(n2).

sum_value = 2
Expand All @@ -14,55 +14,57 @@ def count_triples(array, sum_value):
# If no triples, return 0
if len(array) < 3:
return count

# Here we know the array is 3 or more

# Fix the first number
for F in range(0, len(array) - 2):
# Fix the second number
for S in range(F+1, len(array) -1):
# Fix the last number
for L in range(S+1, len(array)):
# If the sum is less than sum_value, add to count
if (array[F] + array[S] + array[L]) < sum_value:
count+=1
for S in range(F + 1, len(array) - 1):
# Fix the last number
for L in range(S + 1, len(array)):
# If the sum is less than sum_value, add to count
if (array[F] + array[S] + array[L]) < sum_value:
count += 1

return count


print(count_triples(array, sum_value))

array = [5, 1, 3, 4, 7]
array = [5, 1, 3, 4, 7]
sum_value = 12
print(count_triples(array, sum_value))
print(count_triples(array, sum_value))

# Complexity above is N^3


def count_triples(array, sum_value):

# Sort the array first
array.sort()

count = 0
count = 0

# Choose the first element
for F in range(0, len(array)-2):
for F in range(0, len(array) - 2):
# Fix the next elements
S = F + 1
T = len(array) - 1
T = len(array) - 1

# While the element on the right doesn't cross over
while T > S:
# If the sum is greater than or equal to sum value, need to
# If the sum is greater than or equal to sum value, need to
# decrease second element
if (array[F] + array[S] + array[T]) >= sum_value:
T-=1
T -= 1
# Otherwise, add number of elements in range to count and move left value
else:
count+=(T-S)
S+=1
count += T - S
S += 1

return count

array = [5, 1, 3, 4, 7]


array = [5, 1, 3, 4, 7]
print(count_triples(array, 12))
43 changes: 22 additions & 21 deletions dfs/dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# Option 1- with queue

class Graph:

class Graph:
def __init__(self):
self.graph = dict()

Expand All @@ -16,7 +16,7 @@ def dfs(self, source):

# Make a queue and add the source to it
queue = [source]

# All nodes aren't visited
visited = {node: False for node in self.graph}

Expand All @@ -26,29 +26,29 @@ def dfs(self, source):
source = queue.pop(-1)
visited[source] = True
print(source)
for node in self.graph[source]:
for node in self.graph[source]:
if not visited[node]:
visited[node] = True
queue.append(node)


# Create a graph
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)

g.dfs(2)

print()

# Option 2 - with recursion, helper function

class Graph:

class Graph:
def __init__(self):
self.graph = dict()

Expand All @@ -70,17 +70,18 @@ def dfs_helper(self, source, visited):
def dfs(self, source):

visited = {node: False for node in self.graph}

# Call helper function
self.dfs_helper(source, visited)


# Create a graph
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)

g.dfs(2)

0 comments on commit 0a9bf8a

Please sign in to comment.