Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 573600d

Browse files
committedMar 21, 2012
Homework 5.2 unit tests
1 parent 44598dd commit 573600d

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
 

‎homework5.2/task.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -------------
2+
# User Instructions
3+
#
4+
# Here you will be implementing a cyclic smoothing
5+
# algorithm. This algorithm should not fix the end
6+
# points (as you did in the unit quizzes). You
7+
# should use the gradient descent equations that
8+
# you used previously.
9+
#
10+
# Your function should return the newpath that it
11+
# calculates..
12+
#
13+
# Feel free to use the provided solution_check function
14+
# to test your code. You can find it at the bottom.
15+
#
16+
# --------------
17+
# Testing Instructions
18+
#
19+
# To test your code, call the solution_check function with
20+
# two arguments. The first argument should be the result of your
21+
# smooth function. The second should be the corresponding answer.
22+
# For example, calling
23+
#
24+
# solution_check(smooth(testpath1), answer1)
25+
#
26+
# should return True if your answer is correct and False if
27+
# it is not.
28+
29+
from math import *
30+
31+
# Do not modify path inside your function.
32+
path=[[0, 0],
33+
[1, 0],
34+
[2, 0],
35+
[3, 0],
36+
[4, 0],
37+
[5, 0],
38+
[6, 0],
39+
[6, 1],
40+
[6, 2],
41+
[6, 3],
42+
[5, 3],
43+
[4, 3],
44+
[3, 3],
45+
[2, 3],
46+
[1, 3],
47+
[0, 3],
48+
[0, 2],
49+
[0, 1]]
50+
51+
############# ONLY ENTER CODE BELOW THIS LINE ##########
52+
53+
# ------------------------------------------------
54+
# smooth coordinates
55+
# If your code is timing out, make the tolerance parameter
56+
# larger to decrease run time.
57+
#
58+
59+
def smooth(path, weight_data = 0.1, weight_smooth = 0.1, tolerance = 0.00001):
60+
61+
#
62+
# Enter code here
63+
#
64+
65+
# deep copy
66+
newpath = [[0 for row in range(len(path[0]))] for col in range(len(path))]
67+
for i in range(len(path)):
68+
for j in range(len(path[0])):
69+
newpath[i][j] = path[i][j]
70+
71+
72+
# thank you - EnTerr - for posting this on our discussion forum
73+
74+
#newpath = smooth(path)
75+
#for i in range(len(path)):
76+
# print '['+ ', '.join('%.3f'%x for x in path[i]) +'] -> ['+ ', '.join('%.3f'%x for x in newpath[i]) +']'
77+
78+

‎homework5.2/test.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import unittest
2+
import task
3+
from math import *
4+
5+
class TestSmoothing(unittest.TestCase):
6+
7+
def assertSmoothingValid(self, path, newpath_expected):
8+
path_persistent = [pair[:] for pair in path]
9+
10+
newpath = task.smooth(path)
11+
12+
self.assertTrue(type(newpath) == type(newpath_expected),
13+
"Function doesn't return a list")
14+
self.assertTrue(len(newpath) == len(newpath_expected),
15+
"Newpath has the wrong length")
16+
self.assertEqual(path, path_persistent,
17+
"Original path variable was modified by your code")
18+
for index, got, expected in zip(range(len(newpath_expected)), newpath, newpath_expected):
19+
self.assertTrue(type(got) == type(expected),
20+
"Returned list doesn't contain a point at position %d" % index)
21+
self.assertTrue(len(got) == len(expected),
22+
"Returned list doesn't contain a list of two coordinates "
23+
"at position %d" % index)
24+
self.assertAlmostEqual(got[0], expected[0], 3,
25+
"X coordinate differs for point %d: "
26+
"expected %.3f, got %.3f" % (index, expected[0], got[0]))
27+
self.assertAlmostEqual(got[1], expected[1], 3,
28+
"Y coordinate differs for point %d: "
29+
"expected %.3f, got %.3f" % (index, expected[1], got[1]))
30+
31+
def test_videoPath(self):
32+
path = [[0, 0],
33+
[1, 0],
34+
[2, 0],
35+
[3, 0],
36+
[4, 0],
37+
[5, 0],
38+
[6, 0],
39+
[6, 1],
40+
[6, 2],
41+
[6, 3],
42+
[5, 3],
43+
[4, 3],
44+
[3, 3],
45+
[2, 3],
46+
[1, 3],
47+
[0, 3],
48+
[0, 2],
49+
[0, 1]]
50+
newpath_expected = [[0.5449300156668018, 0.47485226780102946],
51+
[1.2230705677535505, 0.2046277687200752],
52+
[2.079668890615267, 0.09810778721159963],
53+
[3.0000020176660755, 0.07007646364781912],
54+
[3.9203348821839112, 0.09810853832382399],
55+
[4.7769324511170455, 0.20462917195702085],
56+
[5.455071854686622, 0.4748541381544533],
57+
[5.697264197153936, 1.1249625336275617],
58+
[5.697263485026567, 1.8750401628534337],
59+
[5.455069810373743, 2.5251482916876378],
60+
[4.776929339068159, 2.795372759575895],
61+
[3.92033110541304, 2.9018927284871063],
62+
[2.999998066091118, 2.929924058932193],
63+
[2.0796652780381826, 2.90189200881968],
64+
[1.2230677654766597, 2.7953714133566603],
65+
[0.544928391271399, 2.5251464933327794],
66+
[0.3027360471605494, 1.875038145804603],
67+
[0.302736726373967, 1.1249605602741133]]
68+
self.assertSmoothingValid(path, newpath_expected)
69+
70+
def test_secondPath(self):
71+
path = [[1, 0], # Move in the shape of a plus sign
72+
[2, 0],
73+
[2, 1],
74+
[3, 1],
75+
[3, 2],
76+
[2, 2],
77+
[2, 3],
78+
[1, 3],
79+
[1, 2],
80+
[0, 2],
81+
[0, 1],
82+
[1, 1]]
83+
84+
answer = [[1.239080543767428, 0.5047204351187283],
85+
[1.7609243903912781, 0.5047216452560908],
86+
[2.0915039821562416, 0.9085017167753027],
87+
[2.495281862032503, 1.2390825203587184],
88+
[2.4952805300504783, 1.7609262468826048],
89+
[2.0915003641706296, 2.0915058211575475],
90+
[1.7609195135622062, 2.4952837841027695],
91+
[1.2390757942466555, 2.4952826072236918],
92+
[0.9084962737918979, 2.091502621431358],
93+
[0.5047183914625598, 1.7609219230352355],
94+
[0.504719649257698, 1.2390782835562297],
95+
[0.9084996902674257, 0.9084987462432871]]
96+
97+
self.assertSmoothingValid(path, answer)
98+
99+
100+
if __name__ == "__main__":
101+
unittest.main()
102+
103+
104+

0 commit comments

Comments
 (0)
Please sign in to comment.