Problem with this Nested Loop practice


#1

Hi i am practicing nested loop here are the examples that i did, but i
was particularly stuck at this part of nested loop

i can do this example below





**
*

code:
c = 5
for r in 1…5

c.times {print "*"}
c -= 1

print "\n"

end

But how to achieve this result


*

**





i was confused on how to reverse the * on the 5th column
basically i wanted the * to start from the 5th column and decrease down
to 4th row and so on from the right-hand side.

Don’t give me a answer
i want a hint only
Thx alot :slight_smile:


#2

with % operator in string, you can format a string

right aligned:

puts “%10s” % ("*“i)
left aligned
puts “%-10s” % ("
”*i)


#3

Regis d’Aubarede wrote in post #1185356:

with % operator in string, you can format a string

right aligned:

puts “%10s” % ("*“i)
left aligned
puts “%-10s” % ("
”*i)

i tried the following with this code:

n = 1

for i in 1…5

    n.times {print "%5s" % ("*") }
    n += 1

    print  "%2s" % "\n"

end

Result:
*
* *
* * *
* * * *
* * * * *

any hint how to increase the increment from left hand side to down?


#4

Suresh Ilankovan wrote in post #1185355:
[…]

i was confused on how to reverse the * on the 5th column
basically i wanted the * to start from the 5th column and decrease down
to 4th row and so on from the right-hand side.

Don’t give me a answer
i want a hint only
Thx alot :slight_smile:

Think of a line as comprised of " " and “*” in related quantities


#5

B. Onzo wrote in post #1185358:

Suresh Ilankovan wrote in post #1185355:
[…]

i was confused on how to reverse the * on the 5th column
basically i wanted the * to start from the 5th column and decrease down
to 4th row and so on from the right-hand side.

Don’t give me a answer
i want a hint only
Thx alot :slight_smile:

Think of a line as comprised of " " and “*” in related quantities

Yup i did that. I can’t think of any way to print the * from the
left-hand downstairs :slight_smile:

it’s always from


or



#6

Suresh Ilankovan wrote in post #1185359:
[…]

Think of a line as comprised of " " and “*” in related quantities

Yup i did that. I can’t think of any way to print the * from the
left-hand downstairs :slight_smile:

it’s always from


or


<appropriate number of " "> + <appropriate number of “*”>


#7

Suresh Ilankovan wrote in post #1185357:

Regis d’Aubarede wrote in post #1185356:

with % operator in string, you can format a string

right aligned:

puts “%10s” % ("*“i)
left aligned
puts “%-10s” % ("
”*i)

i tried the following with this code:

n = 1

for i in 1…5

    n.times {print "%5s" % ("*") }
    n += 1

    print  "%2s" % "\n"

end
irb(main):001:0> 1.upto(5) { |c| puts “%6s” % ("*"*c) }
*
**




=> 1
irb(main):002:0> 1.upto(5) { |c| puts “%-6s” % ("*"*c) }
*
**




=> 1
irb(main):003:0> 5.downto(1) { |c| puts “%-6s” % ("*"*c) }



**
*

=> 1
irb(main):004:0> 5.downto(1) { |c| puts “%6s” % ("*"*c) }



**
 *

http://asciiflow.com/ :slight_smile:


#8

wow hais solution looks simple, but did’t know how to do this way :slight_smile:


#9

Hi im solving these questions that i am facing right now


n = 1
for i in 1…8

   for c in 1..n
           case i
                 when 1
                    print "1"
                 when 2
                    print "1 2"
                 when 3
                    print "1 2 4"
                 when 4
                    print "1 2 4 8"
                 when 5
                    print "1 2 4 8 16"
                 when 6
                    print "1 2 4 8 16 32"
                 when 7
                    print "1 2 4 8 16 32 64"
                 when 8
                    print "1 2 4 8 16 32 64 128"
           end
              print "\n"
   end

end

Even though it’s static. I tried my very best to be dynamic. Any kind
souls can guide me? Don’t give answer please. I want hint only. I tried
alot of ways to be dynamic, but i can only do static for this questions.
:slight_smile:


#10

n.downto(limit) { |i| puts “*”*i }

5.downto(1) { |i| puts “*”*i } :grin: