| import random | |
| import string | |
| def generate_string(length, pattern_type="random"): | |
| if pattern_type == "random": | |
| return ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(length)) | |
| elif pattern_type == "repetitive": | |
| base = random.choice('abcdefghijklmnopqrstuvwxyz') | |
| return base * length | |
| elif pattern_type == "alternating": | |
| chars = random.sample('abcdefghijklmnopqrstuvwxyz', 2) | |
| return ''.join(chars[i % 2] for i in range(length)) | |
| elif pattern_type == "palindrome": | |
| half = ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(length // 2)) | |
| if length % 2 == 0: | |
| return half + half[::-1] | |
| else: | |
| return half + random.choice('abcdefghijklmnopqrstuvwxyz') + half[::-1] | |
| elif pattern_type == "periodic": | |
| period = random.randint(2, min(10, length)) | |
| pattern = ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(period)) | |
| return (pattern * (length // period + 1))[:length] | |
| def generate_operations(initial_length, num_ops, query_ratio=0.4, modify_ratio=0.3, insert_ratio=0.3): | |
| operations = [] | |
| current_length = initial_length | |
| for _ in range(num_ops): | |
| op_type = random.choices(['Q', 'R', 'I'], weights=[query_ratio, modify_ratio, insert_ratio])[0] | |
| if op_type == 'Q': | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| elif op_type == 'R': | |
| x = random.randint(1, current_length) | |
| d = random.choice('abcdefghijklmnopqrstuvwxyz') | |
| operations.append(f"R {x} {d}") | |
| elif op_type == 'I': | |
| x = random.randint(0, current_length) | |
| d = random.choice('abcdefghijklmnopqrstuvwxyz') | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| return operations | |
| def generate_adversarial_case_1(): | |
| # Case 1: Long repetitive string with many queries | |
| initial_string = generate_string(50000, "repetitive") | |
| operations = [] | |
| for _ in range(5000): | |
| x = random.randint(1, len(initial_string)) | |
| y = random.randint(1, len(initial_string)) | |
| operations.append(f"Q {x} {y}") | |
| return initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations) | |
| def generate_adversarial_case_2(): | |
| # Case 2: Periodic string with strategic modifications | |
| initial_string = generate_string(30000, "periodic") | |
| operations = [] | |
| current_length = len(initial_string) | |
| # Add many modifications that break periodicity | |
| for _ in range(3000): | |
| x = random.randint(1, current_length) | |
| d = random.choice('abcdefghijklmnopqrstuvwxyz') | |
| operations.append(f"R {x} {d}") | |
| # Add queries after modifications | |
| for _ in range(2000): | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| return initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations) | |
| def generate_adversarial_case_3(): | |
| # Case 3: Many insertions followed by queries | |
| initial_string = generate_string(10000, "random") | |
| operations = [] | |
| current_length = len(initial_string) | |
| # Many insertions at strategic positions | |
| for _ in range(40000): | |
| x = random.randint(0, current_length) | |
| d = random.choice('abcdefghijklmnopqrstuvwxyz') | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| if current_length >= 100000: | |
| break | |
| # Queries on the expanded string | |
| for _ in range(min(10000, 150000 - len(operations))): | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| return initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations) | |
| def generate_adversarial_case_4(): | |
| # Case 4: Palindromic string with edge case queries | |
| initial_string = generate_string(20000, "palindrome") | |
| operations = [] | |
| current_length = len(initial_string) | |
| # Mix of all operations with emphasis on edge positions | |
| for _ in range(30000): | |
| op_type = random.choice(['Q', 'R', 'I']) | |
| if op_type == 'Q': | |
| # Focus on edge cases and symmetric positions | |
| if random.random() < 0.3: | |
| x, y = 1, current_length | |
| elif random.random() < 0.3: | |
| mid = current_length // 2 | |
| x, y = mid, mid + 1 | |
| else: | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| elif op_type == 'R': | |
| x = random.choice([1, current_length, random.randint(1, current_length)]) | |
| d = random.choice('abcdefghijklmnopqrstuvwxyz') | |
| operations.append(f"R {x} {d}") | |
| elif op_type == 'I': | |
| x = random.choice([0, current_length, random.randint(0, current_length)]) | |
| d = random.choice('abcdefghijklmnopqrstuvwxyz') | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| if current_length >= 100000: | |
| break | |
| return initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations) | |
| def generate_adversarial_case_5(): | |
| # Case 5: Alternating pattern with maximum queries | |
| initial_string = generate_string(50000, "alternating") | |
| operations = [] | |
| # Maximum number of queries allowed | |
| for _ in range(10000): | |
| x = random.randint(1, len(initial_string)) | |
| y = random.randint(1, len(initial_string)) | |
| operations.append(f"Q {x} {y}") | |
| return initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations) | |
| def construct_inputs(): | |
| inputs = [] | |
| # Add the example case | |
| example = """madamimadam | |
| 7 | |
| Q 1 7 | |
| Q 4 8 | |
| Q 10 11 | |
| R 3 a | |
| Q 1 7 | |
| I 10 a | |
| Q 2 11""" | |
| inputs.append(example) | |
| # Add adversarial cases | |
| inputs.append(generate_adversarial_case_1()) | |
| inputs.append(generate_adversarial_case_2()) | |
| inputs.append(generate_adversarial_case_3()) | |
| inputs.append(generate_adversarial_case_4()) | |
| inputs.append(generate_adversarial_case_5()) | |
| # Add some smaller test cases with different patterns | |
| for pattern in ["random", "repetitive", "periodic"]: | |
| for length in [1000, 5000]: | |
| initial_string = generate_string(length, pattern) | |
| operations = generate_operations(length, min(1000, 150000), 0.6, 0.2, 0.2) | |
| test_case = initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations) | |
| inputs.append(test_case) | |
| return inputs | |
Xet Storage Details
- Size:
- 6.76 kB
- Xet hash:
- 73b0c959cdd72a71aeb07b0ce98aa88602a6e61eab14826cd704903160f27ca2
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.