Orig Description
Transformation
Write a program which performs a sequence of commands to a given string $str$. The command is one of:
print a b: print from the a-th character to the b-th character of $str$
reverse a b: reverse from the a-th character to the b-th character of $str$
replace a b p: replace from the a-th character to the b-th character of $str$ with p
Note that the indices of $str$ start with 0.
Input
In the first line, a string $str$ is given. $str$ consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format.
Output
For each print command, print a string in a line.
Constraints
$1 \leq $ length of $str \leq 1000$
$1 \leq q \leq 100$
$0 \leq a \leq b < $ length of $str$
for replace command, $b - a + 1 = $ length of $p$
Sample Input 1
abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4
Sample Output 1
xaze
Sample Input 2
xyz
3
print 0 2
replace 0 2 abc
print 0 2
Sample Output 2
xyz
abc
Extracted Specification
An integer \(N\) (1 ≤ \(N\) ≤ 1000), representing the length of a string.
A string \(S\) of length \(N\), consisting of lowercase letters.
An integer \(Q\) (1 ≤ \(Q\) ≤ 100), representing the number of commands.
A list of \(Q\) commands, each command can be one of the following:
- "print a b": where \(0 \leq a \leq b < N\)
- "reverse a b": where \(0 \leq a \leq b < N\)
- "replace a b p": where \(0 \leq a \leq b < N\) and \(b - a + 1 = \) length of \(p\), and \(p\) is a string of lowercase letters.
### Example Input:
```
5
abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4
```
### Function Signature:
Write a function f(N, S, Q, commands) that takes in the input.
```python
def f(N: int, S: str, Q: int, commands: List[str]):
'''
N: an integer
S: a string
Q: an integer
commands: a list of strings
'''
```
Test Case Generator
Sure, here is a test case generator for the given problem statement:
```python
import random
import string
def generate_random_string(length):
return ''.join(random.choices(string.ascii_lowercase, k=length))
def generate_commands(str_length, q):
commands = []
for _ in range(q):
command_type = random.choice(['print', 'reverse', 'replace'])
a = random.randint(0, str_length - 1)
b = random.randint(a, str_length - 1)
if command_type == 'print':
commands.append(f"print {a} {b}")
elif command_type == 'reverse':
commands.append(f"reverse {a} {b}")
elif command_type == 'replace':
p = generate_random_string(b - a + 1)
commands.append(f"replace {a} {b} {p}")
return commands
def tcgen():
# Generate a random string of length between 1 and 1000
str_length = random.randint(1, 1000)
str_input = generate_random_string(str_length)
# Generate the number of commands between 1 and 100
q = random.randint(1, 100)
# Generate the commands
commands = generate_commands(str_length, q)
return str_input, q, commands
# Example usage
if __name__ == "__main__":
str_input, q, commands = tcgen()
print(str_input)
print(q)
for command in commands:
print(command)
```
This test case generator performs the following steps:
1. Generates a random string `str` of length between 1 and 1000 consisting of lowercase letters.
2. Determines the number of commands `q` between 1 and 100.
3. For each command, randomly selects a command type (`print`, `reverse`, or `replace`), and generates appropriate parameters (indices `a` and `b`, and string `p` for `replace` commands) based on the constraints provided.
4. Returns the generated string, the number of commands, and the list of commands.
This should adequately cover a variety of potential test cases for the problem.
Extract Arguments
def extract_arguments(fh):
str_input = fh.readline().strip()
q = int(fh.readline().strip())
commands = []
for _ in range(q):
commands.append(fh.readline().strip().split())
return str_input, q, commands
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# str_input, q, commands = extract_arguments(fh)
# f(str_input, q, commands)