A minimalistic python library solving the Dutch national flag problem.
Project description
dutch_flag
dutch_flag
is a simple Python library that provides a function to sort an array of three distinct values (representing different colors) in linear time using the Dutch National Flag problem algorithm.
Installation
You can install the package using pip
:
pip install dutch_flag
Usage
Once installed, you can use the sort
function to sort an array of three distinct values. By default, the function sorts an array containing the values 0, 1, and 2, but you can customize the values if needed.
Example
from dutch_flag import sort
# Example with default values (0 = red, 1 = white, 2 = blue)
arr = [2, 0, 1, 2, 1, 0]
sorted_arr = sort(arr)
print(sorted_arr) # Output: [0, 0, 1, 1, 2, 2]
You can also customize the values that represent the colors:
### Example with custom values (r = 'r', w = 'w', b = 'b')
```python
arr = ['w', 'b', 'r', 'w', 'r', 'b']
sorted_arr = sort(arr, r='r', w='w', b='b')
print(sorted_arr) # Output: ['r', 'r', 'w', 'w', 'b', 'b']
### Function Definition
```python
def sort(arr, r=0, w=1, b=2):
"""
Sorts an array of three distinct values (default: r=0, w=1, b=2) in place.
Args:
arr (list): The array to sort.
r (int, optional): The value representing the 'red' color. Default is 0.
w (int, optional): The value representing the 'white' color. Default is 1.
b (int, optional): The value representing the 'blue' color. Default is 2.
Returns:
list: The sorted array.
Raises:
ValueError: If the array contains a value other than r, w, or b.
"""
Edge Cases
- Empty array: The function will return an empty array without raising an error.
- Invalid values: If the array contains values other than
r
,w
, orb
, the function will raise aValueError
.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dutch_flag-0.1.2.tar.gz
.
File metadata
- Download URL: dutch_flag-0.1.2.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08e6ebbf4d21250cb5e70f321d9d9bcb97d5b0e0897a813883ac0a998097815a |
|
MD5 | 03552d4e3239e223ff3618a37fee3fae |
|
BLAKE2b-256 | ae756a904aaced156cb01f0c53345bee5b4de9763308c98afa0c69940071f1c7 |
File details
Details for the file dutch_flag-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: dutch_flag-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17b3d6e6178e0ba54adb51d9aface7820e299df5d073fb7aeb25f7d147972274 |
|
MD5 | 623cc19912f1b61549625d0aa3ea76de |
|
BLAKE2b-256 | affcf07939c8cc2260b5175daf3222aac1ff81d62828e9bccc1cfabcca1bb8f3 |