[Tutor] I need help with my homework. No, really....

Steven D'Aprano steve at pearwood.info
Wed Jul 29 16:33:26 CEST 2015


Part 3...

On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote:
> 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.')


> #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

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.

 
> 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

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.


> 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')


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

Again, I believe these will run all the numbers together.


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

>         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


More information about the Tutor mailing list