patternpythonMinor
Extracting initials with R
Viewed 0 times
initialswithextracting
Problem
Playing around in
I mainly wonder whether I am breaking any good practice/style norms overall or in any tiny detail.
R with a Edx CS50 homework. The job is meant to be done in C, I am using (the more familiar) R just for warmup.I mainly wonder whether I am breaking any good practice/style norms overall or in any tiny detail.
initials <- function(full.name) {
# Returns initials of a full name
# Input will contain only letters (uppercase and/or lowercase) plus
# single spaces between words. Folks like Joseph Gordon-Levitt,
# Conan O’Brien, and David J. Malan won’t be using your program. (If only!)
if (length(full.name) == 0) {
stop ("Valid name please")
} else {
isspace <- integer(0)
fn.split <- unlist(strsplit(full.name, fixed = TRUE, split = ""))
isspace <- which(fn.split == " ")
init <- toupper(fn.split[c(1, (isspace+1))])
return(paste(init, collapse = ""))
}
}
initials("Stack Overflow")
[1] "SO"Solution
You have a bug.
This problem reveals that you haven't tested your program enough.
Always look for corner cases and test well.
A minor thing, but when you reach a
making the code more "flat", which is typically more readable.
As far as good practices go,
in R, it's generally not recommended to use
especially when there is a single point of return from the function.
So I recommend to simply omit the
Putting the above tips together:
length doesn't return the length of a string, but the length of a vector. The function you need is nchar.This problem reveals that you haven't tested your program enough.
Always look for corner cases and test well.
A minor thing, but when you reach a
stop statement, the rest of the function will not be executed, so you could remove the else block,making the code more "flat", which is typically more readable.
As far as good practices go,
in R, it's generally not recommended to use
return statements,especially when there is a single point of return from the function.
So I recommend to simply omit the
return statement at the end of the function.Putting the above tips together:
initials <- function(full.name) {
# Returns initials of a full name
# Input will contain only letters (uppercase and/or lowercase) plus
# single spaces between words. Folks like Joseph Gordon-Levitt,
# Conan O’Brien, and David J. Malan won’t be using your program. (If only!)
if (nchar(full.name) == 0) {
stop ("Valid name please")
}
isspace <- integer(0)
fn.split <- unlist(strsplit(full.name, fixed = TRUE, split = ""))
isspace <- which(fn.split == " ")
init <- toupper(fn.split[c(1, (isspace+1))])
paste(init, collapse = "")
}Code Snippets
initials <- function(full.name) {
# Returns initials of a full name
# Input will contain only letters (uppercase and/or lowercase) plus
# single spaces between words. Folks like Joseph Gordon-Levitt,
# Conan O’Brien, and David J. Malan won’t be using your program. (If only!)
if (nchar(full.name) == 0) {
stop ("Valid name please")
}
isspace <- integer(0)
fn.split <- unlist(strsplit(full.name, fixed = TRUE, split = ""))
isspace <- which(fn.split == " ")
init <- toupper(fn.split[c(1, (isspace+1))])
paste(init, collapse = "")
}Context
StackExchange Code Review Q#150624, answer score: 3
Revisions (0)
No revisions yet.