How to do pattern program in Ruby?

1
       2 1
      3 2 1
     4 3 2 1
    5 4 3 2 1

How to do program like above pattern?

Thank you.

···

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

Hell,

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

···

On 23 Φεβ 2014, at 09:30 , Jaimin Pandya <lists@ruby-forum.com> wrote:
--
"The fool doth think he is wise, but the wise man knows himself to be a fool." - William Shakespeare

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.

···

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

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.

···

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

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.

···

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

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/\.

Panagiotis Atmatzidis wrote in post #1137696:

Hell,

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.

···

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

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

Hello Jaimin,

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

Yes that's a nice start.

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?

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

···

On 26 Φεβ 2014, at 22:55 , Jaimin Pandya <lists@ruby-forum.com> wrote:
--
"The fool doth think he is wise, but the wise man knows himself to be a fool." - William Shakespeare

Hey again,

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

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"

     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.

--
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

···

On 26 Φεβ 2014, at 22:55 , Jaimin Pandya <lists@ruby-forum.com> wrote:
--
"The fool doth think he is wise, but the wise man knows himself to be a fool." - William Shakespeare

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:

From: Jaimin Pandya <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Date: 02/27/2014 11:15 AM
Subject: Re: How to do pattern program in Ruby?
Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>

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.

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

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.

···

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

Look up the Ruby Integer methods,

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:

From: Jaimin Pandya <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Date: 02/28/2014 02:59 AM
Subject: Re: How to do pattern program in Ruby?
Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>

> 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.

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

unknown wrote in post #1138332:

Look up the Ruby Integer methods,
Class: Integer (Ruby 2.1.1)
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.

AM:

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

Thank you.

···

"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/28/2014 > 02:58:25

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

another alternative: Pyramid scheme · GitHub

···

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/\.

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] }

···

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

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.

···

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

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.

···

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

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:

From: Jaimin Pandya <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Date: 02/28/2014 09:50 AM
Subject: Re: How to do pattern program in Ruby?
Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>

unknown wrote in post #1138332:
> Look up the Ruby Integer methods,
> Class: Integer (Ruby 2.1.1)
> 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.

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

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.

···

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

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?

···

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