@@ -525,6 +525,15 @@ def normalize(numbers):
525
525
total = float (sum (numbers ))
526
526
return [n / total for n in numbers ]
527
527
528
+ def clip (x , lowest , highest ):
529
+ """Return x clipped to the range [lowest..highest].
530
+ >>> clip(-1, 0, 1)
531
+ 0
532
+ >>> clip(10, 0, 1)
533
+ 1
534
+ """
535
+ return max (lowest , min (x , highest ))
536
+
528
537
#______________________________________________________________________________
529
538
## OK, the following are not as widely useful utilities as some of the other
530
539
## functions here, but they do show up wherever we have 2D grids: Wumpus and
@@ -549,14 +558,15 @@ def distance2((ax, ay), (bx, by)):
549
558
"The square of the distance between two (x, y) points."
550
559
return (ax - bx )** 2 + (ay - by )** 2
551
560
552
- def clip (vector , lowest , highest ):
561
+ def vector_clip (vector , lowest , highest ):
553
562
"""Return vector, except if any element is less than the corresponding
554
563
value of lowest or more than the corresponding value of highest, clip to
555
564
those values.
556
- >>> clip ((-1, 10), (0, 0), (9, 9))
565
+ >>> vector_clip ((-1, 10), (0, 0), (9, 9))
557
566
(0, 9)
558
567
"""
559
- return type (vector )(map (min , map (max , vector , lowest ), highest ))
568
+ return type (vector )(map (clip , vector , lowest , highest ))
569
+
560
570
#______________________________________________________________________________
561
571
# Misc Functions
562
572
0 commit comments