# a7q3 - print "Test 1" expected = ans =...

This preview shows pages 1–2. Sign up to view the full content.

## CS 116 Ru Wang r46wang 20319247 ## Assignment 7 Question 3 import math ## short_distance: (listof posn) posn float -> (listof posn) ## consumes a list of posn, a target posn and a float indicating the maximum euclidean distance, it produces a list of all posns from the list of posns consumed in which their euclidean distance with the target posn is not greater than the float consumed. ## Examples: ## short_distance ([], [0, 0], 1) -> [] ## short_distance ([[1,1]],[0,0],1) -> [] ## short_distance ([[1,1],[2,2],[3,3],[4,4]],[0,0],2]) -> [[1,1]] ## short_distance ([[1,1],[2,2],[3,3],[4,4]],[0,0],3]) -> [[1,1],[2,2]] def short_distance (origins, destination, gap) : final = [] length = len(origins) pos = 0 while (pos < length): eucdis = math.sqrt(math.pow(origins[pos][0] - destination[0], 2) + math.pow(origins[pos][1] - destination[1], 2)) if (eucdis <= gap): final.append(origins[pos]) pos = pos + 1 else: pos = pos + 1 return final ## Testing: ## short_distance ([], [0, 0], 1) -> []

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: print "Test 1" expected = ans = short_distance (, [0, 0], 1) print expected == ans assert expected == ans ## short_distance ([[1,1]],[0,0],1) -> print "Test 2" expected = ans = short_distance ([[1,1]],[0,0],1) print expected == ans assert expected == ans ## short_distance ([[1,1],[2,2],[3,3],[4,4]],[0,0],2]) -> [[1,1]] print "Test 3" expected = [[1, 1]] ans = short_distance ([[1,1],[2,2],[3,3],[4,4]],[0,0],2) print expected == ans assert expected == ans ## short_distance ([[1,1],[2,2],[3,3],[4,4]],[0,0],3]) -> [[1,1],[2,2]] print "Test 4" expected = [[1,1],[2,2]] ans = short_distance ([[1,1],[2,2],[3,3],[4,4]],[0,0],3) print expected == ans assert expected == ans ## short_distance ([[4,4],[3,3],[2,2],[1,1]], [0,0],4]) print "Test 5" expected = [[3,3],[2,2],[1,1]] ans = short_distance ([[4,4],[3,3],[2,2],[1,1]], [0,0],5) print expected == ans assert expected == ans...
View Full Document

## This note was uploaded on 10/21/2010 for the course CS 116 taught by Professor T.vasiga during the Fall '09 term at Waterloo.

### Page1 / 2

a7q3 - print "Test 1" expected = ans =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online