Thread: 2D lists

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

    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    2D lists


    I have no experience with python coding and i got an assignment about "Drawing Hills using 2D Lists

    Could someone offer me some help, but i need it quick cause i got to present the assignment soon.

    {Assignment}
    A classic draw some ASCII patterns problem, turned onto it's horizontal side. Lets draw some hills, of various heights.

    The input file DATA1.txt will contain 5 lines, integers 0 <= N <= 5

    The output file OUT1.txt will contain 5 lines, the scene generated, as described below.

    Background is drawn with . (period)s, hills are triangles in shape, height specified by input, and are drawn with letter x. Refer to sample input.

    Code:
    Sample Input
    Code:
    1
    2
    3
    0
    4
    Code:
    Sample output
    Code:
    .................
    .............x...
    ......x.....xxx..
    ..x..xxx...xxxxx.
    xxxxxxxxx.xxxxxxx
    Hints

    • This is a perfect time to try and use a 2D list (matrix.) This assignment is possible to complete using a series of nested for-loops and interesting math, but using a matrix will be easier/better.

    • When you are ready to print out your matrix, you may want an easier way to get those array elements back into a single string. Investigate Python's join() function for this.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    This forum actually isn't for homework cheating. You should supply your programming effort and request specific help.

    The input file DATA1.txt will contain 5 lines, integers 0 <= N <= 5

    Can you write code to open and read a file line by line? The data will not be of integer type. Can you convert a string representation of a number to an integer?


    The output file OUT1.txt will contain 5 lines, the scene generated, as described below.

    Can you, using python, create a file? You'll need to.

    As far as the algorithm goes, I'd create an array that's 9*5 rows by 5 columns, all dots. Work on the rows one at a time. Delete the excess rows, and transpose. Then send it to output.

    You'll realize, I hope, the project is a bit screwy. Only the 0 height hills return to 0. That is, the 0 is a pain-in-the-butt special-case.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo