2

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.

Felix
  • 4,510
  • 2
  • 31
  • 46
jackneedshelp
  • 137
  • 1
  • 14

6 Answers6

4

try this:

(1..9).each { |n| print n }
puts
seph
  • 6,066
  • 3
  • 21
  • 19
2

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
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
  • What is the temp string here for? :) – Sergio Tulentsev Oct 09 '16 at 18:19
  • @sergio, I print the first character of `s`, then the first two characters of `s` and so on, rather than creating a string for each line (by creating an array then joining). Have I answered your question? Notice that I modified my answer. – Cary Swoveland Oct 09 '16 at 18:27
  • I don't have anything better to do, so I decided to profile different approaches here. You may find this interesting: http://pastie.org/10940329 – Sergio Tulentsev Oct 09 '16 at 18:36
  • @Sergio, if you mean `print 1`, `print 2` and so on, that doesn't seem as efficient as using strings. Let's see an answer! – Cary Swoveland Oct 09 '16 at 18:36
  • In my benchmarks, using prints is _slightly_ more efficient than that temp string approach. `each_with_object` beats it, though. – Sergio Tulentsev Oct 09 '16 at 18:38
1

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
Hieu Pham
  • 6,577
  • 2
  • 30
  • 50
0
puts (1..9).map(&:to_s).join
undur_gongor
  • 15,657
  • 5
  • 63
  • 75
-1

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
Community
  • 1
  • 1
Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35
  • I am preeeetty sure that you don't need each line to be an actual _number_. Therefore, no need for that "powers of 10" business. Just `each` and `print`. Alternatively, `.to_a` on a range + `.join` – Sergio Tulentsev Oct 09 '16 at 17:22
  • @SergioTulentsev ha yes it's kind of over-the-top (and I do say as such in my answer). I had already started this train of thought and thought it interesting to follow it through. I enjoyed the process:) – Sagar Pandya Oct 09 '16 at 19:02
-1

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
Horacio
  • 2,865
  • 1
  • 14
  • 24
  • you first two lines don't do what OP wants. – Sergio Tulentsev Oct 09 '16 at 17:23
  • @SergioTulentsev I just explain the title of the question "How to count 1 to 9 on a single line in ruby" and then I explain one way to solve the problem of the piramid, sorry if I I hurt you <3 – Horacio Oct 14 '16 at 19:15