[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String Combine problem

Richard Zenn

9/4/2008 7:25:00 AM

Hello,

I have a algorithm problem and I'm trying to resolve it

Here is an array s = ["aaa", "b", "c", "d", "ee", "f"]
I want to combine them with some rules

1.
s[i].size > s[i+1].size
combine them
2.
s[i+1].size > s[i].size
combine them
3.
s[i].size + s[i+1].size

The upper result is

{"aaab"}
{"bc"}
{"bcd"}
{"cd"}
{"dee"}
{"eef"}

Thanks for any help

Richard
--
Posted via http://www.ruby-....

2 Answers

Lloyd Linklater

9/4/2008 1:22:00 PM

0

Richard Zenn wrote:

> Here is an array s = ["aaa", "b", "c", "d", "ee", "f"]
> I want to combine them with some rules
>
> 1.
> s[i].size > s[i+1].size
> combine them
> 2.
> s[i+1].size > s[i].size
> combine them
> 3.
> s[i].size + s[i+1].size
>
> The upper result is
>
> {"aaab"}
> {"bc"}
> {"bcd"}
> {"cd"}
> {"dee"}
> {"eef"}

In the first place, it seems that rules 1 and 2 combine to just say that
they should not be equal in size.

In the second place, if I read the rules correctly, they say that the
result you want is not correct.

Note: I am assuming that rule 3 is not a rule as much as it is an
example of how they should be combined.

s = ["aaa", "b", "c", "d", "ee", "f"]
arr = Array.new
0.upto(s.length - 2) { |i|
arr << s[i] + s[i+1] unless s[i].size == s[i+1].size
}
p arr

=> ["aaab", "dee", "eef"]

"b", "c", "d" are all the same size and would be excluded by the rules.
Could you clarify? I must be missing something.
--
Posted via http://www.ruby-....

Richard Zenn

9/4/2008 3:24:00 PM

0

Wow!it helps a lot

"b", "c", "d" is "one" character
they combine in order

array s = ["aaa", "b", "c", "d", "ee", "f"]

the rule 3 result will be "bc" "cd" "bcd"

Thanks for your help:)

Lloyd Linklater wrote:
> Richard Zenn wrote:
>
>> Here is an array s = ["aaa", "b", "c", "d", "ee", "f"]
>> I want to combine them with some rules
>>
>> 1.
>> s[i].size > s[i+1].size
>> combine them
>> 2.
>> s[i+1].size > s[i].size
>> combine them
>> 3.
>> s[i].size + s[i+1].size
>>
>> The upper result is
>>
>> {"aaab"}
>> {"bc"}
>> {"bcd"}
>> {"cd"}
>> {"dee"}
>> {"eef"}
>
> In the first place, it seems that rules 1 and 2 combine to just say that
> they should not be equal in size.
>
> In the second place, if I read the rules correctly, they say that the
> result you want is not correct.
>
> Note: I am assuming that rule 3 is not a rule as much as it is an
> example of how they should be combined.
>
> s = ["aaa", "b", "c", "d", "ee", "f"]
> arr = Array.new
> 0.upto(s.length - 2) { |i|
> arr << s[i] + s[i+1] unless s[i].size == s[i+1].size
> }
> p arr
>
> => ["aaab", "dee", "eef"]
>
> "b", "c", "d" are all the same size and would be excluded by the rules.
> Could you clarify? I must be missing something.

--
Posted via http://www.ruby-....