A flexible input generator for algorithm testing
Project description
AlgoMock
AlgoMock is a flexible and powerful input generator for algorithm testing and competitive programming. It allows you to easily create mock input data for various algorithm problems, helping you test your solutions more efficiently.
Features
- Generate various types of input: integers, strings, arrays, and 2D arrays
- Define input specifications using a simple and intuitive format
- Reference previously generated values in your specifications
- Customizable output format
- Support for loops and repetition in input generation
Installation
You can install AlgoMock using pip:
pip install algomock
Quick Start
Here's a simple example of how to use AlgoMock:
from algomock import generate_input
spec = [
{'type': 'int', 'limit': [1, 10], 'save_as': '$N'}, # N
{'type': 'space'},
{'type': 'int', 'limit': [1, 10], 'save_as': '$M'}, # M
{'type': 'newline'},
{'type': 'array', 'size': '$N', 'element_type': 'int', 'limit': [1, 100]}, # Array of size N
{'type': 'newline'},
{'type': '2d_array', 'rows': '$M', 'cols': '$N', 'element_type': 'int', 'limit': [0, 100]}, # 2D array of size M x N
]
input_data = generate_input(spec)
print(input_data)
This will generate input data according to the specification and print it in a format suitable for most algorithm problems.
Detailed Usage
AlgoMock uses a list of dictionaries to specify the input format. Each dictionary represents one element of the input. Below is a detailed explanation of each input type and how to use them.
Integer (int)
Generates a random integer within a specified range.
Parameters:
type: Set to'int'limit: A list of two integers[min, max]specifying the range (inclusive)save_as(optional): A variable name to save the generated value for later reference
Example:
{'type': 'int', 'limit': [1, 100], 'save_as': '$N'}
This will generate a random integer between 1 and 100 (inclusive) and save it as $N for later reference.
Array (array)
Generates an array of random integers.
Parameters:
type: Set to'array'size: The size of the array (can be a fixed number or a reference to a previously saved variable)element_type: Currently only supports'int'limit: A list of two integers[min, max]specifying the range for each element (inclusive)save_as(optional): A variable name to save the generated array for later reference
Example:
{'type': 'array', 'size': '$N', 'element_type': 'int', 'limit': [1, 1000], 'save_as': '$arr'}
This will generate an array of $N integers, each between 1 and 1000, and save it as $arr.
2D Array (2d_array)
Generates a 2D array (matrix) of random integers.
Parameters:
type: Set to'2d_array'rows: The number of rows in the 2D arraycols: The number of columns in the 2D arrayelement_type: Currently only supports'int'limit: A list of two integers[min, max]specifying the range for each element (inclusive)save_as(optional): A variable name to save the generated 2D array for later reference
Example:
{'type': '2d_array', 'rows': '$M', 'cols': '$N', 'element_type': 'int', 'limit': [0, 100], 'save_as': '$matrix'}
This will generate a 2D array with $M rows and $N columns, filled with integers between 0 and 100, and save it as $matrix.
String (string)
Generates a random string of lowercase letters.
Parameters:
type: Set to'string'length: The length of the string (can be a fixed number or a reference to a previously saved variable)save_as(optional): A variable name to save the generated string for later reference
Example:
{'type': 'string', 'length': 10, 'save_as': '$str'}
This will generate a random string of 10 lowercase letters and save it as $str.
Newline (newline)
Inserts a newline character.
Parameters:
type: Set to'newline'
Example:
{'type': 'newline'}
This will insert a newline character in the output.
Space (space)
Inserts a single space character.
Parameters:
type: Set to'space'
Example:
{'type': 'space'}
This will insert a single space character in the output.
Repeat Character (repeat_char)
Repeats a specified character a given number of times.
Parameters:
type: Set to'repeat_char'char: The character to repeatcount: The number of times to repeat the charactersave_as(optional): A variable name to save the generated string for later reference
Example:
{'type': 'repeat_char', 'char': '*', 'count': '$N', 'save_as': '$stars'}
This will repeat the * character $N times and save the result as $stars.
Repeat Type (repeat_type)
Repeats a specified input type generation a given number of times.
Parameters:
type: Set to'repeat_type'count: The number of times to repeat the specified input typespec: The specification of the input type to repeatsave_as(optional): A variable name to save the generated values for later reference
Example:
{'type': 'repeat_type', 'count': '$M', 'spec': {'type': 'int', 'limit': [0, 9]}, 'save_as': '$digits'}
This will generate $M random digits (0-9) and save them as $digits.
Loop (loop)
Repeats a set of input specifications a specified number of times.
Parameters:
type: Set to'loop'count: The number of times to repeat the loopspec: A list of input specifications to repeatsave_as(optional): A variable name to save the current loop index for use within the loop
Example:
{'type': 'loop', 'count': '$T', 'save_as': '$test_case', 'spec': [
{'type': 'int', 'limit': [1, 100], 'save_as': '$N'},
{'type': 'space'},
{'type': 'array', 'size': '$N', 'element_type': 'int', 'limit': [1, 1000]},
{'type': 'newline'}
]}
This will repeat the generation of an integer $N, a space, and an array of size $N for $T times, representing $T test cases.
Advanced Example
Here's a comprehensive example that demonstrates the use of various input types:
from algomock import InputGenerator
spec = [
{'type': 'int', 'limit': [1, 5], 'save_as': '$T'}, # Number of test cases
{'type': 'newline'},
{'type': 'loop', 'count': '$T', 'save_as': '$test_case', 'spec': [
{'type': 'int', 'limit': [1, 10], 'save_as': '$N'}, # N
{'type': 'space'},
{'type': 'int', 'limit': [1, '$N'], 'save_as': '$M'}, # M, upper limit depends on N
{'type': 'newline'},
{'type': 'repeat_char', 'char': '*', 'count': '$N', 'save_as': '$stars'}, # Repeat '*' N times
{'type': 'newline'},
{'type': 'repeat_type', 'count': '$M', 'spec': { # Repeat a random digit M times
'type': 'int',
'limit': [0, 9]
}, 'save_as': '$digits'},
{'type': 'newline'},
{'type': 'array', 'size': '$N', 'limit': [1, 100], 'save_as': '$array'}, # Array of size N
{'type': 'newline'},
{'type': '2d_array', 'rows': '$M', 'cols': '$N', 'limit': [0, 100], 'save_as': '$matrix'}, # 2D array of size M x N
{'type': 'newline'},
{'type': 'string', 'length': '$M', 'save_as': '$str'}, # String of length M
{'type': 'newline'},
{'type': 'int', 'limit': [0, '$test_case'], 'save_as': '$index'}, # An integer between 0 and the current loop index
{'type': 'newline'}
]}
]
generator = InputGenerator()
input_data = generator.generate_input(spec)
print(input_data)
This example generates multiple test cases, each with various types of input data, demonstrating the flexibility of AlgoMock.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for 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
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 algomock-0.2.0.tar.gz.
File metadata
- Download URL: algomock-0.2.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d569e099bfeeb333a6107fb850d032d1b2611f001d6fdf0c9f8cf8f0ad04cce1
|
|
| MD5 |
2ae93d1427b1605bc2fd766a60d3b2dc
|
|
| BLAKE2b-256 |
6a12cf854b25f35102c2af8627568d491389386409b4e28816babbffcd048fbf
|
File details
Details for the file algomock-0.2.0-py3-none-any.whl.
File metadata
- Download URL: algomock-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e26181febedb3117144f971fa09f0211fa844bd8c949997064a6aa568bb8b5f
|
|
| MD5 |
93e2ccd2ecf6311e9efc1836c33f6927
|
|
| BLAKE2b-256 |
b011d621f86a8dd5a4f29861bda6fa3a3af1f4576a090c32d42a118e6906536b
|