Problem p02422 - Generation 1

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 arbitrary string of lowercase letters with a length between 1 and 1000.
An integer q (1 ≤ q ≤ 100), representing some quantity or size.
A list of q commands, where each command is either:
- "print a b", with integers 0 ≤ a ≤ b < length of the string
- "reverse a b", with integers 0 ≤ a ≤ b < length of the string
- "replace a b p", with integers 0 ≤ a ≤ b < length of the string and a string p such that (b - a + 1) = length of p

### Example Input:

```
abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4
```

### Function Signature:
Write a function f(str, q, commands) that takes in the input.
```python
def f(str: str, q: int, commands: List[str]):
    '''
    str: a string
    q: an integer
    commands: a list of strings
    '''
```

Test Case Generator

Sure, here's a test case generator for the `Transformation` problem:

```python
import random
import string

def tcgen():
    # Generate a random string of length between 1 and 1000.
    str_len = random.randint(1, 1000)
    str_val = ''.join(random.choices(string.ascii_lowercase, k=str_len))

    # Generate a random number of commands between 1 and 100.
    q = random.randint(1, 100)
    commands = []
    
    for _ in range(q):
        # Randomly choose a command type.
        command_type = random.choice(['print', 'reverse', 'replace'])
        
        # Generate random indices a and b such that 0 <= a <= b < length of str.
        a = random.randint(0, str_len - 1)
        b = random.randint(a, str_len - 1)
        
        if command_type == 'print':
            command = f"print {a} {b}"
        elif command_type == 'reverse':
            command = f"reverse {a} {b}"
        elif command_type == 'replace':
            # Generate a random string p of length b - a + 1.
            p_length = b - a + 1
            p = ''.join(random.choices(string.ascii_lowercase, k=p_length))
            command = f"replace {a} {b} {p}"
        
        commands.append(command)
    
    return str_val, q, commands

# Example usage:
str_val, q, commands = tcgen()
print(str_val)
print(q)
for command in commands:
    print(command)
```

This script generates a random string `str` of length between 1 and 1000, and a sequence of `q` commands (between 1 and 100). Each command is randomly chosen to be either `print`, `reverse`, or `replace`, with valid random indices `a` and `b`. For the `replace` command, a random string `p` of appropriate length is generated. The generated test case includes the initial string, the number of commands, and the list of commands.

You can run the `tcgen` function to generate a new test case each time and see the output by using the example usage snippet provided in the script.

Extract Arguments

def extract_arguments(fh):
    str_input = fh.readline().strip()
    q = int(fh.readline().strip())
    commands = []
    for _ in range(q):
        command = fh.readline().strip().split()
        if command[0] == 'print' or command[0] == 'reverse':
            a, b = int(command[1]), int(command[2])
            commands.append((command[0], a, b))
        elif command[0] == 'replace':
            a, b, p = int(command[1]), int(command[2]), command[3]
            commands.append((command[0], a, b, p))
    return str_input, q, commands

# Example usage:
# with open('input.txt', 'r') as fh:
#     str_input, q, commands = extract_arguments(fh)
#     print(str_input, q, commands)