Using Table
and Gather
:
F[list_] := Module[{l, pl, dpl, pt},
l = Length @ list;
pl = Function[
Gather[
Table[(Part[#, i] + Part[#, -i]) / 2,
{i, Length @ #}
]
]
][list];
dpl = Map[Greater[#, 2] &, Map[Length, pl]];
pt = Catenate[Map[Partition[#, 2, 1] &, pl]];
Which[
Or[
And[SameQ[DuplicateFreeQ[list], True],
SameQ[FreeQ[dpl, True], True]
],
And[SameQ[DuplicateFreeQ[list], False],
SameQ[FreeQ[dpl, True], True]
]
],
Map[Total, pl],
And[SameQ[DuplicateFreeQ[list], False],
SameQ[FreeQ[dpl, True], False]
],
MapApply[Sequence,
{Map[Total, Most @ pt], Function[Total[# / 2]][Last @ pt]}
]
]
];
Test:
l1 = {a};
l2 = {a, b};
l3 = {a, b, c};
l4 = {a, b, c, d};
l5 = {a, b, c, d, e};
l6 = {a, b, c, d, e, f};
l7 = {a, b, c, d, e, f, g};
l8 = {a, b, c, c, b, a};
l9 = {a, 0, 0, 0, 0};(*An example suggested by Syed*)
l10 = {a, b, b, b, b};
F /@ {l1, l2, l3, l4, l5, l6, l7, l8, l9, l10} // Column
Length
-less solution. $\endgroup$