开发者

Permutation gem

开发者 https://www.devze.com 2023-03-09 13:38 出处:网络
I am trying to get a list of strings that are permutations of letters, 开发者_Python百科numbers and the dash that vary in length from 1 to 63.

I am trying to get a list of strings that are permutations of letters, 开发者_Python百科numbers and the dash that vary in length from 1 to 63. I am using the permutation gem, and trying to get strings with 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

The output I get is as follows:

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

I am sure I am missing something fundamental here. Any assistance greatly appreciated.


Not sure what you are trying to accomplish. What about the following code (a simplified example):

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

Returning,

=> ["abc", "acb", "bac", "bca", "cab", "cba"] 


So let's make SQL query with this mechanism:

Let's say I want to find a person in database. Person has Firstname and Lastname, instead of just 'Name'. To make it more complicated, either Firstname and Lastname may contain many words.

... so we have query string from our search engine, containing "Jo Svenda Schmidt" , where "Jo Svenda" is Firstname.

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消