I need to do a cycle within another cycle, from a total number of iterations and number of iterations per cycle, it occurred to me to do it in for and it works, I could also do it in while, but I suspect that it is not the best process. especially since I had to do an extra cycle to take into account the rest of the division.
(UPDATE)
this is the code:
puts 'KABUUM!!!'
total = gets.chomp.to_i
ciclo = gets.chomp.to_i
ciclos = total / ciclo
resto = total % ciclo
c = 0
linea = ''
for x in 1..ciclos do
linea = ''
for y in 1..ciclo do
c += 1
linea << ',' if linea != ''
linea << c.to_s
end
puts linea
end
linea = ''
for n in (c + 1)..total do
linea << ',' if linea != ''
linea << n.to_s
end
puts linea if linea != ''
COMMAND LINE OUTPUT: (several results, depending on the entries)
D:\sites\ruby>ruby bucle.rb
KABUUM!!!
34
6
1,2,3,4,5,6
7,8,9,10,11,12
13,14,15,16,17,18
19,20,21,22,23,24
25,26,27,28,29,30
31,32,33,34
D:\sites\ruby>ruby bucle.rb
KABUUM!!!
25
5
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
D:\sites\ruby>ruby bucle.rb
KABUUM!!!
22
10
1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21,22
D:\sites\ruby>
Any idea how it can be optimized ??? in advance, thank you very much ...