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

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Hollow square program for python


    Good evening again everyone!

    I just finished a previous code with python. I am new to the language, and have been fiddling around with it for about a month now. I wrote a program to prompt for a number of columns and rows, and print in asterisks.

    import utils

    def print_row(col_count):
    col = 0
    while col < col_count:
    print "*",
    col = col + 1
    print

    def print_rect(row_count, col_count):
    row = 0
    while row < row_count:
    print_row(col_count)
    row = row + 1

    row_count = int(utils.get_int("How many rows of asterisks?\n "))
    col_count = int(utils.get_int("How many columns in each row?\n "))
    print_rect(row_count, col_count)
    print "Bye!"



    I am now trying to kind of get the same idea, but instead of asking for the columns and rows, I want to prompt for the length of the outer side and the length of the inner side , and print a square inside the box of asterisks. I would appreciate any help pointing me in the direction.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Please draw a picture of what you want for given input, keeping in mind that Ulam spirals are more interesting.

    Also post your code with appropriate tags---follow the link at my signature for instructions.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    ********************
    ********************
    ********************
    ****-White Space*****
    ****-White Space*****
    ****-White Space*****
    ****-White Space*****
    ********************
    ********************
    ********************


    Something like that. Where the program asks for the width of the outside, and length of the inside


    Edit : Sorry for the white space text, but the forum wasn't recognizing blank lines
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Please use code tags, as indicated following the link at my signature, to preserve white space so critical with python code.

    My square algorithm works as follows, and is demonstrated in j notation.

    1) Recognize that a unit "radius" 1 arbitrary dimension cube is the volume satisfying, in mathematica notation,
    Sum[x[i]^(2*Infinity)] <= 1 .

    2) Generate a Cartesian grid. Suppose your inner radius is 8 and your outer radius is 12. I let the numerator run from -6 to 6 in steps of 1. And divide by half the inner radius. You'll end up with about the right fraction of points with values less than 1 (in absolute value).

    3) Evaluate the function (this time in python notation) sum(X**large_even_number for X in x) <= 1
    I used 16 for the large even number.

    4) Since that was a logical comparison we now have an array of True and False.

    5) Replace the Boolean with its index as 0 or 1 into a string of '* ' (asterisk space).

    6) Print the matrix. (I expanded the columns to correct for character cell aspect ratio.)

    I'll understand if your happier with <= comparisons.

    Code:
       NB. inner square has edge length about 6, outer square has edge length about 10
       NB. make a Cartesian grid   5r3 is five thirds.
       grid=: {@:;~@:(%~ i:@:[ )&(x:@:<.@:-:)/
       [GRID=: grid   6 10
    ┌─────────┬─────────┬───────┬─────────┬─────────┬──────┬────────┬────────┬──────┬────────┬────────┐
    │_5r3 _5r3│_5r3 _4r3│_5r3 _1│_5r3 _2r3│_5r3 _1r3│_5r3 0│_5r3 1r3│_5r3 2r3│_5r3 1│_5r3 4r3│_5r3 5r3│
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │_4r3 _5r3│_4r3 _4r3│_4r3 _1│_4r3 _2r3│_4r3 _1r3│_4r3 0│_4r3 1r3│_4r3 2r3│_4r3 1│_4r3 4r3│_4r3 5r3│
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │_1 _5r3  │_1 _4r3  │_1 _1  │_1 _2r3  │_1 _1r3  │_1 0  │_1 1r3  │_1 2r3  │_1 1  │_1 4r3  │_1 5r3  │
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │_2r3 _5r3│_2r3 _4r3│_2r3 _1│_2r3 _2r3│_2r3 _1r3│_2r3 0│_2r3 1r3│_2r3 2r3│_2r3 1│_2r3 4r3│_2r3 5r3│
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │_1r3 _5r3│_1r3 _4r3│_1r3 _1│_1r3 _2r3│_1r3 _1r3│_1r3 0│_1r3 1r3│_1r3 2r3│_1r3 1│_1r3 4r3│_1r3 5r3│
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │0 _5r3   │0 _4r3   │0 _1   │0 _2r3   │0 _1r3   │0 0   │0 1r3   │0 2r3   │0 1   │0 4r3   │0 5r3   │
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │1r3 _5r3 │1r3 _4r3 │1r3 _1 │1r3 _2r3 │1r3 _1r3 │1r3 0 │1r3 1r3 │1r3 2r3 │1r3 1 │1r3 4r3 │1r3 5r3 │
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │2r3 _5r3 │2r3 _4r3 │2r3 _1 │2r3 _2r3 │2r3 _1r3 │2r3 0 │2r3 1r3 │2r3 2r3 │2r3 1 │2r3 4r3 │2r3 5r3 │
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │1 _5r3   │1 _4r3   │1 _1   │1 _2r3   │1 _1r3   │1 0   │1 1r3   │1 2r3   │1 1   │1 4r3   │1 5r3   │
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │4r3 _5r3 │4r3 _4r3 │4r3 _1 │4r3 _2r3 │4r3 _1r3 │4r3 0 │4r3 1r3 │4r3 2r3 │4r3 1 │4r3 4r3 │4r3 5r3 │
    ├─────────┼─────────┼───────┼─────────┼─────────┼──────┼────────┼────────┼──────┼────────┼────────┤
    │5r3 _5r3 │5r3 _4r3 │5r3 _1 │5r3 _2r3 │5r3 _1r3 │5r3 0 │5r3 1r3 │5r3 2r3 │5r3 1 │5r3 4r3 │5r3 5r3 │
    └─────────┴─────────┴───────┴─────────┴─────────┴──────┴────────┴────────┴──────┴────────┴────────┘
    
    
       NB. 1 >= length of the vector from 0 0 to the coordinates
       (1 >: mp)&>GRID    NB. circle
    0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0
    0 0 0 1 1 1 1 1 0 0 0
    0 0 0 1 1 1 1 1 0 0 0
    0 0 1 1 1 1 1 1 1 0 0
    0 0 0 1 1 1 1 1 0 0 0
    0 0 0 1 1 1 1 1 0 0 0
    0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0
       
    
       NB. The equation of a square is pow(x,INFINITY)+pow(y,INFINITY)
       NB. almost a square   
       (1 >: +/@:(^&16))&> grid 20 40
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 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0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
       
    
                                             
       1j1 #"1 '* ' {~ (1 >: +/@:(^&16))&> grid 20 40
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * *   * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * *                                           * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * *                                       * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * *   * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Last edited by b49P23TIvg; December 4th, 2012 at 09:27 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo