Consider these five ASCII art sea creatures:
- Standard fish:
><>
or<><
- Speedy fish:
>><>
or<><<
- Sturdy fish:
><>>
or<<><
- Stretchy fish:
><<<>
or<>>><
- Crab:
,<..>,
Write a program that accepts an arbitrary string of the characters <>,.
. If there is a way to interpret the entire string as a series of non-overlapping sea creatures, then the string should be reprinted with single spaces inserted between creatures. If this interpretation is impossible, nothing should be output (the program silently ends).
For example, the string <><><>
can be interpreted as two standard fish back-to-back. The corresponding output would be <>< ><>
.
As another example, the string ><>><>>
contains "instances" of...
(brackets only added as indicators)
- a couple standard fish:
[><>][><>]>
- a speedy fish:
><[>><>]>
- a sturdy fish in a couple ways:
[><>>]<>>
and><>[><>>]
however, only the pairing of a standard fish and a sturdy fish [><>][><>>]
spans the entire length of the string with no fish sharing characters (no overlaps). Thus the output corresponding to ><>><>>
is ><> ><>>
.
If there are multiple ways the string could be interpreted, you may print any one of them. (And only print one of them.) For example, <><<<><
can be interpreted as a standard fish and a sturdy fish: [<><][<<><]
, or as a speedy fish and a standard fish: [<><<][<><]
. So either <>< <<><
or <><< <><
would be valid output.
The crabs are just for fun. Since they don't start or end with <
or >
, they are much easier to identify (at least visually). For example, the string
,<..>,><<<>,<..>,><>,<..>,<>>><,<..>,><>>,<..>,<<><,<..>,<><,<..>,>><>
would obviously produce the output
,<..>, ><<<> ,<..>, ><> ,<..>, <>>>< ,<..>, ><>> ,<..>, <<>< ,<..>, <>< ,<..>, >><>
Here are some examples of strings (one per line) that produce no output:
<><>
,<..>,<..>,
>>><>
><<<<>
,
><><>
,<><>,
<<<><><<<>>><>><>><><><<>>><>><>>><>>><>><>><<><
The last string here can be parsed if you remove the leading <
:
<<>< ><<<> >><> ><> ><> <>< <>>>< >><> >><> >><> ><>> <<><
(There may be other possible outputs.)
Details
- The input string will only contain the characters
<>,.
. - The input string will be at least one character long.
- Take input in any common way (command line, stdin) and output to stdout.
- The shortest code in bytes wins. (Handy byte counter.) Tiebreaker is earlier post.