0% found this document useful (0 votes)
3 views

Week 4 Live Coding Solutions

The document outlines solutions for three live coding problems from Week 4, including finding the shortest path in a graph, counting maximum routes between two nodes without revisiting, and determining the order of task completion based on preferences and dependencies. Each problem includes sample inputs, outputs, and solution code. The solutions utilize breadth-first search and other algorithms to address the respective challenges presented in the problems.

Uploaded by

SubhashVenkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Week 4 Live Coding Solutions

The document outlines solutions for three live coding problems from Week 4, including finding the shortest path in a graph, counting maximum routes between two nodes without revisiting, and determining the order of task completion based on preferences and dependencies. Each problem includes sample inputs, outputs, and solution code. The solutions utilize breadth-first search and other algorithms to address the respective challenges presented in the problems.

Uploaded by

SubhashVenkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Week 4 Live Coding Solutions

Week 4 Live Coding Solutions


Week 4 - Live Coding Problem 1
Solution Code
Public Test case
Private Test case
Week 4 - Live Coding Problem 2
Solution Code
Public Test case
Private Test case
Week 4 - Live Coding Problem 3
Solution Code
Public Test case
Private Test case

Week 4 - Live Coding Problem 1


Shortest Path

The express train routes are provided in the adjacency list AList , here you have to find the route
from start to end with minimum number of possible. Write a function minimumhops(AList,
start, end) to return the cities to be visited starting from start to end . Return a list with only
start if the end is not reachable.

Sample Input

1 start = 8
2 end = 7
3 AList = {
4 0: [8],
5 8: [0, 9],
6 1: [3, 5, 8],
7 3: [1, 7, 2],
8 5: [4],
9 2: [8, 9],
10 9: [1],
11 7: [8],
12 4: [2, 6],
13 6: [9]
14 }

Sample output

1 [8, 9, 1, 3, 7]
Graphical representation

Solution Code

1 def BFSListPathLevel(AList, v):


2 level, parent = {}, {}
3 for i in AList.keys():
4 level[i] = -1
5 parent[i] = -1
6 q = []
7
8 level[v] = 0
9 q.append(v)
10
11 while len(q) > 0:
12 j = q.pop(0)
13 for k in AList[j]:
14 if level[k] == -1:
15 level[k] = level[j]+1
16 parent[k] = j
17 q.append(k)
18 return level, parent
19
20 def minimumhops(AList, start, end):
21 level, path = BFSListPathLevel(AList, start)
22 shortestpath = []
23 if level[end] != -1:
24 shortestpath.append(end)
25 while shortestpath[-1] != start:
26 end = path[end]
27 shortestpath.append(end)
28 else:
29 shortestpath.append(start)
30 return shortestpath[::-1]
31

Suffix code(Visible)

1 start = int(input())
2 end = int(input())
3 AList = eval(input())
4 shortestpath = minimumhops(AList, start, end)
5 print(shortestpath)

Public Test case


Input 1

1 8
2 0
3 {0: [8], 8: [9], 1: [3, 5, 8], 3: [1, 7, 2], 5: [4], 2: [8, 9], 9: [1], 7:
[8], 4: [2, 6], 6: [9]}

Output

1 [8]

Input 2

1 8
2 7
3 {0: [8], 8: [0, 9], 1: [3, 5, 8], 3: [1, 7, 2], 5: [4], 2: [8, 9], 9: [1], 7:
[8], 4: [2, 6], 6: [9]}

Output

1 [8, 9, 1, 3, 7]

Input 3
1 0
2 1
3 {0: [1], 1: [2], 2: [3], 3: [4], 4: [5], 5: [6], 6: [7], 7: [8], 8: [9], 9:
[0]}

Output

1 [0, 1]

Private Test case


Input 1

1 1
2 0
3 {0: [1], 1: [2], 2: [3], 3: [4], 4: [5], 5: [6], 6: [7], 7: [8], 8: [9], 9:
[0]}

Output

1 [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Input 2

1 1
2 0
3 {0: [1], 1: [2, 3], 2: [3, 4], 3: [4, 5], 4: [5, 6], 5: [6, 7], 6: [7, 8], 7:
[8, 9], 8: [9, 0], 9: [0]}

Output

1 [1, 2, 4, 6, 8, 0]

Input 3

1 1
2 0
3 {0: [1], 1: [2, 3, 4], 2: [3, 4, 5], 3: [4, 5, 6], 4: [5, 6, 7], 5: [6, 7,
8], 6: [7, 8, 9], 7: [8, 9, 0], 8: [9, 0], 9: [0]}

Output

1 [1, 4, 7, 0]
Week 4 - Live Coding Problem 2
Back and Forth

Write a function backandforth(AList, end1, end2) to return the maximum number of possible
route between node end1 and node end2 in the undirected graph without going through the
same node again with exception to end1 and end2 . The connectivity details between nodes are
provided by the adjacency list AList .

Sample Input

1 AList = {
2 0: [2, 3, 6],
3 2: [0, 3, 4],
4 3: [4, 2, 0, 1],
5 6: [1, 5, 0],
6 1: [3, 6, 5],
7 4: [2, 3, 5],
8 5: [1, 4, 6]
9 }
10 end1 = 0
11 end2 = 1

Sample Output

1 3

Graphical Representation

Explanation

The possible paths are [0, 3, 1], [1, 6, 0] and [0, 2, 4, 5, 1]. Hence the answer is 3.
Solution Code

1 def BFSListPath(AList, v, preventionList):


2 visited, parent = {}, {}
3 for i in AList.keys():
4 visited[i] = False
5 parent[i] = -1
6 q = []
7
8 visited[v] = True
9 q.append(v)
10
11 while len(q) > 0:
12 j = q.pop(0)
13 for k in AList[j]:
14 if not visited[k] and not k in preventionList:
15 visited[k] = True
16 parent[k] = j
17 q.append(k)
18 return visited, parent
19
20 def findpath(parent, start, end):
21 L = []
22 curr = parent[end]
23 while curr != start:
24 L.append(curr)
25 curr = parent[curr]
26 return L
27
28 def backandforth(AList, end1, end2):
29 preventionList = []
30 c = 0
31 visited, parent = BFSListPath(AList, end1, preventionList)
32 while visited[end2]:
33 c += 1
34 path = findpath(parent, end1, end2)
35 preventionList.extend(path)
36 visited, parent = BFSListPath(AList, end1, preventionList)
37 return c
38

Suffix Code

1 end1 = int(input())
2 end2 = int(input())
3
4 AList = {}
5
6 while True:
7 line = input()
8 if line.strip() == '':
9 break
10 u, vs = line.strip().split(':')
11 u = int(u)
12 AList[u] = []
13 for v in vs.strip().split():
14 v = int(v)
15 if v not in AList:
16 AList[v] = []
17 AList[u].append(v)
18
19 print(backandforth(AList, end1, end2))

Public Test case


Input 1

1 0
2 1
3 0 : 2 3 6
4 1 : 3 5 6
5 2 : 0 3 4
6 3 : 0 1 2 4
7 4 : 2 3 5
8 5 : 1 4 6
9 6 : 0 1 5
10
11

Output

1 3

Input 2

1 0
2 1
3 0 : 2 3 6
4 1 : 3 5 6 8
5 2 : 0 3 4 7
6 3 : 0 1 2 4 7
7 4 : 2 3 5 7
8 5 : 1 4 6 8
9 6 : 0 1 5
10 7 : 2 3 4
11 8 : 1 5
12
13

Output
1 3

Input 3

1 0
2 1
3 0 : 2 3 4 5 6 7 8 9 10 11 12
4 1 : 2 3 4 5 6 7 8 9 10 11 12
5 2 : 0 1
6 3 : 0 1
7 4 : 0 1
8 5 : 0 1
9 6 : 0 1
10 7 : 0 1
11 8 : 0 1
12 9 : 0 1
13 10 : 0 1
14 11 : 0 1
15 12 : 0 1
16
17

Output

1 11

Private Test case


Input 1

1 0
2 1
3 0 : 2 3 4 5 6 7 8 9 10 11 12
4 1 : 2 3 4 5 6 7 8 9 10 11 12
5 2 : 0 1
6 3 : 0 1
7 4 : 0 1
8 5 : 0 1
9 6 : 0 1
10 7 : 0 1
11 8 : 0 1
12 9 : 0 1
13 10 : 0 1
14 11 : 0 1
15 12 : 0 1
16
17

Output
1 11

Input 2

1 0
2 1
3 0 : 2
4 1 : 12
5 2 : 0 3
6 3 : 2 4
7 4 : 3 5
8 5 : 4 6
9 6 : 5 7
10 7 : 6 8
11 8 : 7 9
12 9 : 8 10
13 10 : 9 11
14 11 : 10 12
15 12 : 1 11
16
17

Output

1 1

Input 3

1 0
2 1
3 0 : 2 3
4 1 : 11 12
5 2 : 0 3 12
6 3 : 0 2 4
7 4 : 3 5
8 5 : 4 6
9 6 : 5 7
10 7 : 6 8
11 8 : 7 9
12 9 : 8 10
13 10 : 9 11
14 11 : 1 10 12
15 12 : 1 2 11
16
17

Output

1 2
Week 4 - Live Coding Problem 3
Cool Worker

A group of workers have to complete a list of tasks, those tasks have dependencies within the task
list. But the workers prefer some interesting task and hates to do some boring task. They always
do the most interesting one among the available tasks to be done.

Write a function coolWorkers(AList, preference) to return the order in which the tasks will be
done. AList is the adjacency list with the dependencies and preference is the tasks sorted in
preferred order, in which task in index 0 is the most preferred and index -1 (last element) be the
least preferred.

Sample Input

1 AList = {0: [1, 2, 3],


2 1: [7],
3 2: [3, 5],
4 3: [4, 1, 8],
5 7: [],
6 5: [6, 1],
7 4: [5, 7],
8 8: [5],
9 6: [7]}
10 preference = [1, 3, 2, 6, 8, 5, 4, 0, 7]

Sample Output

1 [0, 2, 3, 8, 4, 5, 1, 6, 7]

Graphical representation
Solution Code

1 def coolWorkers(AList, preference):


2 n = len(AList.keys())
3 indegree = {}
4 toposortlist = []
5 for i in AList.keys():
6 indegree[i] = 0
7 for u in AList.keys():
8 for v in AList[u]:
9 indegree[v] = indegree[v] + 1
10
11 for i in range(n):
12 availableTasks = [k for k in AList if indegree[k] == 0]
13 t = [(preference.index(i), i) for i in availableTasks]
14 t.sort()
15 j = t[0][1]
16 toposortlist.append(j)
17 indegree[j] = indegree[j]-1
18 for k in AList[j]:
19 indegree[k] -= 1
20 return toposortlist

Suffix Code(Visible)

1 AList = eval(input())
2 preference = eval(input())
3 print(coolWorkers(AList, preference))

Public Test case


Input 1

1 {0: [1, 2, 3], 1: [7], 2: [3, 5], 3: [4, 1, 8], 7: [], 5: [6, 1], 4: [5, 7],
8: [5], 6: [7]}
2 [1, 3, 2, 6, 8, 5, 4, 0, 7]

Output

1 [0, 2, 3, 8, 4, 5, 1, 6, 7]

Input 2

1 {0: [1, 2, 3], 1: [7], 2: [3, 5], 3: [5, 1], 7: [], 5: [6, 1], 4: [5, 7], 6:
[7], 8: [5]}
2 [0, 8, 7, 5, 6, 1, 3, 2, 4]

Output

1 [0, 8, 2, 3, 4, 5, 6, 1, 7]

Private Test case


Input 1

1 {0: [1, 2, 3], 1: [7], 2: [3, 5], 3: [5, 1], 7: [], 5: [6, 1], 4: [5, 7], 6:
[7], 8: [5]}
2 [2, 4, 3, 1, 6, 0, 7, 5, 8]

Output

1 [4, 0, 2, 3, 8, 5, 1, 6, 7]

Input 2
1 {0: [8], 8: [], 1: [8], 2: [8], 3: [8], 4: [8], 5: [8], 6: [8], 7: [8]}
2 [5, 6, 4, 1, 0, 3, 7, 2, 8]

Output

1 [5, 6, 4, 1, 0, 3, 7, 2, 8]

You might also like