Embercode
✕
Built-in CollectionsQuick quiz

Pipelines with generators and built-ins

6 questions. Covers Comprehensions, generators and ranges.

Questions 1 to 5 of 6

  1. 1. What does this program print, in order? ``python def check(n): print('check', n) return n numbers = (check(n) for n in [5, 12, 7, 20, 3]) found = next(n for n in numbers if n > 10) print('found', found) ``
  2. 2. What does this program print? ``python print(sum(range(1, 5)), sum(range(1, 6))) ``
  3. 3. What does this program print? ``python lines = ['Asha,82', '', 'Sam,91'] pairs = (line.split(',') for line in lines if line.strip()) scores = [int(parts[1]) for parts in pairs if len(parts) == 2] print(sum(scores) / len(scores)) ``
  4. 4. Put these steps of a top-words job in the order the program runs them.

    Use the arrows to put the steps in order.

    1. 1.Count each word with collections.Counter(words)
    2. 2.Sort the (word, count) pairs by count from highest, then by word
    3. 3.Keep the first three pairs with a slice, [:3]
    4. 4.Split the sentence into words with text.split() and loop over the result
  5. 5. What happens when Python runs this code? ``python count = 0 add = lambda x: count += x add(5) print(count) ``

4 question(s) left on this page.