| import random | |
| import string | |
| def generate_string(length): | |
| """Generate a random string of lowercase letters""" | |
| return ''.join(random.choice(string.ascii_lowercase) for _ in range(length)) | |
| def generate_repetitive_string(length, pattern_length=3): | |
| """Generate a string with repetitive patterns""" | |
| pattern = ''.join(random.choice(string.ascii_lowercase) for _ in range(pattern_length)) | |
| return (pattern * (length // pattern_length + 1))[:length] | |
| def generate_palindromic_string(length): | |
| """Generate a palindromic string""" | |
| half = length // 2 | |
| first_half = ''.join(random.choice(string.ascii_lowercase) for _ in range(half)) | |
| if length % 2 == 0: | |
| return first_half + first_half[::-1] | |
| else: | |
| middle = random.choice(string.ascii_lowercase) | |
| return first_half + middle + first_half[::-1] | |
| def generate_operations(initial_length, num_ops, query_ratio=0.4, modify_ratio=0.3, insert_ratio=0.3): | |
| """Generate a sequence of operations""" | |
| 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(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| else: # Insert | |
| x = random.randint(0, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| return operations | |
| def generate_adversarial_case_1(): | |
| """Case 1: Long repetitive string with many queries""" | |
| length = random.randint(5000, 10000) | |
| s = generate_repetitive_string(length, 2) | |
| num_ops = random.randint(8000, 15000) | |
| ops = generate_operations(length, num_ops, query_ratio=0.7, modify_ratio=0.2, insert_ratio=0.1) | |
| return f"{s}\n{num_ops}\n" + "\n".join(ops) | |
| def generate_adversarial_case_2(): | |
| """Case 2: Palindromic string with alternating operations""" | |
| length = random.randint(3000, 8000) | |
| s = generate_palindromic_string(length) | |
| num_ops = random.randint(5000, 12000) | |
| ops = generate_operations(length, num_ops, query_ratio=0.5, modify_ratio=0.3, insert_ratio=0.2) | |
| return f"{s}\n{num_ops}\n" + "\n".join(ops) | |
| def generate_adversarial_case_3(): | |
| """Case 3: Single character repeated with heavy modifications""" | |
| length = random.randint(2000, 6000) | |
| char = random.choice(string.ascii_lowercase) | |
| s = char * length | |
| num_ops = random.randint(6000, 14000) | |
| ops = generate_operations(length, num_ops, query_ratio=0.3, modify_ratio=0.5, insert_ratio=0.2) | |
| return f"{s}\n{num_ops}\n" + "\n".join(ops) | |
| def generate_adversarial_case_4(): | |
| """Case 4: Random string with maximum insertions""" | |
| length = random.randint(1000, 3000) | |
| s = generate_string(length) | |
| num_ops = random.randint(10000, 15000) | |
| ops = generate_operations(length, num_ops, query_ratio=0.2, modify_ratio=0.2, insert_ratio=0.6) | |
| return f"{s}\n{num_ops}\n" + "\n".join(ops) | |
| def generate_adversarial_case_5(): | |
| """Case 5: Edge case with minimal string and maximum queries""" | |
| s = random.choice(string.ascii_lowercase) | |
| num_ops = 10000 | |
| operations = [] | |
| current_length = 1 | |
| for _ in range(num_ops): | |
| if current_length < 100 and random.random() < 0.3: | |
| # Insert operation | |
| x = random.randint(0, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| elif random.random() < 0.8: | |
| # Query operation | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| else: | |
| # Modify operation | |
| x = random.randint(1, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| return f"{s}\n{num_ops}\n" + "\n".join(operations) | |
| def generate_adversarial_case_6(): | |
| """Case 6: Alternating pattern with strategic queries""" | |
| length = random.randint(4000, 8000) | |
| s = ''.join('ab' for _ in range(length // 2)) | |
| if length % 2: | |
| s += 'a' | |
| num_ops = random.randint(7000, 12000) | |
| operations = [] | |
| current_length = length | |
| for _ in range(num_ops): | |
| op_type = random.choices(['Q', 'R', 'I'], weights=[0.6, 0.25, 0.15])[0] | |
| if op_type == 'Q': | |
| # Generate queries that might have long common prefixes | |
| 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('ab') # Keep pattern similar | |
| operations.append(f"R {x} {d}") | |
| else: | |
| x = random.randint(0, current_length) | |
| d = random.choice('ab') | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| return f"{s}\n{num_ops}\n" + "\n".join(operations) | |
| def construct_inputs(): | |
| inputs = [] | |
| # Generate multiple instances of each adversarial case | |
| for _ in range(3): | |
| 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()) | |
| inputs.append(generate_adversarial_case_6()) | |
| return inputs | |
Xet Storage Details
- Size:
- 5.85 kB
- Xet hash:
- fb3ccd29294c0fefeaf988b8b4f20c28c566a6f431b9e13286081ab1b2c66307
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.