| import random | |
| import string | |
| def generate_random_string(length): | |
| return ''.join(random.choice(string.ascii_lowercase) for _ in range(length)) | |
| def generate_operation(current_length, query_count, max_queries): | |
| operations = [] | |
| # Choose operation type with weighted probabilities | |
| if query_count >= max_queries: | |
| # No more queries allowed | |
| op_type = random.choices(['R', 'I'], weights=[0.6, 0.4])[0] | |
| else: | |
| op_type = random.choices(['Q', 'R', 'I'], weights=[0.5, 0.3, 0.2])[0] | |
| if op_type == 'Q': | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| return operations, current_length, query_count + 1 | |
| elif op_type == 'R': | |
| x = random.randint(1, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| return operations, current_length, query_count | |
| else: # op_type == 'I' | |
| x = random.randint(0, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"I {x} {d}") | |
| return operations, current_length + 1, query_count | |
| def generate_test_case(initial_length, num_operations, max_queries): | |
| # Generate initial string | |
| initial_string = generate_random_string(initial_length) | |
| # Generate operations | |
| operations = [] | |
| current_length = initial_length | |
| query_count = 0 | |
| for _ in range(num_operations): | |
| if current_length >= 100000: # Prevent string from getting too long | |
| # Only allow queries and replacements | |
| if query_count < max_queries: | |
| op_type = random.choices(['Q', 'R'], weights=[0.7, 0.3])[0] | |
| else: | |
| op_type = 'R' | |
| if op_type == 'Q': | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| query_count += 1 | |
| else: | |
| x = random.randint(1, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| else: | |
| ops, current_length, query_count = generate_operation(current_length, query_count, max_queries) | |
| operations.extend(ops) | |
| # Format the test case | |
| result = initial_string + '\n' | |
| result += str(len(operations)) + '\n' | |
| result += '\n'.join(operations) | |
| return result | |
| def construct_inputs(): | |
| inputs = [] | |
| # Small test cases | |
| for _ in range(10): | |
| initial_length = random.randint(1, 20) | |
| num_operations = random.randint(1, 30) | |
| max_queries = min(10, num_operations) | |
| inputs.append(generate_test_case(initial_length, num_operations, max_queries)) | |
| # Medium test cases | |
| for _ in range(15): | |
| initial_length = random.randint(20, 1000) | |
| num_operations = random.randint(50, 5000) | |
| max_queries = min(100, num_operations // 5) | |
| inputs.append(generate_test_case(initial_length, num_operations, max_queries)) | |
| # Large test cases | |
| for _ in range(10): | |
| initial_length = random.randint(1000, 50000) | |
| num_operations = random.randint(10000, 150000) | |
| max_queries = min(10000, num_operations // 10) | |
| inputs.append(generate_test_case(initial_length, num_operations, max_queries)) | |
| # Edge cases | |
| # Single character string | |
| inputs.append("a\n3\nQ 1 1\nR 1 b\nQ 1 1") | |
| # Maximum operations with minimal queries | |
| initial_string = generate_random_string(10) | |
| operations = [] | |
| for i in range(149999): | |
| if i % 1000 == 0 and len([op for op in operations if op.startswith('Q')]) < 10000: | |
| x = random.randint(1, 10) | |
| y = random.randint(1, 10) | |
| operations.append(f"Q {x} {y}") | |
| else: | |
| if random.random() < 0.7: | |
| x = random.randint(1, 10) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| else: | |
| x = random.randint(0, 10) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"I {x} {d}") | |
| edge_case = initial_string + '\n' + str(len(operations)) + '\n' + '\n'.join(operations) | |
| inputs.append(edge_case) | |
| return inputs | |
Xet Storage Details
- Size:
- 4.41 kB
- Xet hash:
- e2239ab38477aa1c1ed1783f6de4114bcec19ee978b95f56239774a86d886aa9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.