Mercurial > public > think_complexity
comparison ch3ex3.py @ 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 | 0f98bcb5bd3f |
children |
comparison
equal
deleted
inserted
replaced
32:a13c00c0dfe5 | 33:cfb7f28678c7 |
---|---|
22 hi = mid | 22 hi = mid |
23 else: | 23 else: |
24 lo = mid + 1 | 24 lo = mid + 1 |
25 | 25 |
26 n = lo - 1 | 26 n = lo - 1 |
27 return a[n] if n >= 0 and a[n] == item else None | 27 return n if n >= 0 and a[n] == item else None |
28 | |
29 | |
30 if __name__ == '__main__': | |
31 a = [0, 2, 4, 5, 7, 22] | |
32 | |
33 assert bisection(a, 0) == 0 | |
34 assert bisection(a, 22) == len(a) - 1 | |
35 assert bisection(a, 4) == 2 |