aris
July 4, 2012, 7:56pm
1
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.
“STRING”.each_char.zip((“A”…“Z”).cycle).join[0…-2]
#> “SATBRCIDNEG”
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 Thu, Jul 5, 2012 at 1:56 AM, Joao S. [email protected] 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 Thu, Jul 5, 2012 at 11:02 AM, botp [email protected] 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
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
Information: cycle and zip does not work on ruby1.8
this works for 1.8;
s=“STRING”;s.chars.zip((“A”…“Z”).cycle(s.size)).join.chop
On 07/04/2012 08:27 PM, Hans M. 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 Thu, Jul 5, 2012 at 6:24 PM, Hans M. [email protected]
wrote:
Information: cycle and zip does not work on ruby1.8
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
On Thu, Jul 5, 2012 at 5:24 PM, Hans M. [email protected]
wrote:
Information: cycle and zip does not work on ruby1.8
info: upgrade.
http://www.ruby-lang.org/en/news/2011/10/06/plans-for-1-8-7/
best regards -botp