Editorial for WC '15 Finals J2 - The Oxford Comma


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

The most important observation is that each sentence can contain exactly one occurrence of the word and, one occurrence of the word or, or no occurrence of either word (but it cannot contain both and and or). If that were not the case, then the specifications would be much more confusing. We also notice from reading the question carefully that we are only to add an Oxford comma if a list has 3 or more entries, with an and/or strictly preceding the last entry. In light of this, notice that we're simply looking for the pattern a, b and c and a, b or c where a, b, and c are any words. This is sufficient, because any list with more than 3 entries will contain this pattern as well. So, we loop through the words and for every and/or, check if the previous-previous word is followed by a comma, and whether the previous word is (if not, then add one). To make sure that we're not adding a comma to the pattern a, b. and c, we should also check to make sure the previous word is not followed by a period. We do not have to check whether and/or is followed by another entry, because if and is ever at the end of a sentence, then it would be followed by a period (and so testing equality with the strings and/or would return false).


Comments

There are no comments at the moment.