CS 1MD3 - Winter 2006 - Assignment #2
Generated for Student ID: 0443166
Due Date: Wednesday, February 8th
(You are responsible for verifying that your student number is correct!)
Part 1: Debugging
The following four functions are infested with bugs. Correct them by adding or changing a single
line in each one. Clearly indicate your changes with comments. If you add a line, append the
comment, "# added." If you change a line, append a comment to it containing the original, erroneous line.
Solutions submitted without comments will not be marked.
1
def hammingDistance(str1, str2):
"""Returns the Hamming distance (or "edit distance") between two strings of the
same length. Hamming distance is defined as the minimum number of characters which
must be changed to make the strings identical. For example, the Hamming distance
between "Python" and "Athlon" is 4 (only the last two letters are the same);
the Hamming distance between "noodle" and "needle" is 2 (only the second and
third letters differ)."""
if len(str1) != len(str2):
return None
list1 = list(str1)
list2 = list(str2)
n=len(list1)
distance = 0
i = n-1
while i > 0:
if list1[i] != list2[i]:
distance = distance + 1
i=i-1
return distance
2
def deinterlaceEven(interlacedList):
"""Returns a list consisting of only the even elements of the given list.
For example, deinterlaceEven(['1', 'a', '2', 'b', '3', 'c']) returns [1,2,3]."""
n = len(interlacedList)
if n == 0:
return None
i = 0
evenList = []
while i < N:
evenList.append(interlacedList[i])
i = i+2
return evenList
3
def deinterlaceOdd(interlacedList):
"""Returns a list consisting of only the odd elements of the given list.
For example, deinterlaceOdd(['1', 'a', '2', 'b', '3', 'c']) returns [a,b,c]."""
n = len(interlacedList)
if n <= 1:
return None
i = 1
oddList = []
while i < n:
oddList.append(interlacedList[i])
i = i+2
return oddList
4
def norm(aVector):
"""Computes the Euclidian norm of the given vector in R^n.
The Euclidean norm of x=(x1, x2, ..., xn) is defined as the square root of
x1^2 + x2^2 + ... + xn^2."""
n = len(aVector)
if n <= 0:
return None
theNorm = 0
i = 0
while i < n:
theNorm = theNorm + abs(aVector[i])**2
return pow(theNorm, 0.5)
Part 2: Testing
Create automated tests for the following function using PyUnit. Your tests should ensure that the code produces the expected result in all cases.
def isIsosceles(pt1, pt2, pt3):
"""Accepts three lists of floating point numbers, each list specifying a point in
(real) n-dimensional space. Returns True if the three points are the corners of an
isosceles triangle; returns False otherwise."""
n = len(pt1)
if len(pt2) != n or len(pt3) != n:
return False
edge1sqrd, edge2sqrd, edge3sqrd = 0, 0, 0
for x,y in zip(pt1,pt2):
edge1sqrd = edge1sqrd + (x-y)**2
for x,y in zip(pt1,pt3):
edge2sqrd = edge2sqrd + (x-y)**2
for x,y in zip(pt2,pt3):
edge3sqrd = edge3sqrd + (x-y)**2
if edge1sqrd==edge2sqrd==edge3sqrd: # We'll adopt the convention that equilateral ==> not isosceles
return False
if edge1sqrd==edge2sqrd or edge1sqrd==edge3sqrd or edge2sqrd==edge3sqrd:
return True
else:
return False