Question: import heapq class Node: def __init__(self, left=None, right=None, val=0): self.val = val self.left = left self.right = right self.st = '' t = int(input()) while
import heapq
class Node: def __init__(self, left=None, right=None, val=0): self.val = val self.left = left self.right = right self.st = ''
t = int(input()) while t > 0: t -= 1 ls = list(input()) arr = list(map(int, input().split())) brr = [] n = len(ls) for i in range(n): brr.append([arr[i], Node(None, None, i)])
heapq.heapify(brr) while len(brr) >= 2: y = heapq.heappop(brr) x = heapq.heappop(brr) z = Node(None, None, x[0] + y[0]) if x[0] > y[0]: z.right = x[1] z.left = y[1] else: z.left = x[1] z.right = y[1] heapq.heappush(brr, [z.val, z]) def assign(root, st): if root is None: return root.st = st if root.left is None and root.right is None: del root print(st, end = " ") return assign(root.left, st + '0') assign(root.right, st + '1')
assign(brr[0][1], '') print()
how would i adjust the above code to read input from terminal and print the output to an output.txt file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
