19

4

In APL, you can write tacit functions, called *trains*. How they work is irrelevant for this challenge. Here are the different ways they can be grouped, using `⍴`

as the function:

```
⍴ -> ⍴
⍴⍴ -> ⍴⍴
⍴⍴⍴ -> ⍴⍴⍴
⍴⍴⍴⍴ -> ⍴(⍴⍴⍴)
⍴⍴⍴⍴⍴ -> ⍴⍴(⍴⍴⍴)
⍴⍴⍴⍴⍴⍴ -> ⍴(⍴⍴(⍴⍴⍴))
...
```

The order remains the same. The procedure is that as long as there are strictly more than 3 functions, the last 3 functions are grouped into one function. If we meet a nested train, we parenthesize that first, before continuing. Here is the procedure applied to `⍴⍴⍴⍴⍴⍴`

:

```
Step 0: ⍴⍴⍴⍴⍴⍴
There are strictly more than 3 functions, repeat.
Step 1: ⍴⍴⍴(⍴⍴⍴)
There are strictly more than 3 functions, repeat.
Step 2: ⍴(⍴⍴(⍴⍴⍴))
There are 3 or less functions, we're done.
```

Here is the same procedure applied to `⍴⍴⍴(⍴⍴)⍴(⍴⍴⍴⍴(⍴⍴⍴))⍴⍴`

:

```
Step 0: ⍴⍴⍴(⍴⍴)⍴(⍴⍴⍴⍴(⍴⍴⍴))⍴⍴
There are strictly more than 3 functions, repeat.
We have met a nested train, applying procedure to that first:
Step 0: ⍴⍴⍴⍴(⍴⍴⍴)
There are strictly more than 3 functions, repeat.
We have met a nested train, applying procedure to that first:
Step 0: ⍴⍴⍴
There are 3 or less functions, we're done.
Step 1: ⍴⍴(⍴⍴(⍴⍴⍴))
There are 3 or less functions, we're done.
Step 1: ⍴⍴⍴(⍴⍴)⍴((⍴⍴(⍴⍴(⍴⍴⍴)))⍴⍴)
There are strictly more than 3 functions, repeat.
We have met a nested train, applying procedure to that first:
Step 0: ⍴⍴
There are 3 or less functions, we're done.
Step 2: ⍴⍴⍴((⍴⍴)⍴((⍴⍴(⍴⍴(⍴⍴⍴)))⍴⍴))
There are strictly more than 3 functions, repeat.
Step 3: ⍴(⍴⍴((⍴⍴)⍴((⍴⍴(⍴⍴(⍴⍴⍴)))⍴⍴)))
There are 3 functions or less, we're done.
```

# Input

For this challenge, input will be simplified. This means that you can choose 2 different chars for opening and closing parentheses and 1 char for functions, different from the ones chosen for parentheses. The chars you choose must be consistent. The input will not be empty, and will not contain parentheses with no content (i.e. `()`

).

# Output

Again, you may choose 3 different chars, 2 for parentheses and 1 for functions. Note that they need not be the same as the ones chosen for input, but they must be consistent.

# Rules

- If there are parentheses which only enclose one function within them in the input, you must remove them in the output. Your output may not contain unneeded parentheses (i.e. enclosing only one function, or enclosing the whole output).
- You don't need to implement the algorithm used here, as long as your solution is valid for this challenge.
- Input and output are strings in the format explained in the Input and Output sections. The input will have at least one character.
- Using the standard loopholes is strictly forbidden.
- This is code-golf, so shortest answer wins. However, there won't be an accepted answer, since this is a per-language competition, and to encourage answering in languages in which this task would result in longer code compared to code written in other languages.

# Test cases

The chars used here are `()⍴`

, you should replace them with your chosen chars.

```
⍴ -> ⍴
⍴ -> ⍴
⍴⍴ -> ⍴⍴
⍴⍴⍴ -> ⍴⍴⍴
⍴⍴⍴⍴ -> ⍴(⍴⍴⍴)
⍴⍴⍴⍴⍴⍴⍴⍴⍴⍴⍴⍴⍴⍴⍴ -> ⍴⍴(⍴⍴(⍴⍴(⍴⍴(⍴⍴(⍴⍴(⍴⍴⍴))))))
⍴⍴⍴⍴⍴(⍴⍴⍴)⍴⍴(⍴(⍴⍴⍴)⍴⍴⍴)⍴⍴⍴ -> ⍴(⍴⍴(⍴⍴((⍴⍴⍴)⍴(⍴(⍴(⍴⍴⍴)(⍴⍴⍴))(⍴⍴⍴)))))
(⍴⍴⍴)(⍴⍴⍴)(⍴⍴⍴) -> (⍴⍴⍴)(⍴⍴⍴)(⍴⍴⍴)
(⍴⍴⍴)(⍴⍴⍴)⍴⍴⍴ -> (⍴⍴⍴)(⍴⍴⍴)(⍴⍴⍴)
⍴⍴(⍴)⍴⍴ -> ⍴⍴(⍴⍴⍴)
((⍴⍴)) -> ⍴⍴
⍴⍴((⍴⍴))⍴⍴ -> ⍴⍴((⍴⍴)⍴⍴)
```

_{This challenge has been posted in the Sandbox. If you have the required privilege, you can view the sandbox post here.}

2I think

Fullyis a better title thanClearly. – Adám – 2017-12-11T18:01:36.8931Reference answer to any test case. – Adám – 2017-12-11T18:06:06.313

@Adám I did expect that reference answer, it won't get many upvotes though ;) – Erik the Outgolfer – 2017-12-11T18:31:12.923

@Adám The

Clearlypart in the title refers to the fact that you must remove unneeded parentheses.Fullyis something you're supposed to do when you answer a challenge ;p – Erik the Outgolfer – 2017-12-11T18:32:34.293Is it true that this function will always be idempotent? – Esolanging Fruit – 2017-12-12T02:29:24.987

Can we assume there will never be

`()`

? – Esolanging Fruit – 2017-12-12T02:59:05.410@EsolangingFruit Yes, you can assume there will be no

`()`

, however what do you mean by "idempotent"? – Erik the Outgolfer – 2017-12-12T12:26:44.953@EriktheOutgolfer Idempotent means applying the function repeatedly won't change the result. I think the answer is "yes". – Martin Ender – 2017-12-12T12:49:00.313

@EsolangingFruit Well, with proper adjusting of the I/O chars, the function should be idempotent. – Erik the Outgolfer – 2017-12-12T12:51:19.103

I have to express my distaste for the additional requirement that degenerate inputs need to be trimmed. I don't think it really adds anything to the challenge. – Post Rock Garf Hunter – 2017-12-12T15:00:19.150

@WheatWizard Well, it was suggested to me in the sandbox. – Erik the Outgolfer – 2017-12-12T15:01:43.183

Do the three chosen characters have to be printable? – Οurous – 2017-12-18T01:27:20.557

@Οurous No, they don't. But you still need to actually output them. – Erik the Outgolfer – 2017-12-18T10:59:29.543