Print Numbers Vertically

C

Chris Foley

Hello,
I am trying to figure out how I can print an array of numbers
vertically. For example

a = [ 1, 24, 546, 1000, 32, 256 ]

output (spaces optional) :

1 2 5 1 3 2
4 4 0 2 5
6 0 6
0

I need this because I've got a string to display horizontally and I
need to display a position number underneath each character column.
I have been thinking about how to do this but it gets complicated
quickly. So far Im thinking:

- convert to string
- for each digit place ( start with 1's place go to biggest )
- for each num
- if has digit
-print digit
- else
print space

Any help would be appreciated.
 
C

Chris Foley

The ouput should have each number in its own column.

Second try:

1 2 5 1 3 2
4 4 0 2 5
6 0 6
0
 
S

Sebastian Hungerecker

Chris said:
=C2=A0I am trying to figure out how I can print an array of numbers
vertically. For example

a =3D [ =C2=A01, 24, 546, 1000, 32, 256 ]

output (spaces optional) :

1 2 5 1 3 2
4 4 0 2 5
6 0 =C2=A0 6
=C2=A0 =C2=A0 =C2=A0 0

a =3D [ 1, 24, 546, 1000, 32, 256 ]
height =3D a.max.to_s.length
a.map do |num|
num.to_s.ljust(height).split(//)
end.transpose.each do |line|
puts line.join(" ")
end

Prints:
1 2 5 1 3 2
4 4 0 2 5
6 0 6
0

HTH,
Sebastian
=2D-=20
NP: Black Sabbath - Killing Yourself to Live
Jabber: (e-mail address removed)
ICQ: 205544826
 
C

Chris Foley

Wow, amazing what you can learn from a solution to a simple problem like
that.
Thank you, Sebastian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,149
Messages
2,570,843
Members
47,390
Latest member
RobertMart

Latest Threads

Top