[Tutor] I need help with my homework. No, really....
Steven D'Aprano
steve at pearwood.info
Wed Jul 29 14:54:35 CEST 2015
On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote:
> I'm taking a beginning Python course at Austin Community College. I'm also
> taking two other project-based web programming courses. It's summer
> semester, meaning we have eight weeks instead of the usual 16 to finish all
> the requirements.
> The semester ends Friday, July 131st.
July 131st? Whew, you've got over 100 days to complete this!
*wink*
But seriously... more comments (hopefully useful comments this time)
follow below, interleaved with your code. Grab a coffee, this may be a
bit long. Oh, and I'm going to split my reply over a couple of emails.
> Yes, I am aware that I'm a teensy bit screwed.
>
> I have to complete eight programs ("complete" meaning "functioning"). I'm
> having a really tough time with this one. It's matrix arithmetic using 2d
> arrays.
[...]
> Given x as an array of [5,3] and y as an array of [3,7] perform the
> following:
>
> 1. Load array x column-wise and array y row-wise
I'm not sure that I understand what this means. I think what they mean
is that if the data looks like this:
10, 20, 30, 40, 50, 60
and x and y are both 3x2 arrays, we end up with these:
# read data down the columns first
x = [ [10, 40],
[20, 50],
[30, 60] ]
# read data across the rows first
y = [ [10, 20],
[30, 40],
[50, 60] ]
> 2. Multiply x by y to compute array z
> 3. Compute the sum of all elements in column 2 of array x and add it to the
> sum of all elements in row 2 of y (the first row/column is 0, the second is
> 1, etc. That got me at first)
> 4. Compute the smallest element in row 1 of y
> ---using appropriate headings:
> 5. Print out matrices x, y, and z (display on screen, but y'all probably
> get that)
> 6. Print out sum and smallest element
>
> The data with which array x is loaded:
> 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4
>
> The data with which array y is loaded:
> 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1
>
> Must use functions named as follows:
> LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA
>
> lab5.dat is simply a dat file with the data with which the arrays are
> loaded in one long line, each separated by commas.
Below, you have lab5x.dat and lab5y.dat. Are there two files, or just
one? That's going to make a big difference to the way you read the
input.
> Thanks- in advance- no more comments after the program.
>
> This is what I have thus far:
>
> #Lab #5
> #COSC 1336-31493
> #SUM 2015 NRG
> #Tu/Th 1:15-4:25pm
>
> def main():
> #matrix initialization
> x=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
> y=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
You can simplify the matrix initialization a little bit by using list
multiplication:
x = [ [0]*3, [0]*3, [0]*3 ]
and similarly for y, and z. What they do should be quite obvious:
[0]*2 --> [0, 0]
['hello']*3 --> ['hello', 'hello', 'hello']
Now, if you're paying attention, you might think "Wait, why don't I
multiply each row as well?"
[ [0]*3 ]*5 # Don't do this!
I don't want to spend to much time on this, but in a nutshell, the above
looks like it should work, but it doesn't work as you would expect
because it doesn't copy the inner list. Instead of getting five
different rows of [0, 0, 0], you get the same row repeated five times.
If my explanation doesn't make sense to you, feel free to ask, or feel
free to just accept it on faith that [ [0]*3 ]*5 will not work the way
you want. You can always come back to discuss this later.
> z=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
Your indentation here got messed up. Unfortunately sometimes email
doesn't work well with indentation, which is sad. To fix this, you need
to indent the line z = ... so that it in aligned with the other lines
inside the main function.
z = [ [0]*7, [0]*7, etc. ]
> #file declaration
> infile = open('lab5x.dat','r')
> infile = open('lab5y.dat','r')
> outfile = open('lab5.out', 'w')
You have two variables both called "infile", that isn't going to work.
You need to give them separate names, say, infileX and infileY.
> #variables
> sumx = 0
> sumy = 0
> small = 0
> A = 0
> B = 0
> C = 0
I'm not sure that you need these A B C variables. I think you actually
want to use x, y, z, the three matrices you already initialized.
> #call functions
> LOADX(infile, A)
> LOADY(infile, B)
> COMPUTEZ(A, B, C)
> SUMMATION(A, B)
> SMALLEST(A)
> OUTDATA(file, A, B, C)
That will become:
LOADX(infileX, x)
LOADY(infileY, y)
COMPUTEZ(x, y, z)
SUMMATION(x, y)
SMALLEST(x)
OUTDATA(outfile, x, y, z)
> #close files
> infile.close()
> infile.close()
> outfile.close()
> dummy = input('Press any key to continue.')
Don't forget to change the names of those infiles.
More to follow in my next email.
--
Steve
More information about the Tutor
mailing list