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.
- 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
You can install AlgoMock using pip:
pip install algomock
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.
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.
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.
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
.
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
.
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
.
Inserts a newline character.
Parameters:
type
: Set to'newline'
Example:
{'type': 'newline'}
This will insert a newline character in the output.
Inserts a single space character.
Parameters:
type
: Set to'space'
Example:
{'type': 'space'}
This will insert a single space character in the output.
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
.
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
.
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.
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.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.