ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
def splits(a):
total = sum(a)
s = 0
c = 0
for x in a[:-1]:
s += x
if 2 * s == total:
c += 1
return c
print(splits([1, 2, 1, 0, 2]), splits([0, 0, 0]))- A. 2 2
- B. 1 1
- C. 1 2
- D. 0 2
Correct answer: C. 1 2
A split works when the prefix sum is exactly half the total. For [1,2,1,0,2] the total 6 gives one split, and for [0,0,0] every split works, so the count is 2.
More Python Programming questions
- What is printed? python from collections import deque def zeroone(n, edges, src): adj = [[] for in range(n)] for u, v, w in edges…
- What is printed? python from collections import deque def ladder(begin, end, words): S = set(words) q = deque([(begin, 1)]) seen = {begin}…
- What is printed? python def can(a): s = sum(a) if s % 2: return False t = s // 2 dp = [False] (t + 1) dp[0] = True for x in a: for v in…
- What is printed? python def paths(g): R, C = len(g), len(g[0]) dp = [[0] C for in range(R)] dp[0][0] = 0 if g[0][0] else 1 for r in…
- What is printed? python def cool(p): hold, sold, rest = float('-inf'), 0, 0 for x in p: hold, sold, rest = max(hold, rest - x), hold + x…
- What is printed? python import bisect def inv(a): s, c = [], 0 for x in a: c += len(s) - bisect.bisectright(s, x) bisect.insort(s, x)…