Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

Function in R that returns first word in a sentence that is having a length which is an even number & also longest even word

This function should return the longest even word(string) that is the first occurence of a string with maximal even number length.If there are no even length it should return 00.

Constraints- The sentence string consist of spaces & the sentence range is between 1 to 10^5.

For ex- Sentence 1-"Time & tide waits for none".Here , even characters are time & tide & none with 4 letters. But,time occurs first so time should be displayed. Sentence 2-" Tit for tat".In this sentence no even so it this should return 00. Sentence 3-"Eyes are mirror of person's thoughts" here , thoughts is the greatest even word amongst even words eyes, mirror.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do this using the popular stringr and dplyr libraries.

library(dplyr)
library(stringr)

df <- tibble(
  sentence = c(
    "Time & tide waits for none",
    " Tit for tat",
    "Eyes are mirror of person's thoughts",
    "Some Other Sentence",
    "Odd sentences failure"
  )
)

df <- df %>%
  # Split the sentence and store it in a new column
  mutate(split_sentence = str_split(sentence," ")) %>%
  # Do the next step row wise because we will be dealing with a vector of vectors
  rowwise() %>%
  # Keep only words that have a remainder of 0 when divided by 2 (str_length modulo 2)
  mutate(split_sentence = list(split_sentence[str_length(split_sentence) %% 2 == 0])) %>%
  # Only keep non-null strings !""
  mutate(split_sentence = list(split_sentence[str_length(split_sentence) > 0])) %>%
  # Find the first word with the longest length
  mutate(split_sentence = list(split_sentence[which.max(str_length(split_sentence))])) %>%
  # Keep only the first word left in the vector or return NA if none left
  mutate(first_even = first(split_sentence)) %>%
  # Ungroup because we don't need to work rowwise anymore
  ungroup() %>%
  # Convert any NA values to "00" per question
  mutate(first_even = ifelse(is.na(first_even),"00",first_even)) %>%
  select(-split_sentence)

# A tibble: 5 x 2
#   sentence                             first_even
#   <chr>                                <chr>     
# 1 Time & tide waits for none           Time      
# 2 " Tit for tat"                       00        
# 3 Eyes are mirror of person's thoughts person's  
# 4 Some Other Sentence                  Sentence  
# 5 Odd sentences failure                00       

In your description you said that thoughts would be the longest word but my alogrithm found that person's was just as long. If you want to remove the appostrophe you can figure out how to do that using the str_remove_all() function. I'll leave that for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...