@@ -163,17 +163,19 @@ def dotproduct(X, Y):
163163 """
164164 return sum ([x * y for x , y in zip (X , Y )])
165165
166+ import operator #To make add operator.add work
166167def vector_add (a , b ):
167168 """Component-wise addition of two vectors.
168169 >>> vector_add((0, 1), (8, 9))
169170 (8, 10)
170171 """
171172 return tuple (map (operator .add , a , b ))
172173
174+ import random #As random works in python 3+ when its imported
173175def probability (p ):
174176 "Return true with probability p."
175177 return p > random .uniform (0.0 , 1.0 )
176-
178+ import bisect #As weighted_sammpler uses 'bisect' function
177179def weighted_sample_with_replacement (seq , weights , n ):
178180 """Pick n samples from seq at random, with replacement, with the
179181 probability of each element in proportion to its corresponding
@@ -190,12 +192,12 @@ def weighted_sampler(seq, weights):
190192
191193def num_or_str (x ):
192194 """The argument is a string; convert to a number if possible, or strip it.
193- >>> num_or_str('42')
195+ >> num_or_str('42')
194196 42
195197 >>> num_or_str(' 42x ')
196198 '42x'
197199 """
198- if isnumber ( x ): return x
200+ if x . isnumeric ( ): return x
199201 try :
200202 return int (x )
201203 except ValueError :
@@ -235,13 +237,16 @@ def turn_right(heading):
235237def turn_left (heading ):
236238 return turn_heading (heading , + 1 )
237239
238- def distance ((ax , ay ), (bx , by )):
239- "The distance between two (x, y) points."
240- return math .hypot ((ax - bx ), (ay - by ))
240+ import math
241+ def distance (a , b ):
242+ """The distance between two (x, y) points.
243+ >>> distanece((1,2),(5,5))
244+ 5.0 """
245+ return math .hypot ((a [0 ] - b [0 ]), (a [1 ] - b [1 ]))
241246
242- def distance2 (( ax , ay ), ( bx , by ) ):
247+ def distance2 (a , b ):
243248 "The square of the distance between two (x, y) points."
244- return (ax - bx )** 2 + (ay - by )** 2
249+ return (a [ 0 ] - b [ 0 ] )** 2 + (a [ 1 ] - b [ 1 ] )** 2
245250
246251def vector_clip (vector , lowest , highest ):
247252 """Return vector, except if any element is less than the corresponding
0 commit comments