Week 4 Live Coding Solutions
Week 4 Live Coding Solutions
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
Suffix code(Visible)
1 start = int(input())
2 end = int(input())
3 AList = eval(input())
4 shortestpath = minimumhops(AList, start, end)
5 print(shortestpath)
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]
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
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))
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
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
Sample Output
1 [0, 2, 3, 8, 4, 5, 1, 6, 7]
Graphical representation
Solution Code
Suffix Code(Visible)
1 AList = eval(input())
2 preference = eval(input())
3 print(coolWorkers(AList, preference))
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]
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]