-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·60 lines (50 loc) · 2.39 KB
/
generate.py
File metadata and controls
executable file
·60 lines (50 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import random
import argparse
def generate_fenwick_tree_input(size, num_operations, output_file, query_percentage=20):
"""
Generate a test input file for the Fenwick Tree implementation.
Args:
size: Size of the Fenwick tree
num_operations: Number of operations to generate
output_file: Path to the output file
query_percentage: Percentage of operations that should be queries (default: 20%)
"""
with open(output_file, 'w') as f:
# Write the size of the Fenwick tree
f.write(f"{size} {num_operations}\n")
# Generate random operations
for _ in range(num_operations):
# Decide operation type with probabilities
op_type = random.choices(['a', 'q'],
weights=[(100 - query_percentage),
query_percentage],
k=1)[0]
# Generate random index and value
index = random.randint(0, size - 1)
if op_type == 'q':
# Query operation
f.write(f"q {index}\n")
else:
# Add or delete operation
value = random.randint(1, 100)
f.write(f"{op_type} {index} {value}\n")
def main():
parser = argparse.ArgumentParser(description='Generate test input for Fenwick Tree')
parser.add_argument('--size', type=int, default=128, help='Size of the Fenwick tree')
parser.add_argument('--operations', type=int, default=1000, help='Number of operations to generate')
parser.add_argument('--output', type=str, default='input.txt', help='Output file path')
parser.add_argument('--queries', type=int, default=20, help='Percentage of query operations (0-100)')
args = parser.parse_args()
# Validate input
if args.size <= 0:
parser.error("Size must be positive")
if args.operations <= 0:
parser.error("Number of operations must be positive")
if not 0 <= args.queries <= 100:
parser.error("Query percentage must be between 0 and 100")
generate_fenwick_tree_input(args.size, args.operations, args.output, args.queries)
print(f"Generated {args.operations} operations for a Fenwick tree of size {args.size}")
print(f"Output written to {args.output}")
if __name__ == "__main__":
main()