Tsukihjy's picture
download
raw
5.88 kB
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
left_half = ''.join(random.choice(string.ascii_lowercase) for _ in range(half))
if length % 2 == 0:
return left_half + left_half[::-1]
else:
middle = random.choice(string.ascii_lowercase)
return left_half + middle + left_half[::-1]
def generate_operations(string_length, num_ops):
"""Generate a mix of operations"""
operations = []
current_length = string_length
for _ in range(num_ops):
op_type = random.choices(['Q', 'R', 'I'], weights=[0.4, 0.3, 0.3])[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
if current_length > 100000: # Prevent string from getting too long
current_length -= 1
operations[-1] = f"Q {random.randint(1, current_length)} {random.randint(1, current_length)}"
return operations
def generate_adversarial_operations(string_length, num_ops):
"""Generate adversarial operations that stress the LCQ function"""
operations = []
current_length = string_length
# Add many queries at the beginning and end of string
for _ in range(min(num_ops // 4, 50)):
operations.append(f"Q 1 {random.randint(1, current_length)}")
operations.append(f"Q {current_length} {random.randint(1, current_length)}")
# Add modifications that create/destroy patterns
for _ in range(min(num_ops // 4, 50)):
x = random.randint(1, current_length)
# Alternate between 'a' and other characters to create patterns
d = 'a' if random.random() < 0.7 else random.choice('bcdefghijklmnopqrstuvwxyz')
operations.append(f"R {x} {d}")
# Add insertions at strategic positions
for _ in range(min(num_ops // 4, 50)):
x = random.choice([0, 1, current_length // 2, current_length])
d = random.choice(string.ascii_lowercase)
operations.append(f"I {x} {d}")
current_length += 1
if current_length > 100000:
current_length -= 1
operations[-1] = f"Q {random.randint(1, current_length)} {random.randint(1, current_length)}"
# Fill remaining with queries
remaining = num_ops - len(operations)
for _ in range(remaining):
x = random.randint(1, current_length)
y = random.randint(1, current_length)
operations.append(f"Q {x} {y}")
random.shuffle(operations)
return operations
def construct_inputs():
inputs = []
# Test case 1: Small string with many operations
s = generate_string(10)
ops = generate_operations(10, 20)
test_input = f"{s}\n{len(ops)}\n" + "\n".join(ops)
inputs.append(test_input)
# Test case 2: Repetitive pattern string
s = generate_repetitive_string(50, 3)
ops = generate_adversarial_operations(50, 100)
test_input = f"{s}\n{len(ops)}\n" + "\n".join(ops)
inputs.append(test_input)
# Test case 3: Palindromic string
s = generate_palindromic_string(30)
ops = generate_operations(30, 80)
test_input = f"{s}\n{len(ops)}\n" + "\n".join(ops)
inputs.append(test_input)
# Test case 4: Large string with maximum operations
s = generate_string(1000)
ops = generate_adversarial_operations(1000, 10000)
test_input = f"{s}\n{len(ops)}\n" + "\n".join(ops)
inputs.append(test_input)
# Test case 5: String with all same characters
s = 'a' * 100
ops = generate_operations(100, 500)
test_input = f"{s}\n{len(ops)}\n" + "\n".join(ops)
inputs.append(test_input)
# Test case 6: String that grows significantly through insertions
s = generate_string(20)
ops = []
current_len = 20
for i in range(1000):
if random.random() < 0.6 and current_len < 10000: # Insert
x = random.randint(0, current_len)
d = random.choice(string.ascii_lowercase)
ops.append(f"I {x} {d}")
current_len += 1
elif random.random() < 0.8: # Query
x = random.randint(1, current_len)
y = random.randint(1, current_len)
ops.append(f"Q {x} {y}")
else: # Replace
x = random.randint(1, current_len)
d = random.choice(string.ascii_lowercase)
ops.append(f"R {x} {d}")
test_input = f"{s}\n{len(ops)}\n" + "\n".join(ops)
inputs.append(test_input)
# Test case 7: Edge case with single character
s = 'x'
ops = ['Q 1 1', 'R 1 y', 'Q 1 1', 'I 0 z', 'Q 1 2', 'I 2 z', 'Q 1 3']
test_input = f"{s}\n{len(ops)}\n" + "\n".join(ops)
inputs.append(test_input)
# Test case 8: Maximum constraints
s = generate_repetitive_string(10000, 5)
ops = generate_adversarial_operations(10000, 150000)
test_input = f"{s}\n{len(ops)}\n" + "\n".join(ops)
inputs.append(test_input)
return inputs

Xet Storage Details

Size:
5.88 kB
·
Xet hash:
1754ce279b7c036f64f8a44667b7d9b6128c785bc1d6736c8f358ec182df51ca

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.