Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assessment #17

Open
wants to merge 77 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
3b8cef1
Exercises completed
PC-coding Jul 29, 2020
3569546
Exercises completed
PC-coding Jul 30, 2020
39354d0
Completed Exercises
PC-coding Jul 31, 2020
d536594
Pushing exercises
PC-coding Jul 31, 2020
9784d40
Completed
PC-coding Jul 31, 2020
f07cce6
Completed
PC-coding Jul 31, 2020
6cffc71
Completed Exercises
PC-coding Aug 2, 2020
b45b321
Working on these
PC-coding Aug 3, 2020
7cf72fe
Exercises completed
PC-coding Jul 29, 2020
8b55478
Exercises completed
PC-coding Jul 30, 2020
937c95a
Completed Exercises
PC-coding Jul 31, 2020
8625c8c
Pushing exercises
PC-coding Jul 31, 2020
b6a949d
Completed
PC-coding Jul 31, 2020
44800a1
Completed
PC-coding Jul 31, 2020
e413068
Completed Exercises
PC-coding Aug 2, 2020
bce5b73
Working on these
PC-coding Aug 3, 2020
6be0877
editing
PC-coding Aug 3, 2020
3f12b16
Updated and completed
PC-coding Aug 3, 2020
95a635a
Completed
PC-coding Aug 4, 2020
57dc055
Exercises completed
PC-coding Jul 29, 2020
485ab8a
Exercises completed
PC-coding Jul 30, 2020
6ce55cd
Completed Exercises
PC-coding Jul 31, 2020
68fe6b1
Pushing exercises
PC-coding Jul 31, 2020
af9b4bf
Completed
PC-coding Jul 31, 2020
d6f3670
Completed
PC-coding Jul 31, 2020
12ca515
Completed Exercises
PC-coding Aug 2, 2020
44469f0
Working on these
PC-coding Aug 3, 2020
c05fa28
mid merge
PC-coding Aug 4, 2020
9ec4c2f
mid merge
PC-coding Aug 4, 2020
73b1f95
mid merge
PC-coding Aug 4, 2020
bfdfc40
mid merge
PC-coding Aug 4, 2020
352b10f
Completed
PC-coding Jul 31, 2020
a1dfc79
Completed
PC-coding Jul 31, 2020
fd9249c
Completed Exercises
PC-coding Aug 2, 2020
6b4c0cf
Updated and completed
PC-coding Aug 3, 2020
cb2d917
Completed
PC-coding Aug 4, 2020
ca77da5
mid merge
PC-coding Aug 4, 2020
c077c8e
Completed exercises
PC-coding Aug 4, 2020
3cdad4e
Exercises completed
PC-coding Aug 5, 2020
c110075
completed
PC-coding Aug 5, 2020
644cdf4
Merge pull request #1 from ByteAcademyCo/master
PC-coding Aug 5, 2020
25b1ea6
completed
PC-coding Aug 5, 2020
d76cd1f
completed
PC-coding Aug 5, 2020
bf59585
Merge pull request #2 from ByteAcademyCo/master
PC-coding Aug 6, 2020
a4ef3ab
Completed Exercises
PC-coding Aug 6, 2020
e4debfd
Merge pull request #3 from ByteAcademyCo/master
PC-coding Aug 7, 2020
1aa0f5a
Merge pull request #4 from ByteAcademyCo/master
PC-coding Aug 7, 2020
68b3da0
Partially complete, working on last exercise
PC-coding Aug 9, 2020
769f697
Edited solutions to pass after merge conflict
PC-coding Aug 9, 2020
540d48a
Completed
PC-coding Aug 10, 2020
f0ba6ae
Merge pull request #5 from ByteAcademyCo/master
PC-coding Aug 10, 2020
7ac97f5
finished
PC-coding Aug 10, 2020
77007de
Merge pull request #6 from ByteAcademyCo/master
PC-coding Aug 11, 2020
b61a211
Partially complete
PC-coding Aug 12, 2020
222c985
Reviewed over this problem
PC-coding Aug 12, 2020
2f2edfb
Edited
PC-coding Aug 12, 2020
4366db8
Edited
PC-coding Aug 12, 2020
23f0829
Merge pull request #7 from ByteAcademyCo/master
PC-coding Aug 12, 2020
34032ae
Completed
PC-coding Aug 13, 2020
7ad3317
Edited last solution
PC-coding Aug 13, 2020
b3df4f9
Merge pull request #8 from ByteAcademyCo/master
PC-coding Aug 13, 2020
a4bf64d
Partially complete, working on lvl3 problems
PC-coding Aug 13, 2020
62437cc
Completed
PC-coding Aug 14, 2020
fcf05dc
Merge pull request #9 from ByteAcademyCo/master
PC-coding Aug 14, 2020
c3ca725
Quiz and partially completed Heaps
PC-coding Aug 17, 2020
c227cde
completed
PC-coding Aug 17, 2020
cd70b41
Merge pull request #10 from ByteAcademyCo/master
PC-coding Aug 17, 2020
72e4dce
Completed Exercises
PC-coding Aug 18, 2020
c8a1f08
Completed
PC-coding Aug 18, 2020
1db9a2c
Practice problems
PC-coding Aug 19, 2020
4cfa87e
Practice problems
PC-coding Aug 19, 2020
8fabf11
Completed Exercises
PC-coding Aug 20, 2020
039cb7f
Completed
PC-coding Aug 21, 2020
f5e541d
Completed
PC-coding Aug 21, 2020
4d375f4
edited
PC-coding Aug 21, 2020
bc9e62c
edited
PC-coding Aug 21, 2020
4b2b2d2
edited
PC-coding Aug 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
def factorial(n):
return
if n <= 1:
return 1
else:
return n * factorial(n-1)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

def is_palindrome(string):
if len(string) <= 1:
return True
else:
return string[0] == string[-1] and is_palindrome(string[1:-1])
return string[0] == string[-1]
is_palindrome(string[1:-1])
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
def product_sequence_n(n):
return
if n <= 1:
return 1
else:
return n * product_sequence_n(n-2)
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
def sum_first_n(n):
return
if n == 1:
return 1
else:
return n + sum_first_n(n-1)
20 changes: 19 additions & 1 deletion algorithms/dynamic_programming/2_Tribonacci/solution/solution.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# def tribonacci(n):
# if n == 0 or n == 1:
# return 0
# elif n == 2:
# return 1
# else:
# return tribonacci(n-1) + tribonacci(n-2) +tribonacci(n-3)
def tribonacci(n):
return
if n == 0 or n == 1:
return 0
if n == 2:
return 1
trib = [-1 for x in range(n+1)]
trib[0] = 0
trib[1] = 0
trib[2] = 1
for x in range(3, n+1):
# trib[x] = trib[x-1] + trib[x-2] + trib[x-3]
trib[x] = trib[x-3] + trib[x-2] + trib[x-1] #to make the bottom-up tabulation make more sense
return trib[x]
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
def paths_nth_stair(n):
return



if n <= 2:
return n
else:
paths = [-1 for x in range(n+1)]
paths[1] = 1
paths[2] = 2
for x in range(3, n+1):
paths[x] = paths[x-1] + paths[x-2]
return paths[n]
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
def shortest_path_to_1(n):
return
if n == 1:
return 0
elif n == 2 or n == 3:
return 1
shortest = [-1 for x in range(n+1)]
shortest[1] = 0
shortest[2] = 1
shortest[3] = 1
for x in range(4, n+1):
if shortest[x] == -1:
if x % 3 == 0 and x % 2 == 0:
shortest[x] = 1 + min(shortest[x-1], shortest[x//3], shortest[x//2])
elif x % 3 == 0:
shortest[x] = 1 + min(shortest[x-1], shortest[x//3])
elif x % 2 == 0:
shortest[x] = 1 + min(shortest[x-1], shortest[x//2])
else:
shortest[x] = 1 + shortest[x-1]
return shortest[n]


Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
def longest_common_subseq(str1, str2):
return
length_1 = len(str1)
length_2 = len(str2)
Length = [[None]*(length_2 + 1) for i in range(length_1 + 1)]
for i in range(length_1 + 1):
for x in range(length_2 + 1):
if i == 0 or x == 0:
Length[i][x] = 0
elif str1[i-1] == str2[x-1]:
Length[i][x] = Length[i-1][x-1] + 1
else:
Length[i][x] = max(Length[i-1][x], Length[i][x-1])
return Length[length_1][length_2]
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
def longest_palindrome_substr(str):
return
n = len(str)
if n == 0:
return 0
table = [[0 for x in range(n)] for y in range(n)]
max_length = 1
for i in range(n):
table[i][i] = True
start = 0
for i in range(n-1):
if (str[i] == str[i+1]):
table[i][i+1] = True
start = i
max_length = 2
for k in range(3, n+1):
for i in range(n - k + 1):
j = i + k - 1
if (table[i+1][j-1] and str[i] == str[j]):
table[i][j] = True
if k > max_length:
start = i
max_length = k
return max_length
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
def count_edges(matrix):
return
edge_count = 0
for i in range(len(matrix)):
for j in range(len(matrix[i])):
edge_count += matrix[i][j]
return edge_count
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
def count_islands(adjacency_list):
return
dictionary_count_edges = dict.fromkeys(adjacency_list.keys(), 0)
for adj_keyNode, edge_list in adjacency_list.items():
for keyNode in dictionary_count_edges.keys():
if not adj_keyNode == keyNode:
if keyNode in edge_list:
dictionary_count_edges[keyNode] += 1
number_of_Islands = 0
for edgeCount in dictionary_count_edges.values():
if edgeCount == 0:
number_of_Islands += 1
return number_of_Islands

#adjacency_list = { 0: [1], 1: [0, 1, 2], 2: []}

# dictionary_count_edges -> assigns every key:value pair to 0

#dictionary_count_edges = {0:0, 1:0, 2: 0}
#for every key and list in adjacency_list
#0 : [1]
#1 : [0, 1, 2]
#2 : []

#dictionary_count_edges = {0:2, 1:1, 2:1}
# for every key in dictionary_count_edges
# 0 -> in 0 and 1 so increment by 2
# 1 -> in 0 so increment by 1
# 2 -> in 1 so increment by 1
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
adjacency_matrix = []
adjacency_matrix = [
[1, 1, 0, 1, 0],
[0, 0, 0, 1, 1],
[0, 1, 0, 0, 0],
[1, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
]
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
adjacency_list = {}
adjacency_list = {
0: [1,2,3],
1: [0,3],
2: [1],
3: [0,3]
}
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
adjacency_list = {}
adjacency_list = {
0: [],
1: [2, 3],
2: [1],
3: [2]
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
adjacency_matrix = []
adjacency_matrix = [
[1, 0, 1],
[0, 0, 0],
[1, 0, 1]
]
11 changes: 10 additions & 1 deletion algorithms/graph_traversal/2_most_neighbors/solution/solution.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
def most_neighbours(adjacency_list):
return
maximum_len = -1
maximum_key = -1
for keyNode, edge_list in adjacency_list.items():
number_of_neighbours = len(edge_list)
if number_of_neighbours > maximum_len:
maximum_key = keyNode
maximum_len = number_of_neighbours
elif number_of_neighbours == maximum_len and keyNode < maximum_key:
maximum_key = keyNode
return maximum_key
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
def hops_away(adjacency_list, node, num_hops):
return
if not node in adjacency_list.keys():
return []
elif num_hops == 0:
return [node]
else:
number_hops_away = []
for neighbour in adjacency_list[node]:
number_hops_away.extend(hops_away(adjacency_list, neighbour, num_hops-1))
number_hops_away_1 = []
for n in number_hops_away:
if not n in number_hops_away_1:
number_hops_away_1.append(n)
return number_hops_away_1
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
def exists_path(matrix, origin, destination):
return
def exists_path_visited(matrix, origin, destination, visited):
if origin in visited:
return False
if not (origin in range(len(matrix)) and
destination in range(len(matrix))):
return False
elif matrix[origin][destination] == 1:
return True
visited.append(origin)
for j in range(len(matrix[origin])):
if matrix[origin][j] == 1 and exists_path_visited(matrix, j, destination, visited):
return True
return False
return exists_path_visited(matrix, origin, destination, [])

# matrix = [ [0, 1, 0], [0, 0, 1], [0, 1, 0] ]

# exists_path(matrix, 0, 2) => True

# exists_path_visited(matrix, 0, 2, [])

# visited = [0]
# for j = 0, 1, 2
# matrix, j, destination, visitedlist
# exists_path_visited(matrix, 1, 2, [0])

#matrix[1][2] == 1, so we would return True
32 changes: 31 additions & 1 deletion algorithms/graph_traversal/3_num_components/solution/solution.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
def reachable_dict(matrix):
reachable_dict = {}
for i in range(len(matrix)):
reachable_lst = []
for j in range(len(matrix[i])):
if matrix[i][j] == 1:
reachable_lst.append(j)
reachable_dict[i] = reachable_lst
#getting every key that's directly reachable and putting it into a list
all_reachable = {}
for i, neighbours in reachable_dict.items():
all_reachable_lst = []
for n in neighbours:
all_reachable_lst = all_reachable_lst + reachable_dict[n]
all_reachable_lst = all_reachable_lst + [n]
all_reachable[i] = all_reachable_lst
return all_reachable
#getting every key that's neighbors of the directly reachables and putting it into a list

def num_components(adjacency_matrix):
return
reachableDict = reachable_dict(adjacency_matrix)
num_components = 0
visited = [False for i in range(len(adjacency_matrix))]
for i in range(len(adjacency_matrix)):
if visited[i] == False:
num_components += 1
for j in reachableDict[i]:
visited[j] = True
return num_components

# visited [False, False, False, False]
# visited []
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
def min_hops_helper(max_units, n, river, num_hops):
position = 0
for i in range(max_units, 0, -1):
if position + i <= n and river[position + i] == 1:
num_hops += 1
position = position + i
return min_hops_helper(max_units, n-i, river[position:], num_hops)
if position == n:
return num_hops
else:
return -1

def min_hops(max_units, n, river):
return

return min_hops_helper(max_units, n, river, 0)
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
def steal_jewels(x, jewels):
return
def sort(tuples):
tuples.sort(key = lambda x: x[1])
tuples.reverse()
return tuples

def steal_jewels(x, jewels):
max_val = 0
sorted_value_jewels = sort(jewels)
for jewel in sorted_value_jewels:
jewel_weight = jewel[0]
jewel_value = jewel[1]
if x == 0:
return max_val
elif jewel_weight <= x:
max_val += jewel_value
x = x-jewel_weight
else:
# value of 1kg portion of the jewel
unit_val = jewel_value/jewel_weight
max_val += x * unit_val
return max_val
return max_val
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
def make_change_1(amount, denominations, num_coins):
# if amount is 0 we made all the change and we return num_coins.
if amount == 0:
return num_coins
# itterate through denominations from largest to smallest
for i in range(len(denominations)):
# if the amount is larger or equal to the current coin value we recurse, decreasing the amount by that coin value, and increasing the number of coins by 1.
if amount >= denominations[i]:
return make_change_1(amount-denominations[i], denominations, num_coins+1)
# once we've itterated through all the coins we never hit amount == 0, so we have a remainder we could not make change for and we return -1.
return -1

def make_change(amount, denominations):
return
# Sort denominations in decreasing order
denominations = sorted(denominations, reverse=True)
return make_change_1(amount, denominations, 0)
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
def binary_search_helper(lst, x, low, high):
if high >= low:
mid = (high + low) // 2
if lst[mid] == x:
return mid
elif lst[mid] > x:
return binary_search_helper(lst, x, low, mid - 1)
else:
return binary_search_helper(lst, x, mid + 1, high)
else:
return -1

def binary_search(lst, x):
return
return binary_search_helper(lst, x, 0, len(lst)-1)
Loading