Dear Jim Whittaker,
Do you know the coin partition problem?
http://projecteuler.net/problem=78
Can you tell me if the way you are willing to iterate through the
pairs, triples (partitions) has any similarities with the "coin
partition" problem?
def partitions(str)
ary = [[str]]
0.upto(str.size-2) do |first_slice_index| # don't recurse at the
last element (so size-2)
first_slice = str[0..first_slice_index]
second_slice = str[(first_slice_index+1)..-1]
other_slices = partitions(second_slice) # recursively
other_slices.each do |slice|
candidate = [first_slice] + slice
ary.push candidate
end
end
ary
end
This code returns an Array of Array of "partitions" of the str.
I think these are the "candidates" you want to test for a match, am I right?
partitions("a")
# => [["a"]]
partitions("ab")
# => [["ab"], ["a", "b"]]
partitions("abc")
# => [["abc"], ["a", "bc"], ["a", "b", "c"], ["ab", "c"]]
partitions("abcd")
# => [["abcd"], ["a", "bcd"], ["a", "b", "cd"], ["a", "b", "c", "d"],
["a", "bc", "d"], ["ab", "cd"], ["ab", "c", "d"], ["abc", "d"]]
For "stringy" it gives me this. A 64 elements Array of Arrays.
[["stringy"], ["s", "tringy"], ["s", "t", "ringy"], ["s", "t", "r",
"ingy"], ["s", "t", "r", "i", "ngy"], ["s", "t", "r", "i", "n", "gy"],
["s", "t", "r", "i", "n", "g", "y"], ["s", "t", "r", "i", "ng", "y"],
["s", "t", "r", "in", "gy"], ["s", "t", "r", "in", "g", "y"], ["s",
"t", "r", "ing", "y"], ["s", "t", "ri", "ngy"], ["s", "t", "ri", "n",
"gy"], ["s", "t", "ri", "n", "g", "y"], ["s", "t", "ri", "ng", "y"],
["s", "t", "rin", "gy"], ["s", "t", "rin", "g", "y"], ["s", "t",
"ring", "y"], ["s", "tr", "ingy"], ["s", "tr", "i", "ngy"], ["s",
"tr", "i", "n", "gy"], ["s", "tr", "i", "n", "g", "y"], ["s", "tr",
"i", "ng", "y"], ["s", "tr", "in", "gy"], ["s", "tr", "in", "g", "y"],
["s", "tr", "ing", "y"], ["s", "tri", "ngy"], ["s", "tri", "n", "gy"],
["s", "tri", "n", "g", "y"], ["s", "tri", "ng", "y"], ["s", "trin",
"gy"], ["s", "trin", "g", "y"], ["s", "tring", "y"], ["st", "ringy"],
["st", "r", "ingy"], ["st", "r", "i", "ngy"], ["st", "r", "i", "n",
"gy"], ["st", "r", "i", "n", "g", "y"], ["st", "r", "i", "ng", "y"],
["st", "r", "in", "gy"], ["st", "r", "in", "g", "y"], ["st", "r",
"ing", "y"], ["st", "ri", "ngy"], ["st", "ri", "n", "gy"], ["st",
"ri", "n", "g", "y"], ["st", "ri", "ng", "y"], ["st", "rin", "gy"],
["st", "rin", "g", "y"], ["st", "ring", "y"], ["str", "ingy"], ["str",
"i", "ngy"], ["str", "i", "n", "gy"], ["str", "i", "n", "g", "y"],
["str", "i", "ng", "y"], ["str", "in", "gy"], ["str", "in", "g", "y"],
["str", "ing", "y"], ["stri", "ngy"], ["stri", "n", "gy"], ["stri",
"n", "g", "y"], ["stri", "ng", "y"], ["strin", "gy"], ["strin", "g",
"y"], ["string", "y"]]
Let us know if this is the issue.
And if this is not the issue, just sorry for misunderstanding the point.
I saw you saying that you wanted to iterate through "pairs" first. So I ask:
Does not single letters be allowed? (note that this partitions above
iterate through single letters combination also).
When iterating through pairs, if the str is "abcde", the "ab", "cd",
"e" will be allowed? (the "e" is a single letter).
The same above and "ab", "cde" will be allowed ? ("cde" is triple and
we began with a pair "ab").
Best regards,
Abinoam Jr.
···
On Thu, Dec 19, 2013 at 4:34 AM, Ammar Ali <ammarabuali@gmail.com> wrote:
On Dec 19, 2013, at 5:30 AM, Matthew Kerwin <lists@ruby-forum.com> wrote:
I just have to say, this is quite a complicated little task you've set
yourself. I spent a couple of hours getting something[1] to even
Yes, this is a complicated problem. You can come close to a reasonable solution with brute force techniques (i.e many nested loops) but it will very likely be inefficient and quite limited. There are specialized algorithms for dealing with such problems, like n-gram and approximate string matching algorithms.
Regards,
Ammar