PracticeLevel 4: ItertoolsEx 88. Compress consecutive statuses
0/150 Solved(0%)
Level 4Exercise #88

Compress consecutive statuses

Run-length encode consecutive equal statuses as (status, count) pairs.

What you’ll practice

This Level 4 exercise focuses on groupby in the Itertools curriculum. Solve the challenge prompt above using clear, idiomatic Python and the relevant language or standard-library tools.

Relevant Python reference: itertools.groupby.

Sample Test Cases

Example #1
Input:run_length(['ok','ok','fail','ok'])
Output:[('ok',2),('fail',1),('ok',1)]

Further Reading

Reference Solution

Reveal Reference Solution
def run_length(statuses: list[str]) -> list[tuple[str,int]]:
from itertools import groupby
return [(status, sum(1 for _ in group)) for status, group in groupby(statuses)]
Pro Tips & Keyboard Shortcuts
Color Theme
Ctrl + K then T

Open VS Code Color Theme Quick Pick to select from 20 dark and light themes.

Editor SettingsCtrl + K

Open practice settings drawer to toggle line numbers, indenting, font size & hints.

Search MenuCtrl + /

Expand sidebar menu, focus search bar, and highlight search text instantly.

Focus Code Editor
Esc or Ctrl + `

Instantly highlight and focus code editor from anywhere, restoring cursor right where you left off.

Normal ViewEsc

Collapse sidebar and close all popups or settings drawers for clean focus view.

solution.py· Python 3.11 WASM