Hi All.
What I seek to realize is that having a particular string, I insert
letters of the alphabet between the letters of the original string.
For example if I have the word: STRING ==> SATBRCIDG
- - - -
This is the code he was doing.
######################################################
a = %w{a b c d e f g h i j k l m n o p q r s t u v w x y z}
str=""
puts "Enter a string:: "
str=gets.chomp.to_s
for i in 0..a.length
s=cad.insert(i+2, "#{a[i]}")
end
puts s
#########################################################
Thanks.
on 2012-07-04 19:56
on 2012-07-04 20:46
Hi,
What's "cad"? It isn't defined anywhere.
Apart from that, the calculation of the indices for insertion is wrong.
You start with 2 instead of 1, and you don't take into account that the
string is growing with each step.
The indices have to be 1, 3, 5, 7, ...
# repeat the characters infintely, because the string may have
# any length
alphabet = ('a'..'z').cycle
string = "String"
0.upto string.length - 2 do |i|
string.insert 2 * i + 1, alphabet.next
end
puts string
But like I already said in your last thread: Ruby isn't Java. You need
to get rid of this strange "for" loop.
on 2012-07-05 05:05
On Thu, Jul 5, 2012 at 1:56 AM, Joao Silva <lists@ruby-forum.com> wrote: > What I seek to realize is that having a particular string, I insert > letters of the alphabet between the letters of the original string. try eg, > "string".split(//).zip("abcde".split(//)).join => "satbrcidneg" kind regards -botp
on 2012-07-05 05:07
On Thu, Jul 5, 2012 at 11:02 AM, botp <botpena@gmail.com> wrote: >> "string".split(//).zip("abcde".split(//)).join > => "satbrcidneg" somehow, this seems readable and easy to type, > "string".each_char.zip("abcde".each_char).join => "satbrcidneg" kind regards -botp
on 2012-07-05 08:04
> > What I seek to realize is that having a particular string, I insert > letters of the alphabet between the letters of the original string. > > For example if I have the word: STRING ==> SATBRCIDG > - - - - I'm surprised no one tried something like this. Well, not really :) Not pretty. s = "STRING" a = ("a".."z").cycle.take(s.size).join u = s.size p (s+a).unpack("a"+("x"*(u-1)+"a"+("X"*u)+"a")*(u-1))*"" Harry
on 2012-07-05 09:59
On 07/04/2012 08:27 PM, Hans Mackowiak wrote:
> "STRING".each_char.zip(("A".."Z").cycle).join[0..-2]
I prefer String#chars to String#each_char when not passing a block,
and String#chop to remove the last character.
"STRING".chars.zip((?A..?Z).cycle).join.chop
on 2012-07-05 11:24
Information: cycle and zip does not work on ruby1.8 :D
this works for 1.8;
s="STRING";s.chars.zip(("A".."Z").cycle(s.size)).join.chop
on 2012-07-05 11:36
On Thu, Jul 5, 2012 at 5:24 PM, Hans Mackowiak <lists@ruby-forum.com> wrote: > Information: cycle and zip does not work on ruby1.8 :D info: upgrade. http://www.ruby-lang.org/en/news/2011/10/06/plans-for-1-8-7/ best regards -botp
on 2012-07-05 15:40
On Thu, Jul 5, 2012 at 6:24 PM, Hans Mackowiak <lists@ruby-forum.com> wrote: > Information: cycle and zip does not work on ruby1.8 :D > zip is available for 1.8 Anyway, I think this will work on 1.8 and 1.9 s = "STRING" a = ("a".."z").to_a t = s.size-1 t.downto(1).each{|x| s.insert(x,a[x%26-1])} p s Harry
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.