Tsukihjy's picture
download
raw
6.78 kB
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: Many queries on highly repetitive string
initial_string = generate_string(1000, "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: Alternating pattern with many modifications
initial_string = generate_string(5000, "alternating")
operations = []
current_length = len(initial_string)
for _ in range(7500):
if random.random() < 0.6: # 60% modifications
x = random.randint(1, current_length)
d = random.choice('ab') # Keep alternating pattern
operations.append(f"R {x} {d}")
else: # 40% queries
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: Palindromic string with edge case queries
initial_string = generate_string(3000, "palindrome")
operations = []
current_length = len(initial_string)
# Add many edge case queries (beginning and end positions)
for _ in range(3000):
if random.random() < 0.7: # 70% queries
if random.random() < 0.5:
# Query at edges
x = random.choice([1, current_length])
y = random.randint(1, current_length)
else:
# Random queries
x = random.randint(1, current_length)
y = random.randint(1, current_length)
operations.append(f"Q {x} {y}")
else: # 30% insertions at beginning/end
x = random.choice([0, current_length])
d = random.choice('abcdefghijklmnopqrstuvwxyz')
operations.append(f"I {x} {d}")
current_length += 1
return initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations)
def generate_adversarial_case_4():
# Case 4: Periodic pattern with mixed operations
initial_string = generate_string(8000, "periodic")
operations = generate_operations(len(initial_string), 10000, 0.5, 0.25, 0.25)
return initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations)
def generate_adversarial_case_5():
# Case 5: Maximum constraints stress test
initial_string = generate_string(100000, "random")
operations = []
current_length = len(initial_string)
# Maximum number of operations with high query ratio
for _ in range(150000):
if random.random() < 0.067: # ~10000 queries out of 150000 operations
x = random.randint(1, current_length)
y = random.randint(1, current_length)
operations.append(f"Q {x} {y}")
elif random.random() < 0.5: # Modifications
x = random.randint(1, current_length)
d = random.choice('abcdefghijklmnopqrstuvwxyz')
operations.append(f"R {x} {d}")
else: # Insertions (but limit to keep length <= 10^5)
if current_length < 100000:
x = random.randint(0, current_length)
d = random.choice('abcdefghijklmnopqrstuvwxyz')
operations.append(f"I {x} {d}")
current_length += 1
else:
# If at max length, do modification instead
x = random.randint(1, current_length)
d = random.choice('abcdefghijklmnopqrstuvwxyz')
operations.append(f"R {x} {d}")
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", "alternating", "palindrome", "periodic"]:
for length in [100, 1000, 5000]:
initial_string = generate_string(length, pattern)
operations = generate_operations(length, min(1000, length // 2))
test_case = initial_string + "\n" + str(len(operations)) + "\n" + "\n".join(operations)
inputs.append(test_case)
return inputs

Xet Storage Details

Size:
6.78 kB
·
Xet hash:
9e1b9fa8ef5886ca2e842dbed8f43d2e081bee9cfb519e431335a1a37ba5e008

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