Embercode
✕
Dicts and SetsQuick quiz

Frequency windows

7 questions. Covers Sliding window with a frequency dict in Python.

Questions 1 to 5 of 7

  1. 1. What does this program print? ``python counts = {} s = 'eceba' left = 0 distinct = 0 best = 0 for right, c in enumerate(s): before = counts.get(c, 0) counts[c] = before + 1 if before == 0: distinct += 1 while distinct > 2: out = s[left] remaining = counts[out] - 1 if remaining == 0: del counts[out] distinct -= 1 else: counts[out] = remaining left += 1 best = max(best, right - left + 1) print(best, distinct) ``
  2. 2. What does this program print? ``python counts = {'a': 1} print('sum with get: ' + str(counts.get('z', 0) + 1)) n = counts['z'] print(n) ``
  3. 3. Which dict method returns a default value for a missing key, so the code never gets an error?

    Type your answer.

  4. 4. Write the formula for the length of a window that runs from index left to index right, with both ends included.

    Type your answer.

  5. 5. In the eceba trace with k = 2, the window removes the e at index 0, but the key 'e' stays in the dict. Why?

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.