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.

Hell,

On 23 Φεβ 2014, at 09:30 , Jaimin P. [email protected] 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: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

You’re welcome,

best regards

Thank you for your reply. It’s helpful for me and i get more knowledge
from your reply.

1.upto(5).each {|n| puts " " * (5 - n) + n.downto(1).to_a.join(" ")}

On Sun, Feb 23, 2014 at 7:07 AM, Jaimin P. [email protected]
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

Panagiotis A. wrote in post #1137696:

Hell,

On 23 Φεβ 2014, at 09:30 , Jaimin P. [email protected] 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.

Jaimin P. 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] }

Regis d’Aubarede wrote in post #1137856:

Jaimin P. 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?

Jaimin P. wrote in post #1137859:

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

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 !

:slight_smile:

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.

Hello Jaimin,

On 26 Φεβ 2014, at 22:55 , Jaimin P. [email protected] 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: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

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 P. [email protected] 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: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

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” [email protected] wrote on 02/24/2014
02:11:33
PM:

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

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.

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” [email protected] wrote on 02/27/2014
07:55:01
AM:

From: Jaimin P. [email protected]
To: [email protected]
Date: 02/27/2014 07:55 AM
Subject: Re: How to do pattern program in Ruby?
Sent by: “ruby-talk” [email protected]

Panagiotis A. wrote in post #1138192:

Hey again,

Sorry for the previous mail, was out of line, you have the code
there…

Jaimin P. wrote in post #1138235:

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

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.

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” [email protected] wrote on 02/27/2014
11:14:55
AM:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs