Forum: Ruby How to do pattern program in Ruby?

1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-23 09:30
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

How to do program like above pattern?

Thank you.
A7fca2db6ff3f4583df98043bd5166f9?d=identicon&s=25 Panagiotis Atmatzidis (Guest)
on 2014-02-23 11:42
(Received via mailing list)
Hell,

On 23 Φεβ 2014, at 09:30 , Jaimin Pandya <lists@ruby-forum.com> wrote:

> 1
>       2 1
>      3 2 1
>     4 3 2 1
>    5 4 3 2 1
>
> How to do program like above pattern?

Easy actually:

#!/usr/bin/env ruby
n = 1
while n < 6
  l = []
  1.upto(n){|x|l << x}
  p l.join.reverse.gsub(/(.{1})(?=.)/, '\1 \2')
  n += 1
end

Apart from the complex regular expression which adds the spacing there's
not anything complicated.

I spent a couple of minutes trying to *fix* a center-ed spacing but I
wasn't able to. I think the best way to achieve this is to
write a method which deals with it. But you should probably add the last
result with the longer 'length' and then start aligning the output.

>
> Thank you.

You're welcome,

best regards

>
> --
> Posted via http://www.ruby-forum.com/.


Panagiotis (atmosx) Atmatzidis

email:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-23 14:07
> You're welcome,
>
> best regards

Thank you for your reply. It's helpful for me and i get more knowledge
from your reply.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2014-02-23 18:11
(Received via mailing list)
On Sun, Feb 23, 2014 at 7:07 AM, Jaimin Pandya <lists@ruby-forum.com>
wrote:
>> You're welcome,
>>
>> best regards
>
> Thank you for your reply. It's helpful for me and i get more knowledge
> from your reply.
>
> --
> Posted via http://www.ruby-forum.com/.

another alternative: https://gist.github.com/tamouse/9174122
33087654dbc710e81f51fda5f8241f28?d=identicon&s=25 张泽鹏 (Guest)
on 2014-02-24 09:24
(Received via mailing list)
```ruby
1.upto(5).each {|n| puts " " * (5 - n) + n.downto(1).to_a.join(" ")}
```
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-24 18:45
Panagiotis Atmatzidis wrote in post #1137696:
> Hell,
>
> On 23 Φεβ 2014, at 09:30 , Jaimin Pandya <lists@ruby-forum.com> wrote:
>
>> 1
>>       2 1
>>      3 2 1
>>     4 3 2 1
>>    5 4 3 2 1
>>
>> How to do program like above pattern?
>
> Easy actually:
>
> #!/usr/bin/env ruby
> n = 1
> while n < 6
>   l = []
>   1.upto(n){|x|l << x}
>   p l.join.reverse.gsub(/(.{1})(?=.)/, '\1 \2')
>   n += 1
> end
>

If I want to do this program without array, How can I do this?


Thank you.
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-02-24 19:51
Jaimin Pandya wrote in post #1137849:

> If I want to do this program without array, How can I do this?
>
>

 1.upto(6) { |i| puts "6 5 4 3 2 1"[2*(6-i)..12] }
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-24 20:03
Regis d'Aubarede wrote in post #1137856:
> Jaimin Pandya wrote in post #1137849:
>
>> If I want to do this program without array, How can I do this?
>>
>>
>
>  1.upto(6) { |i| puts "6 5 4 3 2 1"[2*(6-i)..12] }

Thank you , It's help me a lot.


>n = 1
> while n < 6
>   l = []
>   1.upto(n){|x|l << x}
>   p l.join.reverse
>   n += 1
> end
>

In above program , Is It possible to do program without array? If yes ,
how

can I do that?
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-02-24 20:11
Jaimin Pandya wrote in post #1137859:
>> Jaimin Pandya wrote in post #1137849:

> can I do that?
Yes you (should) can...

n = 1
while n < 6
   puts "6 5 4 3 2 1"[2*(6-n)..12]
   n += 1
end
920f6e4a0fbf997d851455f827a10ebc?d=identicon&s=25 unknown (Guest)
on 2014-02-24 20:28
(Received via mailing list)
I would not have thought of using string indexing like that.
I prefer not having the hard coded maximum string.
How about this, the max is variable and it centers the rows.

max = 6
1.upto(max) do |i|
  line = ""
      i.downto(1) {|j| line += "#{j} "}
  puts line.rjust(max + i)
end




"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/24/2014
02:11:33
PM:
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-02-24 20:37
unknown wrote in post #1137867:
> I would not have thought of using string indexing like that.

No list, no string slicing, ... , why use ruby ?
C is enough for that !

:)
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-26 22:55
As I asked I want to do program without using array:

I am trying to do as follow:

n = 1
while n <= 5
  1.upto(n) do |i|
       print "#{i} "
      end
   n += 1
   end

So, Output display like:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

But I want output like as follow:
1
12
123
1234
12345

Means, Output display like horizontally (1 2) in same line, but I want
output in second line.

How to do that?

Thank you.
A7fca2db6ff3f4583df98043bd5166f9?d=identicon&s=25 Panagiotis Atmatzidis (Guest)
on 2014-02-26 23:31
(Received via mailing list)
Hello Jaimin,

On 26 Φεβ 2014, at 22:55 , Jaimin Pandya <lists@ruby-forum.com> wrote:

>   end
>
> So, Output display like:
> 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Yes that's a nice start.

>
> How to do that?

By reading the previous you *must* be able to *study* the code and
figure it out. This is something you will need to do more often than
not, when you write programs. It's very rare for any programmer -
contrary to what I used to believed before - to write code by heart and
get it right the first time.

Most of the times it takes some to search on google, read the
documentation work through a tutorial etc. When you find a code snippet
that does what you want but you don't understand why, try to break it
down.

So here you have a series of elements you need to print in a series.
There are different (possible endless) approaches on how to solve this.
@Regis d'Aubarede offered the following snippet "1.upto(6) { |i| puts "6
5 4 3 2 1"[2*(6-i)..12] }". Try to understand how exactly it works by
experimenting with the code and you'll find the answer to your question,
providing an easy-way-out at this point will do more damage than good
IMHO.

Don't give up.

>
> Thank you.

You're welcome.

>
> --
> Posted via http://www.ruby-forum.com/.


Panagiotis (atmosx) Atmatzidis

email:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5
A7fca2db6ff3f4583df98043bd5166f9?d=identicon&s=25 Panagiotis Atmatzidis (Guest)
on 2014-02-26 23:47
(Received via mailing list)
Hey again,

Sorry for the previous mail, was out of line, you have the code there...
and I'm terribly sleepy.

On 26 Φεβ 2014, at 22:55 , Jaimin Pandya <lists@ruby-forum.com> wrote:

> As I asked I want to do program without using array:
>
> I am trying to do as follow:
>
> n = 1
> while n <= 5
>  1.upto(n) do |i|
>       print "#{i} "

Just add the "\n" (newline) and you're all set: print "#{i}\n"

> 123
> --
> Posted via http://www.ruby-forum.com/.


Panagiotis (atmosx) Atmatzidis

email:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-27 13:55
Panagiotis Atmatzidis wrote in post #1138192:
> Hey again,
>
> Sorry for the previous mail, was out of line, you have the code there...
> and I'm terribly sleepy.

>>
>> I am trying to do as follow:
>>
>> n = 1
>> while n <= 5
>>  1.upto(n) do |i|
>>       print "#{i} "
>
> Just add the "\n" (newline) and you're all set: print "#{i}\n"
>
By adding "\n" , i am not able to get desire output.
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-27 13:57
>
> By reading the previous you *must* be able to *study* the code and
> figure it out. This is something you will need to do more often than
> not, when you write programs. It's very rare for any programmer -
> contrary to what I used to believed before - to write code by heart and
> get it right the first time.
>
> Most of the times it takes some to search on google, read the
> documentation work through a tutorial etc. When you find a code snippet
> that does what you want but you don't understand why, try to break it
> down.
>
> So here you have a series of elements you need to print in a series.
> There are different (possible endless) approaches on how to solve this.
> @Regis d'Aubarede offered the following snippet "1.upto(6) { |i| puts "6
> 5 4 3 2 1"[2*(6-i)..12] }". Try to understand how exactly it works by
> experimenting with the code and you'll find the answer to your question,
> providing an easy-way-out at this point will do more damage than good
> IMHO.
>
> Don't give up.

I am trying to read code and understand how it is work. I will not give
up.

Thank you for your advice.
920f6e4a0fbf997d851455f827a10ebc?d=identicon&s=25 unknown (Guest)
on 2014-02-27 14:11
(Received via mailing list)
Correct, you only want a newline at the end of each line.
You will want a 'puts' outside of the inner loop.

n = 1
while n <= 5
  1.upto(n) do |i|
    print "#{i} "
  end
  puts
  n += 1
end




"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/27/2014
07:55:01
AM:

> From: Jaimin Pandya <lists@ruby-forum.com>
> To: ruby-talk@ruby-lang.org
> Date: 02/27/2014 07:55 AM
> Subject: Re: How to do pattern program in Ruby?
> Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>
>
> Panagiotis Atmatzidis wrote in post #1138192:
> > Hey again,
> >
> > Sorry for the previous mail, was out of line, you have the code
there...
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-27 15:36
Jaimin Pandya wrote in post #1138235:
> Panagiotis Atmatzidis wrote in post #1138192:
>> Hey again,
>>
>> Sorry for the previous mail, was out of line, you have the code there...
>> and I'm terribly sleepy.
>
>>>
>>> I am trying to do as follow:
>>>
>>> n = 1
>>> while n <= 5
>>>  1.upto(n) do |i|
>>>       print "#{i} "
>>
>> Just add the "\n" (newline) and you're all set: print "#{i}\n"
>>
> By adding "\n" , i am not able to get desire output.

Sorry for reply to early and I said by adding "\n" , I am not getting
desire output.

By adding "\n" , I am able to get desire output by using nested loop.

Thank you.
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-27 17:14
I done pattern program by using nested loop:

n = 1
while n <= 5
 1.upto(n) do |i|
     1.upto(n) do |i|
         print i
     end
   print "\n"
   n += 1
   end
  end

By run above program I got the output like:

1
12
123
1234
12345
123456
1234567

But It is not desire output,

I want output like :

1
12
123
1234
12345

How can I get it?

Would anyone help me in this?

Thank you.
920f6e4a0fbf997d851455f827a10ebc?d=identicon&s=25 unknown (Guest)
on 2014-02-27 18:22
(Received via mailing list)
You don't need an additional loop, just print the '\n' after you have
finished printing the full line, i.e. after the loop.
I commented out the lines you want to get rid of.

n = 1
while n <= 5
  1.upto(n) do |i|
    #1.upto(n) do |i|
      print i
    end
    print "\n"
    n += 1
  #end
end



"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/27/2014
11:14:55
AM:
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-27 18:27
unknown wrote in post #1138260:
> You don't need an additional loop, just print the '\n' after you have
> finished printing the full line, i.e. after the loop.
> I commented out the lines you want to get rid of.
>
> n = 1
> while n <= 5
>   1.upto(n) do |i|
>     #1.upto(n) do |i|
>       print i
>     end
>     print "\n"
>     n += 1
>   #end
> end
>
>
Your answer helpful for me.

Thank you very much.
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-28 08:58
> n = 1
> while n <= 5
>   1.upto(n) do |i|
>     #1.upto(n) do |i|
>       print i
>     end
>     print "\n"
>     n += 1
>   #end
> end

In above, I am getting correct output.

Now I want to get output like:

1
21
321
4321
54321

How can I get it?

Thank you.
920f6e4a0fbf997d851455f827a10ebc?d=identicon&s=25 unknown (Guest)
on 2014-02-28 12:56
(Received via mailing list)
Look up the Ruby Integer methods,
http://www.ruby-doc.org/core-2.1.1/Integer.html
there is a fairly short list and you will be able to make a very small
change (change "1.upto(n)")  and get what you want.




"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/28/2014
02:58:25
AM:
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-28 15:49
unknown wrote in post #1138332:
> Look up the Ruby Integer methods,
> http://www.ruby-doc.org/core-2.1.1/Integer.html
> there is a fairly short list and you will be able to make a very small
> change (change "1.upto(n)")  and get what you want.
>
>
>
>
> "ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/28/2014
> 02:58:25
> AM:

As you said, I use for loop in place of "1.upto(n)" then Is it possible
to get correct output?

Thank you.
920f6e4a0fbf997d851455f827a10ebc?d=identicon&s=25 unknown (Guest)
on 2014-02-28 15:57
(Received via mailing list)
No, don't change to a for loop. the 1.upto is the preferred Ruby syntax.

You have
  1.upto(n)
and get
  1 2 ... n

You want
n ... 2 1

so, just reverse what the loop does,

n.downto(1)




"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/28/2014
09:49:44
AM:
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-28 16:17
unknown wrote in post #1138350:
> No, don't change to a for loop. the 1.upto is the preferred Ruby syntax.
>
> You have
>   1.upto(n)
> and get
>   1 2 ... n
>
> You want
> n ... 2 1
>
> so, just reverse what the loop does,
>
> n.downto(1)

Yes, I just not think in that way. At this my stage and my point of view
It's very good answer.

Thank you very much.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.