0

我正在尝试获取一个字符串列表,这些字符串是长度从 1 到 63 不等的字母、数字和破折号的排列。我正在使用排列 gem,并尝试使用join('').

length = 1
alphabet = [('a'..'z').to_a, ('0'..'9').to_a, ('-').to_a].flatten
while length < 64
  puts (alphabet.permutation(length){|x| p x}).join('')
  length += 1
end

我得到的输出如下:

["r", "q", "l"]
["r", "q", "m"]

我确信我在这里遗漏了一些基本的东西。非常感谢任何帮助。

4

2 回答 2

2

不确定您要完成什么。下面的代码怎么样(一个简化的例子):

['a','b','c'].permutation(3).collect { |x| x.join('')}

归来,

=> ["abc", "acb", "bac", "bca", "cab", "cba"] 
于 2011-05-30T17:08:17.007 回答
1

那么让我们用这种机制进行 SQL 查询:

假设我想在数据库中找到一个人。人有名字和姓氏,而不仅仅是“姓名”。更复杂的是,Firstname 和 Lastname 都可能包含许多单词。

...所以我们有来自搜索引擎的查询字符串,其中包含 "Jo Svenda Schmidt" ,其中 "Jo Svenda" 是名字。

query = "Jo Svenda Schmidt".split
=> ["Jo", "Svenda", "Schmidt"]
query.permutation(query.length).collect { |x| x.join(" AND ") }.join(" OR ")

=> "Jo AND Svenda AND Schmidt OR Jo AND Schmidt AND Svenda OR Svenda AND Jo AND Schmidt OR Svenda AND Schmidt AND Jo OR Schmidt AND Jo AND Svenda OR Schmidt AND Svenda AND Jo"

This is almost what we want, but not all. We need to add column names and group it. grouping may be simple, just ... join(" ) OR ( ") at the and, and then add "(" to the beginning and ")" to the end of resulting query.

But how to add nicely 'Firstname=' and 'Lastname=' or 'LIKE' to this query?

于 2011-09-22T08:54:30.337 回答