Lab, Iterators and Generators
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Every pair of people in team must have one meeting. Use itertools.combinations to store all pairs in a list pairs, print each as ana & ben, then print 6 meetings.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so the words are grouped by their length, and the program prints 3 ['cat', 'dog'], 4 ['bird', 'fish'] and 5 ['horse'].
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The program should print one line per team with all its points added up, but red appears twice. Fix it so it prints:
blue 7 red 9
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows change and balance each time one of them changes, once both exist.
from itertools import accumulate
changes = [50, -20, 35, -10]
for change, balance in zip(changes, accumulate(changes)):
passSign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: One loop over itertools.product; no nested for loop.
This code works, but it uses two nested loops. Rewrite it with itertools.product and a single loop. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Using groupby (after sorting), work out each customer's total spend from orders. Then print the customers from highest to lowest total with their rank, but stop as soon as a total is below 50, using itertools.takewhile. Finally print the running total of the listed customers with accumulate. The output should be:
1. cara 140 2. ana 95 3. dev 60 running: [140, 235, 295]
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.