Browse Source

Update 'list.md'

master
Anand Shukla 6 days ago
parent
commit
e611bfe495
  1. 99
      list.md

99
list.md

@ -5,107 +5,130 @@ These exercises will help you get really good at using lists in Python. You'll p
---
## Exercise 1
## PYEKX-6280ca9a
**Flatten a Nested List**
**Flatten a Deeply Nested List**
### Problem
Write a function `flatten_list(nested_list)` that takes a list which may contain nested lists of any depth, and returns a single, flat list.
Write a function `flatten_list(nested_list)` that flattens an arbitrarily nested list into a single-level list.
### Example
```python
flatten_list([1, [2, 3], [4, [5, 6], 7]])
# Output: [1, 2, 3, 4, 5, 6, 7]
```
### Concepts
### Why? (*Why it matters*)
Flattening is common in:
- ETL pipelines
- JSON parsing
- Recursive DOM traversal
- Recursion
- `isinstance` type checking
- `extend` vs `append`
### Pro Constraints
- Must support arbitrary levels of nesting.
- Must not mutate the input list.
- Use recursion or a stack-based approach.
- Bonus: Implement both recursive and iterative versions.
---
## Exercise 2
## PYEKX-1d45ec3e
**Group Consecutive Duplicates**
### Problem
Write a function `group_consecutive(lst)` that groups consecutive identical elements into sublists.
Write a function `group_consecutive(lst)` that groups **only consecutive** duplicate elements into sublists.
### Example
```python
group_consecutive([1, 1, 2, 2, 2, 3, 1, 1])
# Output: [[1, 1], [2, 2, 2], [3], [1, 1]]
```
### Concepts
### Why?
This mimics:
- Run-length encoding
- Log event grouping
- Real-time batching by last-seen state
- Iteration with state tracking
- Temporary sublists
- `for` loops with conditionals
### Pro Constraints
- Avoid using third-party libraries.
- Maintain original order.
- Think about how to handle empty input and non-integer elements.
---
## Exercise 3
## PYEKX-3c65900f
**Rotate List k Times**
**Rotate List (Bidirectional) k Times**
### Problem
Write a function `rotate_list(lst, k)` that rotates the list **right** by `k` steps.
> BONUS: Support negative `k` to rotate left.
> Support negative `k` to rotate left.
### Example
```python
rotate_list([1, 2, 3, 4, 5], 2)
# Output: [4, 5, 1, 2, 3]
rotate_list([1, 2, 3, 4, 5], -2)
# Output: [3, 4, 5, 1, 2]
```
### Concepts
### Why?
- Common in **buffer management, scheduling, circular data** structures.
- Reinforces modular math and slicing mastery.
- Identify concepts yourself.
### Pro Constraints
- Solve it using slicing.
- Then try using only loops - no slicing.
- Handle large `k` values: `rotate_list([1,2,3], 100)`.
---
## Exercise 4
## PYEKX-6a7bc172
**List Difference by Value and Order**
**List Difference (Ordered & Counted)**
### Problem
Write a function `diff_lists(a, b)` that removes **each occurrence** in `b` from `a`, respecting the number of times elements occur.
Write a function `diff_lists(a, b)` that removes one matching element in `b` for each occurrence found in `a`, preserving order.
### Example
```python
diff_lists([1, 2, 2, 3, 4], [2, 4])
# Output: [1, 2, 3]
```
> One `2` and the `4` are removed. The second `2` remains.
### Why?
- Models real-world inventory adjustments.
- Used in symmetric difference, record purging, filtering with quantity.
### Concepts
- Element-by-element removal
- List copying
- `in` with deletion logic
### Pro Constraints
- Do not use `collections.Counter`.
- Optimize for readability and minimal mutations.
- Bonus: Add a flag to toggle whether duplicates in b remove all vs one.
---
## ✅ Pro Tips
---
## Pro Tips
- Try solving without using libraries.
- Then try versions using `collections.Counter`, `itertools`, or `functools` for practice.
- Write unit tests for edge cases:
- Empty lists
- Deep nesting
- Large `k` values
- Negative numbers
```python
def test_flatten():
assert flatten_list([1, [2], [[3]]]) == [1, 2, 3]
assert flatten_list([]) == []
assert flatten_list([[], [[], [[]]]]) == []
def test_rotate():
assert rotate_list([1,2,3], 0) == [1,2,3]
assert rotate_list([1,2,3], 3) == [1,2,3]
assert rotate_list([1,2,3], -1) == [2,3,1]
```

Loading…
Cancel
Save