ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
s = 'aabbbc'
out = []
i = 0
while i < len(s):
j = i
while j < len(s) and s[j] == s[i]:
j += 1
out.append(s[i] + str(j - i))
i = j
print(''.join(out), len(out))- A. a2b3c1 6
- B. a2b3 2
- C. a2b3c1 3
- D. a2b3c 3
Correct answer: C. a2b3c1 3
This is run-length encoding. Each run of equal characters is written as its letter followed by its length. The runs are aa, bbb and c, so there are three runs.
More Python Programming questions
- What is printed? python import math print(math.comb(10, 3) % 7, math.perm(5, 2), math.factorial(5) // math.factorial(3))
- What is printed? python x = [1, 2, 3] it = iter(x) print(next(it), sum(it), list(it))
- What is printed? python def tri(n): return n (n + 1) // 2 print(tri(10), tri(0), [tri(k) for k in range(4)])
- What is printed? python def f(xs): xs.sort() return xs data = [3, 1, 2] res = f(data) print(data is res, data)
- Which Python construct is the most efficient way to test whether a sorted list a contains x in O(log n) time without building extra…
- Which of the following problems can be solved correctly by a greedy algorithm that sorts by end time, when given the intervals and asked…