April 23rd, 2013, 12:24 AM
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],]
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, 02:23 AM
Since you didnít specify whatís being sent TO the function, I guess thisíll do:
Originally Posted by mrhlth
return [[1, 2, 3], [3, 4], ]
My armada: openSUSE 13.2 (home desktop, home laptop, work desktop), Ubuntu 14.04 LTS (server), OpenELEC 5.0 (HTPC), Debian GNU/Linux 7.7.0 (mini laptop), Android 4.2.1 (tablet)
April 23rd, 2013, 02:34 AM
ah my bad! the parameter for listoflists is int n, when n = 4, the table is:
Originally Posted by SuperOscar
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!
April 23rd, 2013, 12:10 PM
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?
whacko array creator
>>> f(4) == [[1,2,3],[0,3,4],[0,0,5]]
A = [[0 for i in range(n - 1)] for j in range(n - 1)]
A[:] = 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
Comments on this post
Last edited by b49P23TIvg; April 23rd, 2013 at 12:12 PM.
[/code] are essential for python code and Makefiles!
April 24th, 2013, 01:56 AM
Thank you SO MUCH for your help! i found the bug and fixed it, now its giving me the correct answers! Thank you again!
Originally Posted by b49P23TIvg