def short_distance(origins,(x,y),gap):
for (i,j) in origins.spilt(“ ”):
h=[]
h.append(float(math.sqrt ((i-x)*(i-x)+(j-y)*(j-y))))
for n in h:
if not gap < n:
print 0
if gap < n :
print n
Asked
Active
Viewed 244 times
-4
-
15It comes with no documentation! :P – Mike Mazur Nov 15 '09 at 09:27
-
1lol. What he means is, it is easier for us to help if you explain what you are trying to do, and what is actually happening – Colin Pickard Nov 15 '09 at 09:28
-
We are going to need a lot more detail. What is this function being used for? – Dominic Bou-Samra Nov 15 '09 at 09:29
-
If you would give an example of the input and output or some sort of description it would really help. We get the general idea you are trying to achieve but there are so many errors it's impossible to guess. – John La Rooy Nov 15 '09 at 11:45
-
2This sounds suspiciously like a homework question. If it is homework, we can help you, but you should be the one to actually solve it. – Judah Gabriel Himango Nov 15 '09 at 16:12
5 Answers
4
I would write the code more like this. If you run the code, it will report any failing tests (of which there are none).
import math
def short_distance(origins, point, gap):
"""
Describe origins, point, and gap are and what the
expected outcome is.
Then provide an example that tests the code
>>> short_distance('1,2 3,4', (5,6), 1.5)
5.65685424949
2.82842712475
"""
origins = parse_origins(origins)
distance_to_point = lambda point2: point_distance(point, point2)
# what's a better name for h?
h = map(distance_to_point, origins)
report(h, gap)
def report(h, gap):
"""
Take the results of the distances and report on them
"""
for distance in h:
if not (gap < distance):
print 0
else:
print distance
def point_distance(p1, p2):
"""
Calculate the distance between two points
>>> point_distance((0,0), (1,0))
1.0
more than one test here would be good
"""
x1, y1 = p1
x2, y2 = p2
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
def parse_origins(origin_string):
"""
Parse an origins string.
>>> parse_origins('1,2 3,4')
((1.0, 2.0), (3.0, 4.0))
"""
points = origin_string.split(' ')
return tuple(map(parse_point, points))
def parse_point(point_string):
"""
Take a string like 1,2 and return a tuple of the numbers
in that string.
>>> parse_point('1,2.0')
(1.0, 2.0)
"""
return tuple(map(float, point_string.split(',')))
def test():
import doctest
doctest.testmod()
if __name__ == '__main__':
test()

Jason R. Coombs
- 41,115
- 10
- 83
- 93
2
- the indentation is wrong; the
for
loops should be indented more than thedef
- typo:
origins.spilt(" ")
should probably beorigins.split(" ")

Mike Mazur
- 2,509
- 1
- 16
- 26
2
Your code is perhaps for finding points from origins
that are close to (x, y)
. There is a lot of errors in it:
- Indention is wrong.
split()
method is spelled wrong.split()
method returns flat list while you are expecting a list of pairs.
The former two are easy to fix. Without knowledge of origins
string format I can't be sure what dou you wish ere. See this question for solutions on how to convert flat list to list of pairs.
Also note that if
statement has else
clause, so you can write:
if gap < n:
print n
else:
print 0

Community
- 1
- 1

Denis Otkidach
- 32,032
- 8
- 79
- 100
0
- You'll need to import math
- The indentation is wrong
- If Origins is a string like
'1,1 2,2 3,3'
,Origins.split(" ")
will give you a list of strings["1,1", "2,2", "3,3"]
. You will need to do some extra work to be able to use it with the for loopfor (i,j) in ...
You need a list of tuples like [(1,1), (2,2), (3,3)] math.sqrt
already returns a float, so you can leave that out

John La Rooy
- 295,403
- 53
- 369
- 502
0
Here's the code:
from math import sqrt
def short_distance(origins,(x,y),gap):
def distance(i, j):
ix, iy = i - x, j - y
return sqrt (ix*ix + iy*iy)
all_distances = (distance(float(i), float(j)) for (i,j) in origins)
for n in all_distances:
print (0 if gap >= n else n)
And then use it like this:
>>> origin = (0, 0)
>>> points = [(1, 1), (2, 1), (1, 2), (2, 2)]
>>> gap = 1.5
>>> short_distance(points, origin, gap)
0
2.2360679775
2.2360679775
2.82842712475
My best guess is this does what you want.

hughdbrown
- 47,733
- 20
- 85
- 108