Thread: Listoflists

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

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Listoflists


    Hi there guys;

    im supposed to return this table:

    1 2 3
    0 3 4
    0 0 5

    in the form of a list of lists, so the function listoflists will return as so: [[1,2,3],[3,4],[5]]

    my attempt:
    ive planned to first create 3 arrays of size 3 full of zeroes, as such: [[0,0,0],[0,0,0],[0,0,0]]
    and then further append them with the numbers i need, but this didnt really work out well for me, as i needed to delete the extra zeroes but couldnt, and the loops used to append everything into the lists confused the hell out of me. any help will be appreciated, thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    Originally Posted by mrhlth
    Hi there guys;

    im supposed to return this table:

    1 2 3
    0 3 4
    0 0 5

    in the form of a list of lists, so the function listoflists will return as so: [[1,2,3],[3,4],[5]]
    Since you didnít specify whatís being sent TO the function, I guess thisíll do:

    Code:
    def listoflists():
        return [[1, 2, 3], [3, 4], [5]]
    My armada: openSUSE 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by SuperOscar
    Since you didnít specify whatís being sent TO the function, I guess thisíll do:

    Code:
    def listoflists():
        return [[1, 2, 3], [3, 4], [5]]
    ah my bad! the parameter for listoflists is int n, when n = 4, the table is:
    1 2 3
    0 3 4
    0 0 5

    and if n = 5, table:
    1 2 3 4
    0 3 4 5
    0 0 5 6
    0 0 0 7

    so the input for this is n, and it returns a list of lists based on a table created, with dimensions n-1 x n-1, sorry for the inconvenience!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Following program is close but runs into index error. I found the bug and fixed it. Can you? I found a simple dependence of A[i][j] on i and j. The line marked with ***1 is unneeded, and the dependence of a[i][j] on with the previous row can be eliminated on the line marked ***2 . Can you find a simplification?
    Code:
    def f(n):
        '''
            whacko array creator
            >>> f(4) == [[1,2,3],[0,3,4],[0,0,5]]
            True
        '''
        A = [[0 for i in range(n - 1)] for j in range(n - 1)]
        A[0][:] = range(1, n) # first row  ******1
        for i in range(1, n - 1): # (yes, this range would need to change as well to answer the second question)
            for j in range(i, n):
                A[i][j] = A[i-1][j] + 1  # ******2
        return A

    Comments on this post

    • mrhlth agrees
    Last edited by b49P23TIvg; April 23rd, 2013 at 12:12 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Following program is close but runs into index error. I found the bug and fixed it. Can you? I found a simple dependence of A[i][j] on i and j. The line marked with ***1 is unneeded, and the dependence of a[i][j] on with the previous row can be eliminated on the line marked ***2 . Can you find a simplification?
    Code:
    def f(n):
        '''
            whacko array creator
            >>> f(4) == [[1,2,3],[0,3,4],[0,0,5]]
            True
        '''
        A = [[0 for i in range(n - 1)] for j in range(n - 1)]
        A[0][:] = range(1, n) # first row  ******1
        for i in range(1, n - 1): # (yes, this range would need to change as well to answer the second question)
            for j in range(i, n):
                A[i][j] = A[i-1][j] + 1  # ******2
        return A
    Thank you SO MUCH for your help! i found the bug and fixed it, now its giving me the correct answers! Thank you again!

IMN logo majestic logo threadwatch logo seochat tools logo