jackneedshelp
jackneedshelp

Reputation: 137

How to count 1 to 9 on a single line in ruby

I'm struggling to figure out how to loop numbers in a single line on ruby.

x = 0
while x <= 9
  puts x
  x +=1
end

This would give you

0
1
2
3
4
5
6
7
8
9

Each on different lines.

But what I want is to get this on a single line so like

01234567891011121314151617181920

Also not limited to just 0-9 more like 0 to infinity on a single line.

The purpose is to make an triangle of any size that follows this pattern.

1
12
123
1234
12345
123456

Each of these would be on a different line. The formatting here won't let me put in on different lines.

Would really like to solve this. It is hurting my head.

Upvotes: 2

Views: 2393

Answers (6)

Cary Swoveland
Cary Swoveland

Reputation: 110685

You said you want "to make a triangle of any size that follows this pattern", so you should not make assumptions about how that should be done. Here are two ways to do that.

#1

def print_triangle(n)
  (1..n).each.with_object('') { |i,s| puts s << i.to_s }
end

print_triangle(9)
1
12
123
1234
12345
123456
1234567
12345678
123456789

#2

def print_triangle(n)
  s = (1..n).to_a.join
  (1..n).each { |i| puts s[0,i] }
end

print_triangle(9)
1
12
123
1234
12345
123456
1234567
12345678
123456789

Upvotes: 2

undur_gongor
undur_gongor

Reputation: 15954

puts (1..9).map(&:to_s).join

Upvotes: 0

Hieu Pham
Hieu Pham

Reputation: 6707

how about this solution:

last_num = 9
str      = (1..last_num).to_a.join # create string 123456789
0.upto(last_num-1){ |i| puts str[0..i] } # print line by line

Upvotes: 1

Horacio
Horacio

Reputation: 2965

The simplest way if you want to start from 1

9.times {|n| puts n+1}

try if you want to start from 0

10.times {|n| puts n}

if you want pyramid format this is one way to do

9.times{|c| puts (1..c+1).to_a.join}

this is the ouput

2.3.0 :025 > 9.times{|c| puts (1..c+1).to_a.join}
1
12
123
1234
12345
123456
1234567
12345678
123456789

Upvotes: -1

Sagar Pandya
Sagar Pandya

Reputation: 9497

Regarding your final aim there are lots of (probably easier) ways, but here's one:

def print_nums k
  k.times { |n| puts (1..(n+1)).map { |i| i*10**(n+1-i) }.inject(:+) }
end

print_nums 9
#1
#12
#123
#1234
#12345
#123456
#1234567
#12345678
#123456789

This approach generates the actual numbers using units, tens, hundreds etc in relation to the line number i.


Thought Process

Looking at a basic example of four lines:

1
12
123
1234

is the same as:

1*10**0                                   #=> 1
1*10**1 + 2*10**0                         #=> 12
1*10**2 + 2*10**1 + 3*10**0               #=> 123
1*10**3 + 2*10**2 + 3*10**1 + 4*10**0     #=> 1234

which in Ruby can be generated with:

(1..1).map { |i| i*10**(1-i) }.inject(:+) #=> 1
(1..2).map { |i| i*10**(2-i) }.inject(:+) #=> 12
(1..3).map { |i| i*10**(3-i) }.inject(:+) #=> 123
(1..4).map { |i| i*10**(4-i) }.inject(:+) #=> 1234

looking for a pattern we can generalise and put in a method:

def print_nums k
  k.times { |n| puts (1..(n+1)).map { |i| i*10**(n+1-i) }.inject(:+) }
end

You could (and should) of course ignore all of the above and just extend the excellent answer by @seph

3.times { |i| (1..(i+1)).each { |n| print n }; puts }
#1
#12
#123

Upvotes: -1

seph
seph

Reputation: 6076

try this:

(1..9).each { |n| print n }
puts

Upvotes: 4

Related Questions