ruby - How to work in nested `for`-loop -


i need output of floyd triangle like:

1 0 1 1 0 1 0 1 0 1 

i tried. didn't exactly. can explain logic?

this code tried:

k = 0 in 1..5    j in 1..5      if (i%2)==0;        k = (j%2==0) ? 1:0;      else;        k = (j%2==0) ? 0:1;        puts k,'';      end   end   puts end 

the main issue here in order "triangle" shape of output, need inner loop increment 1 instead of 1 5.

k = 0 in 1..5    j in 1..i     if (i%2)==0       k = j + 1     else       k = j     end     print "#{k%2} "   end   puts end 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -