Lesson guide
What this Python exercise practices
Reconstruct a list from value-count tuples is a advanced practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 25 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Advanced
- Estimated time
- 25 minutes
Summary
Build a flattened list by repeating values according to provided counts.
Problem statement
Write a function reconstruct(pairs) that takes a list of tuples pairs where each tuple is (value, count). Return a single list containing each value repeated count times, preserving the order of tuples. You may assume count is a non-negative integer. If pairs is empty, return an empty list.
Task
Given a list of (value, count) tuples, return a list where each value appears count times in order.
Examples
Simple numeric reconstruction
Input
[(1, 3), (2, 1)]
Output
[1, 1, 1, 2]
Explanation
1 repeated 3 times, then 2 repeated once.
Input format
A single argument: a list of (value, count) tuples. count is a non-negative integer.
Output format
Return a list with values repeated according to their counts.
Constraints
Do not import external libraries. Counts are non-negative integers; handle zero by producing no occurrences of that value. Maintain the input order.
Samples
Sample input 0
[('a', 2), ('b', 0), ('a', 1)]
Sample output 0
['a', 'a', 'a']
Explanation 0
First 'a' twice, then 'b' zero times (skipped), then 'a' once more.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.