Browse Source

Update 'list.md'

master
Anand Shukla 7 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 ### Problem
Write a function `flatten_list(nested_list)` that flattens an arbitrarily nested list into a single-level list.
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.
### Example ### Example
```python ```python
flatten_list([1, [2, 3], [4, [5, 6], 7]]) flatten_list([1, [2, 3], [4, [5, 6], 7]])
# Output: [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 ### Pro Constraints
- `isinstance` type checking - Must support arbitrary levels of nesting.
- `extend` vs `append` - 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** **Group Consecutive Duplicates**
### Problem ### Problem
Write a function `group_consecutive(lst)` that groups **only consecutive** duplicate elements into sublists.
Write a function `group_consecutive(lst)` that groups consecutive identical elements into sublists.
### Example ### Example
```python ```python
group_consecutive([1, 1, 2, 2, 2, 3, 1, 1]) group_consecutive([1, 1, 2, 2, 2, 3, 1, 1])
# Output: [[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 ### Pro Constraints
- Temporary sublists - Avoid using third-party libraries.
- `for` loops with conditionals - 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 ### Problem
Write a function `rotate_list(lst, k)` that rotates the list **right** by `k` steps. Write a function `rotate_list(lst, k)` that rotates the list **right** by `k` steps.
> Support negative `k` to rotate left.
> BONUS: Support negative `k` to rotate left.
### Example ### Example
```python ```python
rotate_list([1, 2, 3, 4, 5], 2) rotate_list([1, 2, 3, 4, 5], 2)
# Output: [4, 5, 1, 2, 3] # 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 ### Problem
Write a function `diff_lists(a, b)` that removes one matching element in `b` for each occurrence found in `a`, preserving order.
Write a function `diff_lists(a, b)` that removes **each occurrence** in `b` from `a`, respecting the number of times elements occur.
### Example ### Example
```python ```python
diff_lists([1, 2, 2, 3, 4], [2, 4]) diff_lists([1, 2, 2, 3, 4], [2, 4])
# Output: [1, 2, 3] # 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.
### 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.
### Concepts
- Element-by-element removal
- List copying
- `in` with deletion logic
---
## ✅ Pro Tips ---
## Pro Tips
- Try solving without using libraries. - Try solving without using libraries.
- Then try versions using `collections.Counter`, `itertools`, or `functools` for practice. - Then try versions using `collections.Counter`, `itertools`, or `functools` for practice.
- Write unit tests for edge cases: - Write unit tests for edge cases:
- Empty lists - Empty lists
- Deep nesting - Deep nesting
- Large `k` values - Large `k` values
- Negative numbers - 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