69
9
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.
Nice codegolf with parallels to compression (greedy/lazy substring matching) – schnaader – 2015-04-09T08:42:36.013
4I thought we were going to interpret Fish' lyrics :-( – RemcoGerlich – 2015-04-09T08:47:13.760
9@RemcoGerlich Not that Fish either – Calvin's Hobbies – 2015-04-09T09:05:43.060
4Finally a code-golf for RFC 3889 General Fish Transfer Format (implementing RFC3500: Standard Fish Transfer Format)! – Sanchises – 2015-04-09T09:39:06.407
11Bonus points if you do it using BrainF***! And the longer it takes your program to realize its own code isn't fish-compliant, the better. – mbomb007 – 2015-04-09T18:26:27.603
3I saw this and scrolled down, resigned to the fact that this would be @Calvin'sHobbies and nobody, nobody else. – Soham Chowdhury – 2015-04-10T15:12:14.270
1
((><>)|(<><)|(>><>)|(<><<)|(><>>)|(<<><)|(><<<>)|(<>>><)|(,<..>,))+
– Kevin – 2015-04-11T08:09:28.543@Kevin: Exactly what I was thinking when I saw this question :D – Mark K Cowan – 2015-04-12T16:42:12.670
what about
><><>
from this string you can create two independent fish, how this should be handled? – user902383 – 2015-04-13T12:24:13.463@user902383 There are two fish in there but they overlap, so there is no output. – Calvin's Hobbies – 2015-04-13T20:38:22.487