#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Need help with this problem. ( Python 3.2.1.1)


    So I have this project for school. We are supposed to make :

    h t t p : / / ed.edim.co/9437390/draw_with_loops.txt?Expires=1360962848&Signature=RYlWmjPnkWZTe9Seo7gesm6nucijqq3vVHhbJIr~s1lqNBQ4a9I 7C9uVE9khvJniXarLYw1gH-iC1F-xTWjUtUGV2sc3CzYxQh9mozZVbZAA0TiAwK0BEXOt368gWB7GTb34G2zJe3934AfCnfnAGPgzbQYCL9JsYaulvFJwjkk_&Key-Pair-Id=APKAI3N2VAFIZ34RBHFA

    I need to know how to make the third triangle, printing only 1 character at a time, and nesting atleast one loop, all in under 11 lines of code (Excluding the def triangle():, and triangle() ) Can anyone help?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    2
    The link doesn't seem to work.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    you have to get rid of the spaces in the : h t t p : / /

    it wouldn't let me post a link in my first post
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    The j (link) figure 3 triangle verb fits within the required number of lines.
    Code:
       figure3triangle =: i. (< |)"0/ i:
    
       figure3triangle 8
    1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1
    1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
    1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1
    1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1
    1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1
    1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    
       '* ' ({~ figure3triangle) 8
            *        
           ***       
          *****      
         *******     
        *********    
       ***********   
      *************  
     ***************
    We can make it work in python!
    Code:
    import operator                                                 # line 1
    integers = lambda y: list(range(y) if 0 <= y else reversed(range(-y)))          ; assert integers(8) == [0, 1, 2, 3, 4, 5, 6, 7]
    steps = lambda y: list(range(-y, y + ((0 <= y) - (y < 0)), (0 <= y) - (y < 0))) ; assert [3, 2, 1, 0, -1, -2, -3] == steps(-3)
    dyadicHook = lambda f, g: lambda x, y: f(x, g(y))               # line 4
    table = lambda u: lambda x, y: [[u(X,Y) for Y in y] for X in x] # line 5
    monadicFork = lambda f, g, h: lambda y: g(f(y), h(y))           # line 6
    figure3triangle = monadicFork(integers, table(dyadicHook(operator.lt, operator.abs)), steps) # figure3triangle is a function
    BOOLEAN_ARRAY = figure3triangle(8)                              # line 8
    for ROW in BOOLEAN_ARRAY:                                       # line 9
        print(''.join('* '[ATOM] for ATOM in ROW))                  # line 10
    #print('\n'.join(''.join('* '[ATOM] for ATOM in ROW) for ROW in BOOLEAN_ARRAY))  # replacement for lines 9 and 10
    Last edited by b49P23TIvg; February 15th, 2013 at 06:04 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    The following does it in seven lines but does not nest one loop (note Python 2.7 print statement)
    Code:
    triangle_height=8
    spaces = " " * triangle_height
    to_print = "#"
    for ctr in range(triangle_height):
        print "%s%s" % (spaces, to_print)
        to_print += "##"
        spaces = spaces[1:]
    So maybe something like the following. "Print one character at a time" could be done instead of packing the string if you suppress the space following and/or the new line of a normal print statement.
    Code:
    triangle_height=8
    for ctr in range(triangle_height):
        spaces = ""
        for sp in range(ctr, triangle_height):
            spaces += " "
        stars = "*" + ("*" * ctr*2)     ## or a for() loop
        print "%s%s" % (spaces, stars)

IMN logo majestic logo threadwatch logo seochat tools logo