...

/

Solution: Calculate String Length

Solution: Calculate String Length

See one of several solutions to the "Calculate String Length" challenge and compare your solution to it.

We'll cover the following...

Compare solutions

Please compare your code to the one given below. R is a flexible programming language. We can find the solution using extra functions or by following the steps in a different order. Please keep in mind that this solution is only one of many. Feel free to play with the code and try various methods. Here is our solution to the challenge in the previous lesson:

Press + to interact
config.py
hello.txt
library(stringr) # Call the libraries
library(readr) # Call the libraries
create_matrix <- function(x){
# Code between the lines
# ------
text <- read_file('hello.txt') # Read the file
my_text <- str_to_lower(text) # Lowercase the text
my_text <- str_replace_all(my_text,'\n','') # Replace all newline characters with nothing-delete-
my_text <- str_split(my_text,' ') # Split the text where blanks are
my_text <- my_text[[1]] # Select the first underlying element
words <- my_text[str_detect(my_text,'sh')] # Detect all words involving 'sh'
result <- str_length(words) # Calculate the lengths of words
# ------
#Please name the desired output as 'result'
cat(result)
}
create_matrix(text)

Code explanation

...