April 22nd, 2013, 11:24 PM

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!
April 23rd, 2013, 01:23 AM

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, home laptop, work desktop), Debian GNU/Linux 7.7.0 (server), OpenELEC 5.0 (HTPC), Android 4.2.1 (tablet)
April 23rd, 2013, 01:34 AM

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 n1 x n1, sorry for the inconvenience!
April 23rd, 2013, 11:10 AM

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[i1][j] + 1 # ******2
return A
Comments on this post
Last edited by b49P23TIvg; April 23rd, 2013 at 11:12 AM.
[code]
Code tags[/code] are essential for python code and Makefiles!
April 24th, 2013, 12:56 AM

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[i1][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!