10
\$\begingroup\$

Background

Your friend, a linguist, has recorded and analyzed some simple conversation snippets in various languages. Being quite absent-minded, they have forgotten which language each snippet was in. You must help them by creating a program that analyzes the sentence structures and rules out impossible cases.

Input

Your input is a non-empty string containing the characters SVO, which stand for subject, verb, and object. It represents a conversation snippet analyzed by the linguist.

Output

Your task is to break the string into sentences, and enter a period . after each sentence. A sentence contains either a verb, OR a verb and a subject, OR a verb, a subject and an object. However, you don't know which word order the original language uses; English uses subject-verb-object, but other languages, like Latin, use subject-object-verb. In fact, all six permutations exist in natural languages, so you must check each of them.

Your output shall contain, in a newline-separated string, each applicable word order, a colon :, and the input string broken into sentences according to that order. If the string cannot be parsed in some word order, the corresponding line shall be omitted. The order of the lines does not matter, and it is guaranteed that at least one word order can be parsed.

Example

Consider the input

VSVOSV

In the VOS order, the snipped can be parsed as VS.VOS.V., and in the SVO order, it can be parsed as V.SVO.SV.. The order OSV also works, and the full output is

VOS:VS.VOS.V.
SVO:V.SVO.SV.
OSV:V.SV.OSV.

Rules and scoring

You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed. Regular expressions and all built-ins are allowed.

Test cases

Input:
V
Output:
VSO:V.
VOS:V.
SVO:V.
OVS:V.
SOV:V.
OSV:V.

Input:
SVV
Output:
SVO:SV.V.
SOV:SV.V.
OSV:SV.V.

Input:
VSVOV
Output:
SVO:V.SVO.V.

Input:
VSOVS
Output:
VSO:VSO.VS.
OVS:VS.OVS.

Input:
SVOSV
Output:
SVO:SVO.SV.
OSV:SV.OSV.

Input:
VSVOSV
Output:
VOS:VS.VOS.V.
SVO:V.SVO.SV.
OSV:V.SV.OSV.

Input:
VSVVSOVSV
Output:
VSO:VS.V.VSO.VS.V.
OVS:VS.V.VS.OVS.V.
SOV:V.SV.V.SOV.SV.

Input:
SVVSVSOVSVV
Output:
SOV:SV.V.SV.SOV.SV.V.

Input:
VSOVSVSOVSVVS
Output:
VSO:VSO.VS.VSO.VS.V.VS.
OVS:VS.OVS.VS.OVS.V.VS.
\$\endgroup\$
  • \$\begingroup\$ I knew I should've implemented a string permutation feature into Japt an hour ago... \$\endgroup\$ – ETHproductions Jan 10 '16 at 21:18
1
\$\begingroup\$

Perl 5 - 104 bytes

$a=<>;for(qw/VSO VOS SVO OVS SOV OSV/){$s=s/O//r;print"$_:".$a=~s/($_|$s|V)/$1./gr if$a=~/^($_|$s|V)*$/}
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES7), 172 bytes

x=>[for(i of"V(SO?)?0V(O?S)?0(SVO?|V)0(O?VS|V)0(SO?)?V0(O?S)?V".split(0))if((y=x.match(RegExp(i,"g"))).join``==x)i.match(/\w/g).slice(0,3).join``+":"+y.join`.`+"."].join`
`

Could probably be golfed further. Suggestions welcome!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.