Comma quibbling: Difference between revisions

Content deleted Content added
m →‎{{header|R}}: Syntax highlighting.
→‎{{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[!nchar(vect) == 0]
vect <- vect[nchar(vect) != 0]
len <- length(vect)
len <- length(vect)
allButLastWord <- if(len >= 2) paste0(vect[seq_len(len - 1)], collapse = ", ") else ""
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.
if(is.null(result)) paste0("{", paste0(vect[seq_len(len-1)], collapse = ", "), " and ", vect[len], "}") else result
}
}
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("", "", "").