The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by drboo]

Hi--I'm teaching an introductory programming class using Python and have
turned up something strange with lists passed into a procedure or function. I
assigned a series of simple problems for students to use for practice, and
among them is the task of creating a procedure or function that inputs two
lists of integers and outputs a list such that each element is the sum of the
corresponding element of the two inputs. If the lists are unequal length, the
result lis should be as long as the shorter list only.

I demonstrated this to a student as a procedure an as a function...and noticed
that when it was a procedure the list passed in to accept the result remained
unchanged, but the shorter list of the two operands is changed although it
never appears on the business end on an assignment. I also noticed that as a
function, this problem still occurs, although the function does return the
correct value.

Here is the function. Any ideas what silly obvious thing I have overlooked?

def ListSum(A,B):
#
# determine shorter list
#
lenA = len(A)
lenB = len(B)

if lenA < lenB:
D=A
else:
D=B

i = 0

while i < lenA and i < lenB:
D[i] = A[i] + B[i]
i = i + 1

return D

------------------
Richrd
import-bot (20211) [Avatar] Offline
#2
Re: Lists As Parameters
[Originally posted by daryl harms]



> Hi--I'm teaching an introductory programming class using Python and have
> turned up something strange with lists passed into a procedure or function. I
> assigned a series of simple problems for students to use for practice, and
> among them is the task of creating a procedure or function that inputs two
> lists of integers and outputs a list such that each element is the sum of the
> corresponding element of the two inputs. If the lists are unequal length, the
> result lis should be as long as the shorter list only.
>
> I demonstrated this to a student as a procedure an as a function...and noticed
> that when it was a procedure the list passed in to accept the result remained
> unchanged, but the shorter list of the two operands is changed although it
> never appears on the business end on an assignment. I also noticed that as a
> function, this problem still occurs, although the function does return the
> correct value.
>
> Here is the function. Any ideas what silly obvious thing I have overlooked?
>
> def ListSum(A,B):
> #
> # determine shorter list
> #
> lenA = len(A)
> lenB = len(B)
>
> if lenA < lenB:
> D=A
> else:
> D=B
>
Richard, what is probably tripping you up is that the above assignment of D to
the shorter of the two list above. If, for example A were the shorter of the
two lists, after the above assignment both A and D will be pointing to this
list

A------------->[.......]
D-------------/

and the modifications made below to D will be made to this list. See section
10.5 on page 104 for another example of this.

> i = 0
>
> while i < lenA and i < lenB:
> D[i] = A[i] + B[i]
> i = i + 1
>
> return D
>
> ------------------
> Richrd

If you modify your assignment to D to a copy of the list, your function should
work as you want.


>>> def ListSum(A,B):
#
# determine shorter list
#
lenA = len(A)
lenB = len(B

if lenA < lenB:
D=A[:] # assign to a copy of list A
else:
D=B[:] # or assign to a copy of list B
i = 0

while i < lenA and i < lenB:
D[i] = A[i] + B[i]
i = i + 1

return D

>>> list1 = [1,2,3,4,5]
>>> list2 = [4,3,2,1]
>>> ListSum(list1,list2)
[5, 5, 5, 5]
>>> list1
[1, 2, 3, 4, 5]
>>> list2
[4, 3, 2, 1]
>>>

Another option would be to use the list append method and replace the
assignment with an initially empty list D,

>>> def ListSum(A,B):
lenD = min(len(A),len(B))
D = []
i = 0
while i < lenD:
D.append(A[i] + B[i])
i = i + 1
return D

>>> ListSum(list1,list2)
[5, 5, 5, 5]
>>> list1
[1, 2, 3, 4, 5]
>>> list2
[4, 3, 2, 1]
>>>