Discussion:
[Tutor] I need help with my homework. No, really....
Lissa Hopson
2015-07-29 09:16:58 UTC
Permalink
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.
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.

If ANYONE can help me, I'd really appreciate it. Someday, maybe I can be
the guy helping someone else...except I'm a girl. Whatever. I digress. I'm
already planning to retake the course because I want to get more out of it-
I like Python a lot, it's just really difficult to absorb it all that
fast...especially since I'm used to HTML and JavaScript.

Okay- so these are the directions for the program, and I'll cut and paste
my program so far from the interpreter to the email. Don't want to freak
anyone out with attachments. It's gonna be a long email.


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
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.
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]]

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]]
#file declaration
infile = open('lab5x.dat','r')
infile = open('lab5y.dat','r')
outfile = open('lab5.out', 'w')
#variables
sumx = 0
sumy = 0
small = 0
A = 0
B = 0
C = 0

#call functions
LOADX(infile, A)
LOADY(infile, B)
COMPUTEZ(A, B, C)
SUMMATION(A, B)
SMALLEST(A)
OUTDATA(file, A, B, C)
#close files
infile.close()
infile.close()
outfile.close()
dummy = input('Press any key to continue.')

#develop functions

#load matrix x
def LOADX(infile, A):
#local variables
n=0
k=0
s=0
templist = infile.readline().strip('\n').split(',')
while (k<3):
j=0
while(j<5):
A[j][k] = int(templist[n])
s=s+A[j][k]
j=j+1
k=k+1
n=n+1

#load matrix y
def LOADY(infile, B):
#local variables
n=0
j=0
templist = infile.readline().strip('\n').split(',')
while (j<3):
k=0
while (k<7):
B[j][k] = int(templist[n])
s=s+B[j][k]
j=j+1
n=n+1
k=k+1

#define computation of Z matrix
def COMPUTEZ (A, B, C):
i=0
while (i<5):
j=0
while (j<=7):
k=0
while (k<=3):
C[i][j]= C[i][j]+ A[i][k] * B[k][j]
k=k+1
j=j+1
i=i+1



#def summation
def SUMMATION(x,y):
s=0
k=0
j=0
while (k<5):
sumx=sumx + x[k][2]
k=k+1
while (j<7):
sumy=sumy + y[2][j]
j=j+1
s=sumx + sumy

#def smallest
def SMALLEST (B):
k=0
s=B[1][k]
k=k+1
while (k<7):
if(s> B[1][k]):
s=B[1][k]
k=k+1





def OUTDATA(outfile, x, y, z,SMALLEST,SUMMATION):
i=0
j=0
k=0
while (k<3):
print(A[k][0],A[k][1],A[k][2],A[k][3],A[k][4])
k=k+1

file.write[str(A[k][0])+str(A[k][1])+str(A[k][2])+str(A[k][3])+str(A[k][3])+str(A[k][4])]
while (j<7):
print(B[j][0],B[j][1],B[j][2])
j=j+1
file.write[str(B[j][0])+str(B[j][1])+str(B[j][2])]
while (i<7):
print(C[i][0],C[i][1],C[i][2],C[i][3],C[i][4])
file.write[str(C[i][0]+C[i][1]+C[i][2]+C[i][3]+C[i][4])]
print ('Summation= ',SUMMATION)
file.write('Summation= ', SUMMATION)
print ('Smallest= ',SMALLEST)
file.write('Smallest= ',SMALLEST)

main()
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-07-29 12:54:35 UTC
Permalink
Post by Lissa Hopson
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.
Post by Lissa Hopson
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.
[...]
Post by Lissa Hopson
Given x as an array of [5,3] and y as an array of [3,7] perform the
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] ]
Post by Lissa Hopson
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
5. Print out matrices x, y, and z (display on screen, but y'all probably
get that)
6. Print out sum and smallest element
1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1
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.
Post by Lissa Hopson
Thanks- in advance- no more comments after the program.
#Lab #5
#COSC 1336-31493
#SUM 2015 NRG
#Tu/Th 1:15-4:25pm
#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.
Post by Lissa Hopson
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. ]
Post by Lissa Hopson
#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.
Post by Lissa Hopson
#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.
Post by Lissa Hopson
#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)
Post by Lissa Hopson
#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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-07-29 13:59:45 UTC
Permalink
Part 2...
Post by Lissa Hopson
Given x as an array of [5,3] and y as an array of [3,7] perform the
1. Load array x column-wise and array y row-wise
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
5. Print out matrices x, y, and z (display on screen, but y'all probably
get that)
6. Print out sum and smallest element
1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1
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.
Thanks- in advance- no more comments after the program.
#load matrix x
#local variables
n=0
k=0
s=0
It's hard to tell what those variables mean from the names. It may be
more useful to give them descriptive names. I think that k is the
column number, j (below) is the row number, n is an index into the
templist you generate next, and s is, well, I have no idea what s is.
You do some calculations on s, but then it never gets used, so I'm not
sure what it is for.


item = 0
column = 0
s = 0 # huh?
Post by Lissa Hopson
templist = infile.readline().strip('\n').split(',')
To be clear, this reads the first line from the file, and one line only.
It removes the newline \n from the end, then splits on commas, and
returns a list of strings, say:

['1', '2', '3', '4', ...]

Is that what you expect?
Post by Lissa Hopson
j=0
A[j][k] = int(templist[n])
s=s+A[j][k]
j=j+1
k=k+1
n=n+1
Assuming s in not needed, this becomes:

while (column < 3):
row = 0
while(row < 5):
A[row][column] = int(templist[item])
row = row + 1
column = column + 1
item = item + 1


But that can't be right, because you end up processing:

column=0, row=0
column=1, row=1
column=2, row=2

and then stopping. That only gives you three numbers. What you need is
to process fifteen numbers:

column=0, row=0
column=0, row=1
column=0, row=2
column=0, row=3
column=0, row=4
column=1, row=0
...
column=2, row=4

The way to do that is to only increase the column when you've processed
all the rows. Here's a sketch, you can fill in the details:

while (column < 3):
while(row < 5):
process one element A[row][column]
add one to row
# when we get here (outdented), we've finished the inner
# while loop, but are still inside the outer while loop
add one to column
Post by Lissa Hopson
#load matrix y
LOADY should be almost exactly the same as LOADX, except that instead of
looping down the columns, you should loop across the rows. So:

while row < 3:
while column < 7:

but otherwise more or less the same as LOADX.
Post by Lissa Hopson
#define computation of Z matrix
Try re-writing COMPUTEZ with row and columns, as above, and see if that
Post by Lissa Hopson
i=0
j=0
k=0
C[i][j]= C[i][j]+ A[i][k] * B[k][j]
k=k+1
This bit can't work, because you have a while loop where k never
advances!

while k <= 3:
process C[i][j] ...

but k doesn't change. So Python will loop forever, or until you get sick
of waiting and type Ctrl-C to halt it. You need to advance k inside the
while loop:

while k <= 3:
process C[i][j] ...
k = k + 1

Remember that the body of the while loop is defined by the *indented*
block beneath it. You do have a k = k+1 beneath the while loop, but it
isn't indented enough, so it counts as *outside* the while block.

I haven't studied it in detail, but you can try fixing that and see if
it works.
Post by Lissa Hopson
#def summation
s=0
k=0
j=0
sumx=sumx + x[k][2]
k=k+1
sumy=sumy + y[2][j]
j=j+1
s=sumx + sumy
This can't work, because sumx and sumy don't have a value to start
with. You need to initialize them (perhaps zero?) first. Actually, I
don't think you need them at all. I think you can just calculate the
total directly:

total = 0
while row < 5:
total = total + x[row][2]
while column < 7:
total = total + y[2][column]

You'll need to initialize the variables, advance them, etc.


More to follow...
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-07-29 14:33:26 UTC
Permalink
Part 3...
Post by Lissa Hopson
1. Load array x column-wise and array y row-wise
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
5. Print out matrices x, y, and z (display on screen, but y'all probably
get that)
6. Print out sum and smallest element
1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1
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.
Thanks- in advance- no more comments after the program.
#Lab #5
#COSC 1336-31493
#SUM 2015 NRG
#Tu/Th 1:15-4:25pm
#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]]
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]]
#file declaration
infile = open('lab5x.dat','r')
infile = open('lab5y.dat','r')
outfile = open('lab5.out', 'w')
#variables
sumx = 0
sumy = 0
small = 0
A = 0
B = 0
C = 0
#call functions
LOADX(infile, A)
LOADY(infile, B)
COMPUTEZ(A, B, C)
SUMMATION(A, B)
SMALLEST(A)
OUTDATA(file, A, B, C)
#close files
infile.close()
infile.close()
outfile.close()
dummy = input('Press any key to continue.')
#def smallest
k=0
s=B[1][k]
k=k+1
s=B[1][k]
k=k+1
I don't think that works at all. There doesn't seem to be any attempt to
check for the smallest value.

Python has a function, min(), which can take a list of values and
returns the smallest of them. So we can do the following:

def SMALLEST(B):
# Return the smallest value in row 1 of matrix B.
get row one
pass it to function min()
return the result


Obviously that's not actual Python code!

Now, remember, your matrix looks like this:

[ [a, b, c, d], # row 0
[e, f, g, h], # row 1
etc.

So getting a row is easy. (Getting a column is trickier.)

the_row = B[1]
result = min(the_row)
return result


will put row 1 into variable the_row, then pass it to min(), and
finally return it.
Post by Lissa Hopson
i=0
j=0
k=0
print(A[k][0],A[k][1],A[k][2],A[k][3],A[k][4])
k=k+1
This should be printing the x matrix, but you're using variable A
instead, which as far as I understand it, won't exist. I think the
easiest fix for this problem is to change the name in the function
declaration:

def OUTDATA(outfile, x, y, z, SMALLEST, SUMMATION):

becomes:

def OUTDATA(outfile, A, B, C, SMALLEST, SUMMATION):


Note carefully that you have a clash between the names of the
*function* SMALLEST and the argument SMALLEST. Python won't be
confused, but you may be! I recommend that you change the name in the
function declaration.
Post by Lissa Hopson
file.write[str(A[k][0])+str(A[k][1])+str(A[k][2])+str(A[k][3])+str(A[k][3])+str(A[k][4])]
Three problems with this one line:

(1) The indentation is lost. Maybe that's just an email thing.

(2) The variable should be called outfile, not file.

(3) You're writing the numbers mashed up together: "12345678"
instead of "12,34,56".

Here's a little trick: you can join a list of strings with commas like
this:

list_of_strings = ['12', '34', '56']
print( ','.join(list_of_strings) )

(except you won't use print, you will write it to a file).

So first you make a list of numbers making up the row:

row = A[k][:]

Convert each item from an int to a str:

row = [str(n) for n in row]

Join with commas:

thestring = ','.join(row)

and finally write it to the file:

# don't forget the newline at the end of each line
outfile.write(thestring + '\n')
Post by Lissa Hopson
print(B[j][0],B[j][1],B[j][2])
j=j+1
file.write[str(B[j][0])+str(B[j][1])+str(B[j][2])]
print(C[i][0],C[i][1],C[i][2],C[i][3],C[i][4])
file.write[str(C[i][0]+C[i][1]+C[i][2]+C[i][3]+C[i][4])]
Again, I believe these will run all the numbers together.
Post by Lissa Hopson
print ('Summation= ',SUMMATION)
file.write('Summation= ', SUMMATION)
The print() line is okay, because Python will happily print ints as well
as strings. But you can't write ints to a file, you need to convert to a
string first.
Post by Lissa Hopson
print ('Smallest= ',SMALLEST)
file.write('Smallest= ',SMALLEST)
Likewise.


Whew! I'm not sure if I caught everything. There's probably some more
bugs that escaped me, but that should give you a good start to go on
with.

Good luck! Let us know how you go!
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...