# String Combine problem

**URL:** <https://www.ruby-forum.com/t/string-combine-problem/147692>\
**Category:** Ruby\
**Created:** [September 4, 2008, 9:30am UTC](https://www.ruby-forum.com/t/string-combine-problem/147692 "2008-09-04T09:30:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luckyzenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecc23a/32.png) [@luckyzenn](https://www.ruby-forum.com/u/luckyzenn)\
**Post date:** [September 4, 2008, 9:30am UTC](https://www.ruby-forum.com/t/string-combine-problem/147692/1 "2008-09-04T09:30:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luckyzenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecc23a/32.png) [@luckyzenn](https://www.ruby-forum.com/u/luckyzenn)\
**Post date:** [September 4, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/string-combine-problem/147692/2 "2008-09-04T15:27:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![luckyzenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecc23a/32.png) [@luckyzenn](https://www.ruby-forum.com/u/luckyzenn)\
**Post date:** [September 4, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/string-combine-problem/147692/3 "2008-09-04T17:29:46Z")

</div>

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 L. 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.

---

<div class="post-metadata">

**Author:** ![luckyzenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecc23a/32.png) [@luckyzenn](https://www.ruby-forum.com/u/luckyzenn)\
**Post date:** [September 5, 2008, 3:55am UTC](https://www.ruby-forum.com/t/string-combine-problem/147692/4 "2008-09-05T03:55:19Z")

</div>

From: Richard Zenn [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

rules 1 & 2 simply says that if they differ in size, combine them, no?

# 3.

# s[i].size + s[i+1].size

where is the condition here?  
add the sizes?? it does not show in your output below

# The upper result is

# {“aaab”}

# {“bc”}

# {“bcd”}

# {“cd”}

# {“dee”}

# {“eef”}

hmmm, it seems that you are combining them regardless… ??

here is a simplistic code (judging only fr your output)

botp@botp-desktop:~$ cat test.rb  
s = [“aaa”, “b”, “c”, “d”, “ee”, “f”]  
t = []  
(1…s.size-2).each do |i|  
t \<\< s[i-1] + s[i]  
if s[i].size == s[i-1].size and s[i].size ==s[i+1].size  
t \<\< s[i-1]+s[i]+s[i+1]  
end  
t \<\< s[i] + s[i+1]  
end  
p t.uniq

botp@botp-desktop:~$ ruby test.rb  
[“aaab”, “bc”, “bcd”, “cd”, “dee”, “eef”]

i hope that is ok to startup your ruby interest.

next time, pls show your initial/tried code, no matter how wrong it is  
you think. help us help you, ie.

kind regards -botp
