def calculate_depth(t, n):
if n not in t:
return 1
depth = 1
for c in tree[n]:
depth += calculate_depth(t, c)
return depth
tree = {}
tree['A'] = ['B', 'C']
tree['B'] = ['D', 'E']
tree['C'] = ['F']
tree['D'] = []
tree['E'] = []
tree['F'] = []
print(calculate_depth(t, 'A'))
Note: t→ Tree, n→ Node, c→ Child
What is the output of the given code ______?
def r_s(d):
total = 0
for key, value in d.items():
if isinstance(value, dict):
total += r_s(value)
elif isinstance(value, int):
total += value
return total
data = {
'a': 1,
'b': {'c': 2, 'd': {'e': 3}},
'f': {'g': 4},
'h': 5
}
print(r_s(data))
What is the output of the r_s(data) function__________?
def computeD(X):
D = [0] * len(X)
for i in range(1, len(X)):
if X[i] < X[i - 1]:
D[i] = X[i - 1] - X[i]
else:
D[i] = 0
return D
X = [8, 5, 7, 4, 10]
result = computeD(X)
print(result)
Which ONE of the following values is returned by the function computeD(X) for X=[8,5,7,4,10]?
def p_s(data):
result = []
for key, value in data.items():
if isinstance(value, list):
for v in value:
if v % 2 == 0:
result.append(v)
elif isinstance(value, dict):
result.extend(p_s(value))
return result
data = {
'a': [1, 2, 3],
'b': {'c': [4, 5], 'd': [6, 7, 8]},
'e': [9, 10]
}
output = p_s(data)
Which of the following statements are true about the final output?
[MSQ]
def m_s(words):
result = []
for word in words:
transformed = word[::-1].upper()
if len(transformed) % 2 == 0:
result.append(transformed)
else:
result.append(transformed[::-1])
return result
words = ["apple", "banana", "kiwi", "cherry", "date"]
output = m_s(words)
Which of the following statements are true about the final output? [MSQ]
def fun(D, s1, s2):
if s1 < s2:
D[s1], D[s2] = D[s2], D[s1]
fun(D, s1 + 1, s2 - 1)
What does this Python function fun() do? Select the ONE appropriate option below.
def m_d(d1, d2):
for key, value in d2.items():
if key in d1:
if isinstance(d1[key], dict) and isinstance(value, dict):
merge_dicts(d1[key], value)
elif isinstance(d1[key], list) and isinstance(value, list):
d1[key].extend(value)
elif isinstance(d1[key], set) and isinstance(value, set):
d1[key].update(value)
else:
d1[key] = value
return d1
dict1 = {'a': {'b': 1, 'c': 2}, 'd': [1, 2], 'e': {1, 2}}
dict2 = {'a': {'b': 3}, 'd': [3, 4], 'e': {3}, 'f': 4}
result = m_d(dict1, dict2)
Which of the following are true about the m_d function? [MSQ]
def d_s(matrix):
n = len(matrix)
total = 0
for i in range(n):
total += matrix[i][i]
if i != n - i - 1:
total += matrix[i][n - i - 1]
return total
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
result = d_s(matrix)
Which of the following are true about the diagonal_sum function? [MSQ]
def fun(matrix):
if len(matrix) == 0:
return []
return list(map(list, zip(*matrix)))
What does this Python function fun() do? Select the ONE appropriate option below.
def fun(arr):
x = [0] * len(arr)
x[0] = arr[0]
for i in range(1, len(arr)):
x[i] = x[i-1] + arr[i]
return x
def fun(arr):
return len(set(arr))
def fun(n, r):
return (1 - r**n) / (1 - r)
result =fun(10, 2)
def fun(s):
x = []
for start in range(len(s)):
for end in range(start + 1, len(s) + 1):
x.append(s[start:end])
return max(x, key=len)
result = fun("abcd")
deffun(s):
parts = s.split('-')
return sum(int(part) for part in parts)
result = fun("10-20-30")
def fun(s, n):
return s * n
result = fun("R", 3) + "B" + repeat_string("R", 2)