Skip to content

Commit 4fb4bec

Browse files
committed
Adding solutions for Assignment 5
1 parent e98569a commit 4fb4bec

6 files changed

+144
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,18 @@ When I went to take the HackerRank "CodePath Interview Prep - Unit 4 Test", once
178178
And here's a GIF of my InterviewBit Graphs topic page:
179179

180180
* [GIF of my InterviewBit Graphs topic page](https://github.com/tachyonlabs/CodePath-Alumni-Professional-Interview-Prep-Course/blob/master/interviewbit-graphs-topics.gif)
181+
182+
## Assignment 5
183+
184+
I completed four more InterviewBit Graphs problems:
185+
186+
* Possibility Of Finishing All Courses Given Pre Requisites
187+
* Word Search Board
188+
* Knight on Chess Board
189+
* Clone Graph
190+
191+
And here's a GIF of my updated InterviewBit Graphs topic page:
192+
193+
* GIF of my InterviewBit Graphs topic page
194+
195+
I took the "CodePath Interview Prep - Unit 5" test on January 28, so you should have received it.

interviewbit-graphs-clone-graph.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for a undirected graph node
2+
# class UndirectedGraphNode:
3+
# def __init__(self, x):
4+
# self.label = x
5+
# self.neighbors = []
6+
from collections import deque
7+
8+
9+
class Solution:
10+
# @param node, a undirected graph node
11+
# @return a undirected graph node
12+
def cloneGraph(self, node):
13+
cloned_nodes = {}
14+
checked = {}
15+
cloned_start_node = None
16+
queue = deque([node])
17+
# first a BFS to clone all the nodes but with empty neighbors lists
18+
while queue:
19+
current = queue.popleft()
20+
cloned_nodes[current.label] = UndirectedGraphNode(current.label)
21+
if not cloned_start_node:
22+
cloned_start_node = cloned_nodes[current.label]
23+
checked[current] = 1
24+
for neighbor in current.neighbors:
25+
if neighbor not in checked and neighbor not in queue:
26+
queue.append(neighbor)
27+
28+
# then another round to fill in the neighbors lists
29+
checked = {}
30+
queue = deque([node])
31+
while queue:
32+
current = queue.popleft()
33+
checked[current] = 1
34+
for neighbor in current.neighbors:
35+
cloned_nodes[current.label].neighbors.append(cloned_nodes[neighbor.label])
36+
if neighbor not in checked and neighbor not in queue:
37+
queue.append(neighbor)
38+
39+
return cloned_start_node
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections import deque
2+
3+
class Solution:
4+
# @param A : integer
5+
# @param B : integer
6+
# @param C : integer
7+
# @param D : integer
8+
# @param E : integer
9+
# @param F : integer
10+
# @return an integer
11+
def knight(self, A, B, C, D, E, F):
12+
directions = [(-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)]
13+
dest = [F, E]
14+
source = [D, C]
15+
rows = B
16+
cols = A
17+
queue = deque([source + [0]])
18+
dont_check_again = [[False for c in range(cols + 1)] for r in range(rows + 1)]
19+
dont_check_again[D][C] = True
20+
while queue:
21+
row, col, steps = queue.popleft()
22+
if [row, col] == dest:
23+
return steps
24+
for y, x in directions:
25+
new_y, new_x = row + y, col + x
26+
if new_y >= 1 and new_y <= rows and new_x >= 1 and new_x <= cols and not dont_check_again[new_y][new_x]:
27+
queue.append([new_y, new_x, steps + 1])
28+
dont_check_again[new_y][new_x] = True
29+
30+
return -1
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
# @param A : integer
3+
# @param B : list of integers
4+
# @param C : list of integers
5+
# @return an integer
6+
def is_cycle(self, num):
7+
if num in self.black:
8+
return False
9+
if num in self.gray:
10+
return True
11+
del self.white[num]
12+
self.gray[num] = 1
13+
return any([self.is_cycle(n) for n in self.course_list[num]])
14+
15+
def solve(self, A, B, C):
16+
self.white = {i: 1 for i in range(1, A + 1)}
17+
self.gray = {}
18+
self.black = {}
19+
self.course_list = [[] for i in range(A + 1)]
20+
for i in range(len(B)):
21+
self.course_list[C[i]].append(B[i])
22+
23+
for i in range(1, A + 1):
24+
if self.is_cycle(i):
25+
return 0
26+
else:
27+
keys = self.gray.keys()
28+
for key in keys:
29+
del self.gray[key]
30+
self.black[key] = 1
31+
32+
return 1

interviewbit-graphs-topics-2.gif

3.78 MB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
# @param A : list of strings
3+
# @param B : string
4+
# @return an integer
5+
def __init__(self):
6+
self.directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
7+
self.bad = {}
8+
9+
def word_search(self, r, c, idx):
10+
if idx == len(self.word):
11+
return True
12+
if (r, c, idx) in self.bad or r < 0 or c < 0 or r >= len(self.A) or c >= len(self.A[r]):
13+
return False
14+
if self.A[r][c] != self.word[idx]:
15+
self.bad[(r, c, idx)] = 1
16+
return False
17+
return any(self.word_search(r + y, c + x, idx + 1) for y, x in self.directions)
18+
19+
def exist(self, A, B):
20+
# InterviewBit is putting a space on the end of at least one test case :-(!!!
21+
self.word = B.strip()
22+
self.A = A
23+
for row in range(len(A)):
24+
for col in range(len(A[row])):
25+
if A[row][col] == B[0] and self.word_search(row, col, 0):
26+
return 1
27+
28+
return 0

0 commit comments

Comments
 (0)