changeset 33:cfb7f28678c7

bisection() wasn't returning the index like it should. Added some simple tests.
author Brian Neal <bgneal@gmail.com>
date Wed, 09 Jan 2013 20:11:53 -0600
parents a13c00c0dfe5
children 66a5e7f7c10f
files ch3ex3.py
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ch3ex3.py	Wed Jan 09 20:09:19 2013 -0600
+++ b/ch3ex3.py	Wed Jan 09 20:11:53 2013 -0600
@@ -24,4 +24,12 @@
             lo = mid + 1
 
     n = lo - 1
-    return a[n] if n >= 0 and a[n] == item else None
+    return n if n >= 0 and a[n] == item else None
+
+
+if __name__ == '__main__':
+    a = [0, 2, 4, 5, 7, 22]
+
+    assert bisection(a, 0) == 0
+    assert bisection(a, 22) == len(a) - 1
+    assert bisection(a, 4) == 2