Week 6 & 7 Live Coding Solutions
Week 6 & 7 Live Coding Solutions
Sample input 1
1 [1,2,3,4,5,6]
Output
1 Min
Sample input 2
1 [5,3,4,2,1]
Output
1 Max
Sample input 3
1 [1,5,4,7,6,3,2]
Output
1 None
Solution
Solution Code
1 def minheap(A):
2 for i in range((len(A) - 2) // 2 + 1):
3 if A[i] > A[2*i + 1] or (2*i + 2 != len(A) and A[i] > A[2*i + 2]):
4 return False
5 return True
6 def maxheap(A):
7 for i in range((len(A) - 2) // 2 + 1):
8 if A[i] < A[2*i + 1] or (2*i + 2 != len(A) and A[i] < A[2*i + 2]):
9 return False
10 return True
11 def type_of_heap(A):
12 if minheap(A)==True:
13 return 'Min'
14 if maxheap(A)==True:
15 return 'Max'
16 return 'None'
Suffix Code(visible)
1 A=eval(input())
2 print(type_of_heap(A))
1 [1,2,3,4,5,6,7,8,9]
Output
1 Min
Input 2
1 [5,3,4,2,1]
Output
1 Max
Input 3
1 [1,5,4,7,6,3,2]
Output
1 None
1 [67,65,43,54,6,2,1,19,5]
Output
1 Max
Input 2
1 [1,2,3,4,8,5,6,7]
Output
1 Min
Input 3
1 [1,2,3,4,5,9,8,7,6,12,13]
Output
1 Min
Week-6 Live coding problem 2
Write a function findRedundantEdges(E,n) that accept an edge list E in increasing order of the
edge weight where all edge weights are distinct and the number of vertices n (labeled from 0 to
n-1) in a connected undirected graph and the function returns a list of redundant edges in
increasing order of weight, so by removing these edges, the graph should remain connected with
the minimum total cost of edges(minimum cost spanning tree). Try to write solution code of
complexity O((E+V)logV).
Note - Selected edges tuples in the output list should be similar to input list edges tuples.
Sample input 2
1 4
2 [(0,1,10),(1,2,20),(2,3,30),(3,0,40),(1,3,50)]
Output
Solution
Solution Code
1 class MakeUnionFind:
2 def __init__(self):
3 self.components = {}
4 self.members = {}
5 self.size = {}
6 def make_union_find(self,vertices):
7 for vertex in range(vertices):
8 self.components[vertex] = vertex
9 self.members[vertex] = [vertex]
10 self.size[vertex] = 1
11 def find(self,vertex):
12 return self.components[vertex]
13 def union(self,u,v):
14 c_old = self.components[u]
15 c_new = self.components[v]
16 # Always add member in components which have greater size
17 if self.size[c_new] >= self.size[c_old]:
18 for x in self.members[c_old]:
19 self.components[x] = c_new
20 self.members[c_new].append(x)
21 self.size[c_new] += 1
22 else:
23 for x in self.members[c_new]:
24 self.components[x] = c_old
25 self.members[c_old].append(x)
26 self.size[c_old] += 1
27
28 def findRedundantEdges(E,n):
29 st = MakeUnionFind()
30 st.make_union_find(n)
31 redlist=[]
32 for edge in E:
33 if st.find(edge[0])!=st.find(edge[1]):
34 st.union(edge[0], edge[1])
35 else:
36 redlist.append(edge)
37 return redlist
Suffix code(visible)
1 n = int(input())
2 E=eval(input())
3 print(findRedundantEdges(E,n))
1 7
2 [(0,1,10),(0,2,50),(0,3,60),(5,6,75),(2,1,80),(6,4,90),(1,6,100),(2,5,110),
(1,3,150),(3,4,180),(2,4,200)]
Output
1 [(2, 1, 80), (2, 5, 110), (1, 3, 150), (3, 4, 180), (2, 4, 200)]
Input 2
1 4
2 [(0,1,10),(1,2,20),(2,3,30),(3,0,40),(1,3,50)]
Output
Input 3
1 4
2 [(0,2,1),(0,1,2),(0,3,3),(1,2,4),(2,3,6)]
Output
1 6
2 [(0,1,1),(1,2,3),(1,3,4),(0,2,5),(2,4,7),(2,3,12),(3,4,13),(1,5,15),(2,5,17),
(3,5,21),(4,5,25)]
Output
1 [(0, 2, 5), (2, 3, 12), (3, 4, 13), (2, 5, 17), (3, 5, 21), (4, 5, 25)]
Input 2
1 6
2 [(0,1,1),(1,2,3),(1,3,4),(1,4,5),(0,4,7),(0,5,10),(2,3,12),(3,4,13),(1,5,15),
(2,5,17),(3,5,21),(4,5,25)]
Output
1 [(0, 4, 7), (2, 3, 12), (3, 4, 13), (1, 5, 15), (2, 5, 17), (3, 5, 21), (4,
5, 25)]
Input 3
1 7
2 [(0,1,10),(1,2,50),(2,3,60),(3,0,75),(3,1,80),(6,4,90),(1,6,100),(2,5,110),
(3,6,150),(3,4,180),(0,4,200)]
Output
1 [(3, 0, 75), (3, 1, 80), (3, 6, 150), (3, 4, 180), (0, 4, 200)]
Week-6 Live coding problem 3
Write a function find_kth_largest(root, k) that accept root as a reference of root node of
BST of n elements and an integer k, where 0 < k <= n . The function should return the kth
largest element without doing any modification in Binary Search Tree. The complexity of the
solution should be in order of O(logn + k)
1 class Tree:
2 def __init__(self,initval=None):
3 self.value = initval
4 if self.value:
5 self.left = Tree()
6 self.right = Tree()
7 else:
8 self.left = None
9 self.right = None
10 return
Sample input
Output
1 5
Solution
Solution Code
1 def kthlargest(root):
2 global count,result
3 if root.right!=None:
4 find_kth_largest(root.right,k)
5 count += 1
6 if count == k:
7 result = root.value
8 return
9 find_kth_largest(root.left,k)
10 count = 0
11 result = -1
12 def find_kth_largest(root,k):
13 kthlargest(root)
14 return result
Suffix code(hidden)
1 class Tree:
2 # Constructor:
3 def __init__(self,initval=None):
4 self.value = initval
5 if self.value:
6 self.left = Tree()
7 self.right = Tree()
8 else:
9 self.left = None
10 self.right = None
11 return
12 # Only empty node has value None
13 def isempty(self):
14 return (self.value == None)
15
16 def insert(self,v):
17 if self.isempty():
18 self.value = v
19 self.left = Tree()
20 self.right = Tree()
21 if self.value == v:
22 return
23 if v < self.value:
24 self.left.insert(v)
25 return
26 if v > self.value:
27 self.right.insert(v)
28 return
29
30 T = Tree()
31 bst = eval(input())
32 k = int(input())
33 for i in bst:
34 T.insert(i)
35 print(find_kth_largest(T,k))
1 [5,4,6,3,2,1,7]
2 3
Output
1 5
Input 2
1 [8,7,6,5,4,3,2,1]
2 2
Output
1 7
Input 3
1 [108, 348, 332, 463, 167, 148, 155, 331, 435, 349, 261, 336, 135, 449, 384,
183, 428, 262, 434, 276, 87, 29, 203, 24, 347, 119, 251, 370, 456, 433, 49,
421, 410, 57, 218, 226, 359, 163, 42, 179, 192, 10, 295, 235, 99, 286, 116,
290, 169, 146, 71, 34, 44, 141, 353, 132, 346, 488, 84, 16, 74, 289, 424, 59,
240, 252, 427, 250, 321, 281, 496, 288, 112, 408, 393, 247, 12, 387, 447,
278, 323, 338, 483, 379, 80, 114, 365, 118, 77, 164, 154, 325, 376, 180, 54,
140, 401, 223, 50, 14, 396, 25, 117, 38, 230, 144, 440, 206, 48, 388, 227,
268, 360, 300, 414, 274, 445, 200, 444, 106, 324, 490, 211, 477, 476, 238,
354, 204, 195, 258, 404, 26, 471, 263, 468, 176, 58, 110, 15, 19, 264, 378,
94, 439, 186, 193, 91, 419, 30, 102, 174, 7, 337, 136, 143, 88, 134, 291]
2 5
Output
1 477
1 [15,4,7,8,5,3,9,13,16,1]
2 1
Output
1 16
Input 2
1 [15,4,7,8,5,3,9,13,16,1]
2 10
Output
1 1
Input 3
1 [364, 266, 305, 157, 133, 391, 316, 68, 409, 432, 172, 39, 467, 92, 277, 82,
425, 311, 107, 204, 120, 497, 320, 178, 359, 90, 206, 239, 153, 1, 91, 31,
392, 106, 209, 262, 303, 122, 430, 195, 191, 156, 60, 344, 285, 67, 268, 496,
225, 4, 96, 396, 358, 356, 429, 235, 108, 291, 275, 388, 341, 465, 118, 12,
363, 161, 104, 486, 197, 20, 18, 472, 164, 199, 366, 26, 336, 227, 287, 244,
132, 272, 258, 110, 299, 184, 142, 86, 243, 50, 185, 167, 294, 116, 11, 180,
416, 176, 450, 10, 245, 492, 264, 121, 421, 454, 362, 162, 386, 6, 37, 426,
408, 41, 134, 298, 30, 25, 74, 155, 301, 128, 489, 340, 329, 446, 3, 282, 13,
233, 475, 64, 190, 315, 51, 373, 61, 474, 399, 213, 248, 208, 331, 179, 471,
140, 249, 415, 77, 186, 317, 188, 57, 230, 293, 148, 457, 355, 260, 276, 177,
322, 189, 418]
2 10
Output
1 467
Week-7 Live coding problem 1
Write a function decode(root, ciphertext) that accepts a variable root which contains the
reference of the root node of Huffman tree and an encoded message ciphertext in the form of
a string (using 0 and 1). The function returns the decoded message in the form of a string.
1 class Node:
2 def __init__(self,frequency,symbol = None,left = None,right = None):
3 self.frequency = frequency
4 self.symbol = symbol
5 self.left = left
6 self.right = right
Sample Input
Output
Solution
Solution code
1 # Solution
2 def decode(root,ciphertext):
3 message =''
4 temp = root
5 for i in ciphertext:
6 if i == '0':
7 temp = temp.left
8 if i == '1':
9 temp = temp.right
10 if temp.left == None and temp.right == None:
11 message += temp.symbol
12 temp = root
13 return message
Suffix Code(Hidden)
1 class Node:
2 def __init__(self,frequency,symbol=None,left=None,right=None):
3 self.frequency = frequency
4 self.symbol = symbol
5 self.left = left
6 self.right = right
7
8 def Huffman(s):
9 char = list(s)
10 freqlist=[]
11 unique = set(char)
12 for c in unique:
13 freqlist.append((char.count(c),c))
14 nodes = []
15 for nd in sorted(freqlist):
16 nodes.append((nd,Node(nd[0],nd[1])))
17 while len(nodes) > 1:
18 nodes.sort()
19 L = nodes[0][1]
20 R = nodes[1][1]
21 newnode = Node(L.frequency + R.frequency,L.symbol+R.symbol,L,R)
22 nodes.pop(0)
23 nodes.pop(0)
24 nodes.append(((L.frequency+R.frequency,L.symbol+R.symbol),newnode))
25 return newnode
26
27 # huffman code
28 '''a 111
29 b 000
30 c 001
31 d 10
32 e 110
33 f 01'''
34
35 s = 'aaaacccbbdddddddeeefffff'
36 cipher = input()
37 res = Huffman(s)
38 print(decode(res,cipher))
Public Test case
Input 1
1 0001110001110000011011001
Output
1 bababcdef
Input 2
1 11100000110110011110000011011001
Output
1 abcdefabcdef
Input 3
1 01110100010001111110000011011001
Output
1 fedcbaabcdef
1 0111010001000111111000001101100111100000110110011110000011011001
Output
1 fedcbaabcdefabcdefabcdef
Input 2
1 111111111111111111111111111111111
Output
1 aaaaaaaaaaa
Input 3
1 100110011001100110011001
Output
1 dfdfdfdfdfdf
Week-7 Live coding problem 2
Write a method MaxValueSelection(items, C) that accepts a dictionary items where each key
of the dictionary represents the item name and the corresponding value is a tuple (number of
units, value of all units) and function accept one more variable C which represents the
maximum capacity of units you can select from all items to get maximum value.
Sample input
1 {1:(10,60),2:(20,100),3:(30,120)}
2 50
Output
1 240.0
Solution
Solution Code
Suffix code(Visible)
1 items = eval(input())
2 C = int(input())
3 print(round(MaxValueSelection(items, C),2))
Public Test Case
Input 1
1 {1:(10,60),2:(20,100),3:(30,120)}
2 50
Output
1 240.0
Input 2
1 {1:(6,110),2:(7,120),3:(3,2)}
2 10
Output
1 178.57
Input 3
1 {1:(4,400),2:(9,1800),3:(10,3500),4:(5,4000),5:(2,1000),6:(1,200)}
2 15
Output
1 7800.0
Input 4
1 {1:(4,400),2:(9,1800),3:(10,3500),4:(20,4000),5:(2,1000),6:(1,200)}
2 20
Output
1 6100.0
Input 5
1 {1:(4,1600),2:(9,2700),3:(10,3500),4:(5,4000),5:(2,1000),6:(2,1200),7:
(2,1350),8:(9,1800),9:(10,2300),10:(5,1530),11:(2,100),12:(1,120),13:
(2,1600),14:(3,2700),15:(7,3500),16:(8,4000),17:(1,1000),18:(6,1200),19:
(1,1350),20:(10,1800),21:(2,2300),22:(10,1530),23:(4,100),24:(1,125)}
2 1
Output
1 1350.0
Private test case
Input 1
1 {1:(4,400),2:(9,1800),3:(10,3500),4:(5,4000),5:(2,1000),6:(1,200)}
2 8
Output
1 5350.0
Input 2
1 {1:(4,1600),2:(9,2700),3:(10,3500),4:(5,4000),5:(2,1000),6:(2,1200),7:
(2,1350),8:(9,1800),9:(10,2300),10:(5,1530),11:(2,100),12:(1,120)}
2 25
Output
1 12650.0
Input 3
1 {1:(4,1600),2:(9,2700),3:(10,3500),4:(5,4000),5:(2,1000),6:(2,1200),7:
(2,1350),8:(9,1800),9:(10,2300),10:(5,1530),11:(2,100),12:(1,120)}
2 10
Output
1 7050.0
Input 4
1 {1:(4,1600),2:(9,2700),3:(10,3500),4:(5,4000),5:(2,1000),6:(2,1200),7:
(2,1350),8:(9,1800),9:(10,2300),10:(5,1530),11:(2,100),12:(1,120),13:
(2,1600),14:(3,2700),15:(7,3500),16:(8,4000),17:(1,1000),18:(6,1200),19:
(1,1350),20:(10,1800),21:(2,2300),22:(10,1530),23:(4,100),24:(1,125)}
2 30
Output
1 21500.0
Input 5
1 {1:(4,1600),2:(9,2700),3:(10,3500),4:(5,4000),5:(2,1000),6:(2,1200),7:
(2,1350),8:(9,1800),9:(10,2300),10:(5,1530),11:(2,100),12:(1,120),13:
(2,1600),14:(3,2700),15:(7,3500),16:(8,4000),17:(1,1000),18:(6,1200),19:
(1,1350),20:(10,1800),21:(2,2300),22:(10,1530),23:(4,100),24:(1,125)}
2 2
Output
1 2500.0
Week-7 Live coding problem 3
Write a function IsCodeValid(hfcode, message) that accept a dictionary hfcode in which key
represents the character and corresponding value represents the Huffman code for that
character and function accept one more string message (encoded message generated using
Huffman codes). The function returns True if message is valid, otherwise return False .
Sample Input
Output
1 False
Sample Input
Output
1 True
Solution
1 def IsCodeValid(hfcode,message):
2 emsg = ''
3 huffcode ={}
4 maxlength=0
5 for i,j in hfcode.items():
6 huffcode[j]=i
7 if len(j) > maxlength:
8 maxlength=len(j)
9 cd = ''
10 for b in message:
11 cd += b
12 if len(cd) > maxlength:
13 return False
14 if cd in huffcode:
15 emsg += huffcode[cd]
16 cd = ''
17 if cd == '':
18 return True
19 else:
20 return False
Suffix Code(visible)
1 hfcode = eval(input())
2 message = input()
3 print(IsCodeValid(hfcode,message))
Output
1 False
Input 2
Output
1 True
Input 3
Output
1 True
Output
1 False
Input 2
Output
1 True
Input 3
Output
1 False