R中的函数可返回句子中的第一个单词,该单词的长度为偶数且偶数最长

时间:2018-12-20 21:33:54

标签: r

此函数应返回最长的偶数单词(string),这是最大偶数长度的字符串的首次出现。如果没有偶数长度,则应返回00。

约束- 句子字符串由空格组成,句子范围在1到10 ^ 5之间。

对于前- 句子1-“时间和潮汐不等待”。在这里,甚至字符都是时间和潮汐且不包含4个字母。但是,时间首先发生,因此应该显示时间。 句子2-“针锋相对”。在这句话中甚至没有,所以它应该返回00。 句子3-“眼睛是人思想的镜子”,思想是偶数眼睛,镜子中最大的偶数词。

1 个答案:

答案 0 :(得分:0)

您可以使用流行的stringr和dplyr库执行此操作。

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       

在您的描述中,您说thoughts是最长的单词,但我的逻辑发现person's一样长。如果您要删除此掩膜,则可以使用str_remove_all()函数找出解决方法。我会留给你的。