Tsukihjy's picture
download
raw
5.03 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 exceeding limit
current_length -= 1
operations.pop()
return operations
def generate_adversarial_queries(string_length, num_queries):
"""Generate adversarial query patterns"""
operations = []
# Edge case queries
operations.append(f"Q 1 1") # Same position
operations.append(f"Q 1 {string_length}") # Start and end
operations.append(f"Q {string_length} {string_length}") # Last position
# Adjacent positions
for i in range(min(5, string_length - 1)):
pos = random.randint(1, string_length - 1)
operations.append(f"Q {pos} {pos + 1}")
# Random queries for remaining
for _ in range(num_queries - len(operations)):
x = random.randint(1, string_length)
y = random.randint(1, string_length)
operations.append(f"Q {x} {y}")
return operations
def construct_inputs():
inputs = []
# Test case 1: Small string with many operations
s1 = "abcabc"
ops1 = [
"Q 1 4",
"Q 2 5",
"R 3 a",
"Q 1 4",
"I 0 x",
"Q 2 5"
]
input1 = s1 + "\n" + str(len(ops1)) + "\n" + "\n".join(ops1)
inputs.append(input1)
# Test case 2: Repetitive pattern string
s2 = generate_repetitive_string(50, 3)
ops2 = generate_adversarial_queries(50, 20) + [
"R 25 z",
"I 25 y",
"Q 1 26",
"Q 24 27"
]
input2 = s2 + "\n" + str(len(ops2)) + "\n" + "\n".join(ops2)
inputs.append(input2)
# Test case 3: Palindromic string
s3 = generate_palindromic_string(80)
ops3 = []
# Query symmetric positions
for i in range(10):
pos1 = random.randint(1, 40)
pos2 = 81 - pos1
ops3.append(f"Q {pos1} {pos2}")
ops3.extend([
"R 40 x",
"I 40 y",
"Q 1 82"
])
input3 = s3 + "\n" + str(len(ops3)) + "\n" + "\n".join(ops3)
inputs.append(input3)
# Test case 4: Large string with mixed operations
s4 = generate_string(1000)
ops4 = generate_operations(1000, 100)
input4 = s4 + "\n" + str(len(ops4)) + "\n" + "\n".join(ops4)
inputs.append(input4)
# Test case 5: Maximum constraints
s5 = generate_string(10000)
ops5 = []
# Many queries first
ops5.extend(generate_adversarial_queries(10000, 50))
# Then modifications
for _ in range(100):
if random.random() < 0.5:
x = random.randint(1, len(s5))
d = random.choice(string.ascii_lowercase)
ops5.append(f"R {x} {d}")
else:
x = random.randint(0, len(s5))
d = random.choice(string.ascii_lowercase)
ops5.append(f"I {x} {d}")
input5 = s5 + "\n" + str(len(ops5)) + "\n" + "\n".join(ops5)
inputs.append(input5)
# Test case 6: All same character
s6 = "a" * 100
ops6 = [
"Q 1 50",
"Q 25 75",
"R 50 b",
"Q 1 50",
"Q 51 100",
"I 50 c",
"Q 1 51"
]
input6 = s6 + "\n" + str(len(ops6)) + "\n" + "\n".join(ops6)
inputs.append(input6)
# Test case 7: Alternating pattern
s7 = "ab" * 500
ops7 = generate_operations(1000, 80)
input7 = s7 + "\n" + str(len(ops7)) + "\n" + "\n".join(ops7)
inputs.append(input7)
return inputs

Xet Storage Details

Size:
5.03 kB
·
Xet hash:
992f9b3fe1041256ab64226f7918c231cc39ecf7573c742805293ec1707b6441

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