Forum: Ruby Problem with this Nested Loop practice

Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-12-15 15:58
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 :)
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2016-12-15 16:08
with  % operator in string, you can format a string

right aligned:
  > puts "%10s" % ("*"*i)
left aligned
  > puts "%-10s" % ("*"*i)
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-12-15 16:21
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?
150e3d60e1da33d0ef45a3fab66d6019?d=identicon&s=25 B. Onzo (bonzo_goat)
on 2016-12-15 16:54
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 :)

Think of a line as comprised of " " and "*" in related quantities
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-12-15 17:32
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 :)
>
> 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 :)

it's always from

*
* *
* * *

or

* * *
* *
*
150e3d60e1da33d0ef45a3fab66d6019?d=identicon&s=25 B. Onzo (bonzo_goat)
on 2016-12-15 18:02
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 :)
>
> it's always from
>
> *
> * *
> * * *
>
> or
>
> * * *
> * *
> *

<appropriate number of " "> + <appropriate number of "*">
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2016-12-15 20:49
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/  :)
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-12-16 01:22
wow hais solution looks simple, but did't know how to do this way :)
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2017-01-09 05:24
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.
:)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.