A Python library for static arrays with dynamic memory allocation like malloc, realloc, and free, and various array operations like searching, sorting, and mathematics.
Project description
FlexArray
FlexArray is a Python library that provides an array-like structure with dynamic memory allocation features similar to C's malloc, realloc, and free.
Installation
You can install the package via pip:
pip install flexarray
Usage
Implementing Array with flexarray
from flexarray.array import Array
# Create an array with initial size 3
arr = Array(3)
arr[0] = 10
arr[1] = 20
print(arr) # Array([10, 20, None])
# Automatic dynamic resizing
arr[5] = 50
print(arr) # Array([10, 20, None, None, None, 50])
# Malloc: Allocate memory dynamically
arr.malloc(5)
print(arr) # Array([None, None, None, None, None])
# Realloc: Resize the array
arr.realloc(10)
print(arr) # Array([None, None, None, None, None, None, None, None, None, None])
# Free: Deallocate memory
arr.free()
print(arr) # Array([])
Using Mathematics and Sorting on FlexArray
from flexarray.array import Array
from flexarray.mathematics import (
SumOfElements, AverageOfElements, MaxElement, MinElement,
ProductOfElements, MedianOfElements, ModeOfElements
)
from flexarray.sorting import BubbleSort, SelectionSort, InsertionSort
# Step 1: Create a FlexArray with initial elements
arr = Array(6)
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
arr[5] = 60
# Step 2: Perform mathematical operations
print("Sum of elements:", SumOfElements(arr)) # Output: 210
print("Average of elements:", AverageOfElements(arr)) # Output: 35.0
print("Maximum element:", MaxElement(arr)) # Output: 60
print("Minimum element:", MinElement(arr)) # Output: 10
print("Product of elements:", ProductOfElements(arr)) # Output: 720000000
print("Median of elements:", MedianOfElements(arr)) # Output: 35.0
print("Mode of elements:", ModeOfElements(arr)) # Output: None (since no repeating elements)
# Step 3: Sorting FlexArray using different algorithms
# Bubble Sort
BubbleSort(arr)
print("Array after Bubble Sort:", arr)
# Selection Sort
arr.realloc(6) # Reset array for demonstration
arr[0], arr[1], arr[2], arr[3], arr[4], arr[5] = 50, 40, 30, 20, 10, 60 # Unsorted
SelectionSort(arr)
print("Array after Selection Sort:", arr)
# Insertion Sort
arr.realloc(6) # Reset array again
arr[0], arr[1], arr[2], arr[3], arr[4], arr[5] = 60, 50, 40, 30, 20, 10 # Unsorted
InsertionSort(arr)
print("Array after Insertion Sort:", arr)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flexarray-0.1.1.tar.gz.
File metadata
- Download URL: flexarray-0.1.1.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd4758b6330160e1f744578a889332ef58b651c73954c5c49f53a3b0675eda05
|
|
| MD5 |
3cc576afd5a6c6675635a921519c74f8
|
|
| BLAKE2b-256 |
a52d66e6469fac7858e87d06394129e29ac86d7a9639c36a048e2b1c44481252
|
File details
Details for the file flexarray-0.1.1-py3-none-any.whl.
File metadata
- Download URL: flexarray-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
529f06e5405f01227269200586279fa694ec290d5127eafe5e9ae9c397daaed5
|
|
| MD5 |
8696fa59808933b388d544ac3b180cac
|
|
| BLAKE2b-256 |
6149939afe4029832768b7af4ed36049407279dbd9f9af52848c2c4ee7e39f34
|