From: Larry Lv Date: 2011-04-22T18:24:27+09:00 Subject: Re: Reversing a string without using array, classes and reverse function --90e6ba6154121f097a04a17e6c89 Content-Type: text/plain; charset=ISO-8859-1 #!/usr/bin/env ruby while line = gets.chomp r = String.new (1..line.length).each do |i| r << line[-1*i] end puts r puts r == line.reverse end On Fri, Apr 22, 2011 at 4:43 PM, Stu wrote: > One more with ruby blocks (This would be the ruby way =) ) > > s = "1234567890" > r = String.new > s.length.times{|i| r << s[(i+1)*(-1)]} > > r == s.reverse > true > > cheers!!! > > That was fun!!!... give me another =) > > ~Stu > On Fri, Apr 22, 2011 at 3:32 AM, Stu wrote: > > On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis > > wrote: > > > >> for j in 1..len do > >> mycommand += s[-1*j] > >> > >> > > > > very nice I like the use of your math. By multiplying the array index > > by negative one your sending the index a reverse number while your for > > loop traverses forward. This is perfect logic. > > > > Here is a rewrite with my while loop: > > > > s = "1234567890" > > r=String.new > > i = 1 > > while i <= s.length > > r << s[i*(-1)] > > i+=1 > > end > > printf s > > "0987654321" > > => "0987654321" > >> y == x.reverse > > true > > > > -- Best Regards, *Larry Lv* @ Baidu NLP --90e6ba6154121f097a04a17e6c89--