[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

duplicating characters in a string

Adam Akhtar

3/6/2008 4:13:00 PM

If i have a string "abc" and want to make it like this "aabbcc", how do
i go about it?

I thought convert it into an array and then use string.map{|x| x << x}

though i havnt tested that yet. Is there a better way or a string method
that saves me from having to convert it into an array?
--
Posted via http://www.ruby-....

29 Answers

Tim Hunter

3/6/2008 4:19:00 PM

0

Adam Akhtar wrote:
> If i have a string "abc" and want to make it like this "aabbcc", how do
> i go about it?
>
> I thought convert it into an array and then use string.map{|x| x << x}
>
> though i havnt tested that yet. Is there a better way or a string method
> that saves me from having to convert it into an array?

I strongly doubt that this is the only way, but I'll offer

irb(main):004:0> "abc".gsub(/./) { |c| c + c}
=> "aabbcc"

Now let's see how many other ways people can think of...
--
Posted via http://www.ruby-....

Gary Wright

3/6/2008 4:27:00 PM

0


On Mar 6, 2008, at 11:12 AM, Adam Akhtar wrote:

> If i have a string "abc" and want to make it like this "aabbcc",
> how do
> i go about it?
>
> I thought convert it into an array and then use string.map{|x| x << x}
>
> though i havnt tested that yet. Is there a better way or a string
> method
> that saves me from having to convert it into an array?

Ask 10 Ruby programmers this question and I'm sure you'll get 11
different answers. If your definition of 'best' is 'fastest' you'll
simply have to code and benchmark a couple of solutions. Here is one
way to do it:

>> a = "abc"
=> "abc"
>> (b = a.split(//)).zip(b).join
=> "aabbcc"

Gary Wright

Sebastian Hungerecker

3/6/2008 4:41:00 PM

0

Tim Hunter wrote:
> "abc".gsub(/./) { |c| c + c}
> => "aabbcc"

"abc".gsub(/(.)/,'\1\1')
=> "aabbcc"

--
NP: Placebo - Every You Every Me
Jabber: sepp2k@jabber.org
ICQ: 205544826

Adam Akhtar

3/6/2008 4:54:00 PM

0


> Ask 10 Ruby programmers this question and I'm sure you'll get 11
> different answers. If your definition of 'best' is 'fastest' you'll
> simply have to code and benchmark a couple of solutions.

Well actually im new to ruby so although fastest/best solutions would be
appreciated I'd actually prefer ones that were not to advanced or
cryptic to read but at the same time not to innefficent.

Thanks so far for your suggestions!!

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

7stud --

3/6/2008 4:58:00 PM

0

Adam Akhtar wrote:
>
>> Ask 10 Ruby programmers this question and I'm sure you'll get 11
>> different answers. If your definition of 'best' is 'fastest' you'll
>> simply have to code and benchmark a couple of solutions.
>
> Well actually im new to ruby so although fastest/best solutions would be
> appreciated I'd actually prefer ones that were not to advanced or
> cryptic to read but at the same time not to innefficent.
>
> Thanks so far for your suggestions!!


str = 'abc'
repeat = 2

new_str = ""
0.upto(str.length) do |i|
new_str << str[i, 1] * repeat
end

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

7stud --

3/6/2008 5:03:00 PM

0

Adam Akhtar wrote:
>
>> Ask 10 Ruby programmers this question and I'm sure you'll get 11
>> different answers. If your definition of 'best' is 'fastest' you'll
>> simply have to code and benchmark a couple of solutions.
>
> Well actually im new to ruby so although fastest/best solutions would be
> appreciated I'd actually prefer ones that were not to advanced or
> cryptic to read but at the same time not to innefficent.
>
> Thanks so far for your suggestions!!

str = 'abc'
duplicate = 2

new_str = ""
str.each_byte do |byte|
duplicate.times do
new_str << byte
end
end

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

Rodrigo Bermejo

3/6/2008 5:15:00 PM

0

7stud -- wrote:
> Adam Akhtar wrote:
>>
>>> Ask 10 Ruby programmers this question and I'm sure you'll get 11
>>> different answers. If your definition of 'best' is 'fastest' you'll
>>> simply have to code and benchmark a couple of solutions.


#answers.succ

"abc".gsub(/./) { |x| x * 2 }
--
Posted via http://www.ruby-....

Robert Klemme

3/6/2008 5:59:00 PM

0

On 06.03.2008 17:40, Sebastian Hungerecker wrote:
> Tim Hunter wrote:
>> "abc".gsub(/./) { |c| c + c}
>> => "aabbcc"
>
> "abc".gsub(/(.)/,'\1\1')
> => "aabbcc"

You do not need groups:

irb(main):003:0> "abc".gsub /./, '\\&\\&'
=> "aabbcc"

Cheers

robert

Sebastian Hungerecker

3/6/2008 6:06:00 PM

0

Robert Klemme wrote:
> You do not need groups:
>
> irb(main):003:0> "abc".gsub /./, '\\&\\&'
> => "aabbcc"

You do not need double backslashes either:

>> "abc".gsub /./, '\&\&'
=> "aabbcc"

--
NP: Katatonia - Quiet World
Jabber: sepp2k@jabber.org
ICQ: 205544826

Joel VanderWerf

3/6/2008 10:02:00 PM

0

Rodrigo Bermejo wrote:
> "abc".gsub(/./) { |x| x * 2 }

That's the most elegant way to refer to captures, but if speed matters:

"abc".gsub(/(.)/, "\\1\\1")

The "\\1" is always easy to mess up, which is why I generally prefer the
block form.

It's only about a factor of two faster, according to the following, but
that matters sometimes.

require 'benchmark'

Benchmark.bmbm do |b|
s = "abc" * 1_000_000
re = /(.)/

b.report("s.gsub(re){...}") do
s.gsub(re) { |x| x * 2 }
end

b.report("s.gsub(re,...)") do
s.gsub(re, "\\1\\1")
end
end

__END__

Rehearsal ---------------------------------------------------
s.gsub(re){...} 5.970000 0.000000 5.970000 ( 6.009089)
s.gsub(re,...) 2.900000 0.060000 2.960000 ( 3.148006)
------------------------------------------ total: 8.930000sec

user system total real
s.gsub(re){...} 5.760000 0.010000 5.770000 ( 5.925049)
s.gsub(re,...) 2.800000 0.060000 2.860000 ( 2.914432)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407