7stud2
(7stud --)
4 July 2012 17:56
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.
--
Posted via http://www.ruby-forum.com/ .
7stud2
(7stud --)
4 July 2012 18:27
2
"STRING".each_char.zip(("A".."Z").cycle).join[0..-2]
#> "SATBRCIDNEG"
···
--
Posted via http://www.ruby-forum.com/ .
7stud2
(7stud --)
4 July 2012 18:46
3
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.
···
--
Posted via http://www.ruby-forum.com/ .
Harry3
(Harry)
5 July 2012 06:03
5
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
7stud2
(7stud --)
5 July 2012 09:24
6
Information: cycle and zip does not work on ruby1.8
···
--
Posted via http://www.ruby-forum.com/ .
botp1
(botp)
5 July 2012 03:05
7
"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 Thu, Jul 5, 2012 at 11:02 AM, botp <botpena@gmail.com> wrote:
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 07/04/2012 08:27 PM, Hans Mackowiak wrote:
"STRING".each_char.zip(("A".."Z").cycle).join[0..-2]
--
Lars Haugseth
botp1
(botp)
5 July 2012 09:35
9
info: upgrade. Plans for 1.8.7
best regards -botp
···
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
Harry3
(Harry)
5 July 2012 13:40
10
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 6:24 PM, Hans Mackowiak <lists@ruby-forum.com> wrote:
Information: cycle and zip does not work on ruby1.8