ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def partition(s):
last = {ch: i for i, ch in enumerate(s)}
out = []
start = end = 0
for i, ch in enumerate(s):
end = max(end, last[ch])
if i == end:
out.append(i - start + 1)
start = i + 1
return out
print(partition('ababcbacadefegdehijhklij'))- A. [10, 7, 7]
- B. [9, 8, 7]
- C. [9, 7, 8, 0]
- D. [9, 7, 8]
Correct answer: D. [9, 7, 8]
A segment ends once every letter inside it has reached its last occurrence. The three segments have lengths 9, 7 and 8, which add up to the 24 characters.
More Python Programming questions
- What is printed? python def units(boxes, cap): boxes = sorted(boxes, key=lambda b: -b[1]) total = 0 for n, u in boxes: take = min(n, cap)…
- What is printed? python def minremove(iv): iv = sorted(iv, key=lambda x: x[1]) keep = 0 end = float('-inf') for s, e in iv: if s >= end…
- What is printed? python def longest(s): st = [-1] best = 0 for i, ch in enumerate(s): if ch == '(': st.append(i) else: st.pop() if not st…
- What is printed? python import heapq from collections import Counter words = 'a b c a b a d d d d'.split() c = Counter(words)…
- What is printed? python from functools import cache @cache def cat(n): if n <= 1: return 1 return sum(cat(i) cat(n - 1 - i) for i in…
- What is printed? python from functools import lrucache grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]] @lrucache(maxsize=None) def best(r, c): if…