[Tutor] exercise problem

Roelof Wobben rwobben at hotmail.com
Fri Aug 27 19:56:35 CEST 2010



Now I have this :


def add_vectors(u, v):
      >>> add_vectors([1, 0], [1, 1])
      [2, 1]
      >>> add_vectors([1, 2], [1, 4])
      [2, 6]
      >>> add_vectors([1, 2, 1], [1, 4, 3])
      [2, 6, 4]
      >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
      [13, -4, 13, 5]
    while teller < len(u):
        getal1 = u[teller] + v[teller]
    return uitkomst2
uitkomst= []
vector= [1, 2, 1], [1, 4, 3]
uitkomst = add_vectors(u,v)
print uitkomst         

The only problem I have is to build up uitkomst2.


on every loop getal1 has the value of the outcome.


So I thought this would work


uitkomst2 [teller] = getal1 


But then i get a out of range.



Date: Fri, 27 Aug 2010 10:19:30 -0700
From: alan.gauld at btinternet.com
Subject: Re: [Tutor] exercise problem
To: rwobben at hotmail.com

>u v, result
first example.
u : [1.0]  v: [1,1] result [2.1] 

OK, Great, you got that.

first split  u en v in only numbers.

No, you should leave them as lists.

Then add u[0] en v[0] and u[1] and v[1] 
put the outcome in the new  vector.

Almost except you don't know how many elements there will be so 
you need a loop to process all the elements.

outcome= [outcome u, outcome[u]
return outcome.

This confused me, the output should be:

[ u[0]+v[0], u[1]+v[1], u[2]+v[2], ...., [u[n]+v[n] ]

And in case you are wondering, a vector is used in math to, 
for example, represent a point in space. A 2 dimensional 
point has an X,Y coordinate so we can create a 2 element 
vector: [x,y]

We can add, subtract and multiply vectors. Vectors can also 
be used to represent other physical measures, for example 
AC electric current has a magnitude and phase angle at any 
point in time. These two values can be combined as a vector.
We can use bigger vectors to represent, for example the 
set of inputs to a parallel port printer, so we would have a 
list of 8 binary values. Once again we can express mathematically 
the processing of this vector as a function and by applying the 
function to the vector deermine the expected output for any given input. 
That could be, for example, an ASCII character for the printer example...
They are very important in science and engineering.

The tutorial you are following does expect the reader to be quite 
math literate - it is part of a university comp sci course after all. If
you do not have a strong math background you may find some of 
the others more readable. For example mine( :-) ) does not assume 
any knowledge of math beyond basic high school level - really basic 
geometry and arithmetic.

Alan G.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100827/83da590c/attachment-0001.html>

More information about the Tutor mailing list