Подберите все слова, содержащие только определенные символы и определенную длину

RouteData также имеет конструктор , который не принимает аргументов . Просто создайте его и добавьте нужные ему значения. Нет необходимости издеваться над ним, когда вы можете его создать.

 var routeData = new RouteData();
 routeData.Values.Add( "key1", "value1" );

 var controllerContext = new ControllerContext(httpContextMock.Object, routeData, controllerMock.Object);

1
задан dylanjm 18 January 2019 в 02:58
поделиться

1 ответ

Я не уверен, что одобряю обман - я болею за твою жену! Но это интересная проблема, поэтому я ее упущу ;-).

Вот другой подход. Возьмите все слова в dat и отсортируйте их по алфавиту; также сортируйте буквы в вашем наборе ввода в алфавитном порядке. Мы можем использовать простое регулярное выражение, чтобы найти слова в словаре, которые содержат не более указанного числа токенов каждой буквы.

library(tidyverse)

# Get the dictionary.
dat = read_lines("https://raw.githubusercontent.com/dwyl/english-words/master/words.txt")

# A function that returns possible words given a set of letters.  The letters
# are provided as a single string argument (e.g., "ilrfle").
possible.words = function(letters) {
  # Filter to words that contain only letters in the list.  This step isn't
  # strictly necessary, but it gives later steps a smaller list to have to
  # process.
  right.letters = unique(dat[grepl(paste("^[", letters, "]+$", sep = ""), dat)])
  # We're going to create a data frame where the first column is the word and
  # the second column is the word with its characters sorted in alphabetical
  # order.  Start with the first column.
  df = data.frame(word = right.letters, stringsAsFactors = F)
  # Now add the second column.  This could probably be done in dplyr, but my
  # initial attempt with mutate didn't work, and for the examples I've tried
  # the loop actually doesn't take too long.
  for(i in 1:nrow(df)) {
    df$sorted.word[i] = paste(sort(unlist(strsplit(df$word[i], ""))), collapse = "")
  }
  # Now we want to extract words that contain only as many tokens of each
  # letter as there were in the initial set.  We can use a regular expression
  # to compare the (sorted) letters of the initial set to the (sorted) letters
  # of each word, where each letter in the initial set is optional.
  sorted.letters.regex = paste(sort(paste(unlist(strsplit(letters, "")), "?", sep = "")), collapse = "")
  df = df %>%
    filter(grepl(paste("^", sorted.letters.regex, "$", sep = ""), sorted.word))
  return(df$word)
}
0
ответ дан A. S. K. 18 January 2019 в 02:58
поделиться
Другие вопросы по тегам:

Похожие вопросы: