The problem:
Write me a function that takes a non-null IEnumerable
(1) If the sequence is empty then the resulting string is "{}".
(2) If the sequence is a single item "ABC" then the resulting string is "{ABC}".
(3) If the sequence is the two item sequence "ABC", "DEF" then the resulting string is "{ABC and DEF}".
(4) If the sequence has more than two items, say, "ABC", "DEF", "G", "H" then the resulting string is "{ABC, DEF, G and H}". (Note: no Oxford comma!)
My solution:
static string JoinStrings(IEnumerable<string> strings) {
int len = strings.Count();
return "{"+(
(len > 1) ?
strings.Take(len - 1)
.Aggregate((string head, string tail) => head+", "+tail)+
" and " +strings.Last()
: (len == 1) ?
strings.First()
: "")+
"}";
}