Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

Untitled

By: a guest on Nov 27th, 2012  |  syntax: Python  |  size: 0.57 KB  |  views: 51  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #divide and conquer
2. def min(a):
3.     n = len(a)
4.     if n==1:
5.         return a[0]
6.     if n==2:
7.         if a[0] < a[1]:
8.             return a[0]
9.         return a[1]
10.     n = int(n / 2)
11.     return min([min(a[:n]),min(a[n:])])
12.
13. #incremental
14. def min(a, z):
15.     if a[0] < z:
16.         z = a[0]
17.
18.     if len(a)== 1:
19.         return z
20.     return min(a[1:],z)
21.
22. #equivalent, without the function overhead
23. def min(a):
24.     z = a[0]
25.     for i in a:
26.         if i < z:
27.             z = i
28.     return z
29.
30. print min([1,6,43,7,3,4,-6,8,45,3,7,5,34,6,8,89,3,2,7,2,7,4,8,89,5,-1])
clone this paste RAW Paste Data
Top