| 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_len=3): | |
| """Generate a string with repetitive patterns""" | |
| pattern = ''.join(random.choice(string.ascii_lowercase) for _ in range(pattern_len)) | |
| return (pattern * (length // pattern_len + 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): | |
| """Generate a mix of operations""" | |
| operations = [] | |
| current_length = initial_length | |
| query_count = 0 | |
| max_queries = min(num_ops // 3, 10000) # Limit queries as per constraint | |
| for _ in range(num_ops): | |
| if query_count >= max_queries: | |
| # Only do modifications and insertions | |
| op_type = random.choice(['R', 'I']) | |
| else: | |
| op_type = random.choice(['Q', 'R', 'I']) | |
| if op_type == 'Q' and current_length > 0: | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| query_count += 1 | |
| elif op_type == 'R' and current_length > 0: | |
| x = random.randint(1, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| elif op_type == 'I': | |
| 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""" | |
| initial_string = generate_repetitive_string(50000, 2) | |
| num_ops = 150000 | |
| operations = [] | |
| current_length = len(initial_string) | |
| # Generate mostly queries on similar positions | |
| for i in range(min(10000, num_ops)): | |
| x = random.randint(1, min(1000, current_length)) | |
| y = random.randint(1, min(1000, current_length)) | |
| operations.append(f"Q {x} {y}") | |
| # Fill remaining with modifications | |
| for i in range(len(operations), num_ops): | |
| if random.random() < 0.7: # 70% modifications | |
| x = random.randint(1, current_length) | |
| d = random.choice('ab') # Limited alphabet | |
| operations.append(f"R {x} {d}") | |
| else: # 30% insertions | |
| x = random.randint(0, current_length) | |
| d = random.choice('ab') | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| return initial_string, operations | |
| def generate_adversarial_case_2(): | |
| """Case 2: Palindromic string with edge case queries""" | |
| initial_string = generate_palindromic_string(30000) | |
| num_ops = 100000 | |
| operations = [] | |
| current_length = len(initial_string) | |
| # Generate queries that test palindrome properties | |
| for i in range(min(8000, num_ops)): | |
| if random.random() < 0.5: | |
| # Query symmetric positions | |
| pos = random.randint(1, current_length // 2) | |
| mirror_pos = current_length - pos + 1 | |
| operations.append(f"Q {pos} {mirror_pos}") | |
| else: | |
| # Random queries | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| # Add modifications and insertions | |
| for i in range(len(operations), num_ops): | |
| op_type = random.choice(['R', 'I']) | |
| if op_type == 'R': | |
| x = random.randint(1, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| else: | |
| x = random.randint(0, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| return initial_string, operations | |
| def generate_adversarial_case_3(): | |
| """Case 3: Many insertions at beginning/end""" | |
| initial_string = generate_string(10000) | |
| num_ops = 120000 | |
| operations = [] | |
| current_length = len(initial_string) | |
| # Some initial queries | |
| for i in range(min(5000, num_ops)): | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| # Many insertions at edges | |
| for i in range(len(operations), num_ops): | |
| if random.random() < 0.6: # 60% insertions | |
| if random.random() < 0.5: | |
| x = 0 # Insert at beginning | |
| else: | |
| x = current_length # Insert at end | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"I {x} {d}") | |
| current_length += 1 | |
| else: # 40% modifications | |
| x = random.randint(1, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| return initial_string, operations | |
| def construct_inputs(): | |
| inputs_list = [] | |
| # Generate multiple adversarial cases | |
| for case_func in [generate_adversarial_case_1, generate_adversarial_case_2, generate_adversarial_case_3]: | |
| initial_string, operations = case_func() | |
| input_str = initial_string + '\n' | |
| input_str += str(len(operations)) + '\n' | |
| input_str += '\n'.join(operations) | |
| inputs_list.append(input_str) | |
| # Generate some smaller test cases with specific patterns | |
| for _ in range(3): | |
| length = random.randint(1000, 5000) | |
| initial_string = generate_string(length) | |
| num_ops = random.randint(10000, 50000) | |
| operations = generate_operations(length, num_ops) | |
| input_str = initial_string + '\n' | |
| input_str += str(len(operations)) + '\n' | |
| input_str += '\n'.join(operations) | |
| inputs_list.append(input_str) | |
| return inputs_list | |
Xet Storage Details
- Size:
- 6.31 kB
- Xet hash:
- 948c723444f036b922e65e32fd1303c41f1be9089587d08a84dd7b16ebaf0b44
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.