Comma quibbling: Difference between revisions
Content deleted Content added
ReeceGoding (talk | contribs) m →{{header|R}}: Syntax highlighting. |
ReeceGoding (talk | contribs) →{{header|R}}: Big refactor. |
||
Line 3,570: | Line 3,570: | ||
#The task does not consider empty strings to be words, so we remove them immediately. |
#The task does not consider empty strings to be words, so we remove them immediately. |
||
#We could also remove non-upper-case characters, but the tasks gives off the impression that the user will do that. |
#We could also remove non-upper-case characters, but the tasks gives off the impression that the user will do that. |
||
vect <- vect[ |
vect <- vect[nchar(vect) != 0] |
||
len <- length(vect) |
len <- length(vect) |
||
⚫ | |||
if(len == 0) return("{}") |
|||
paste0("{", if(nchar(allButLastWord) == 0) vect else paste0(allButLastWord, " and ", vect[len]), "}") |
|||
result <- switch(len, |
|||
paste0("{", vect, "}"), |
|||
paste0("{", vect[1], " and ", vect[2], "}")) |
|||
#R's switch for numerics doesn't support default cases, so this next line cannot go in the switch. |
|||
⚫ | |||
} |
} |
||
quib(character(0)) #R has several types of empty string, e.g. character(0), "", and c("", "", ""). |
quib(character(0)) #R has several types of empty string, e.g. character(0), "", and c("", "", ""). |